0

I'm trying to make a program that draws a Koch fractal. Is there any way I can draw a line in Java by length instead of coordinates?

A koch fractal looks kind of like a snowflake. The repeating pattern is equilateral triangles inserted 1/3 of the way into each line (with the triangle's sides being 1/3 the length of the line). enter image description here

Originally I was trying to draw triangles recursively, but I couldn't figure out how to calculate the coordinates. Then I thought it would be way easier if I could just draw lines of a certain length and rotate them, and reduce the length of the lines each time. Except that I don't know if I even can draw lines by length in Java. I have tried searching the internet for this and have not found an answer, which makes me think it's not possible, but I thought I would ask here just to make sure.

I realize this is way beyond my technical college level. I also realize I could probably find a complete program that someone else has already written, but I want to see if I can figure it out (mostly) on my own.

sparkhee93
  • 1,381
  • 3
  • 21
  • 30
  • Which drawing/painting API? – James Wierzba Feb 25 '16 at 19:17
  • What did you search for? Did you look at the related questions when you posted this one? – Amit Feb 25 '16 at 19:20
  • Possible duplicate of [Draw a line at a specific angle in Java](http://stackoverflow.com/questions/3536428/draw-a-line-at-a-specific-angle-in-java) – Amit Feb 25 '16 at 19:22
  • I did read that question but I couldn't figure out how to apply it to my problem. @sparky thanks for editing this, it is my first question here and my natural tendency is to over-inform – Jacqueline Connors Feb 26 '16 at 05:22
  • Hi Jacqueline, if this or any answer has solved your question please consider accepting it by clicking the check-mark. This indicates to the wider community that you've found a solution and gives some reputation to both the person who answered and to yourself. There is no obligation to do this. – Seany84 Jul 26 '16 at 19:46

1 Answers1

0

First of all, I am assuming that you have some sort of function drawLine(int x1, int y1, int x2, int y2) in whatever API you are using for Java. If this is true, and you want to draw a line by length I would believe you could just do it using the standard trigonometry functions (Math.sin(...), Math.cos(...) and Math.tan(...)).

Example

What you would want to draw using a given length is at least the following data:

  • A starting coordinate
  • The angle of the line with the line y = c (where c is any number)
  • The length of the line

Your code could then use something like this:

public void drawLineByLength(int xStart, int yStart, double angle, double length) {
    int xEnd = (int) (xStart + (Math.cos(angle) * length));
    int yEnd = (int) (yStart + (Math.sin(angle) * length));

    drawLine(xStart, yStart, xEnd, yEnd);
}

Note that you will have to import the Math class for this method. In addition, it will only work if you were to have a drawLine(...) function available to you that takes the coordinates of two points.

Implementation

If I understand your intentions, you would want to keep track of the current coordinate the "pen" is at; the angle it is drawing at; and the length of the next line it is going to draw. You could add something at the end of the drawLineByLength(...) method that updates these variables.

Vulcano
  • 155
  • 9
  • I am using drawLine with 4 coordinates. The thing that threw me off is, my understanding is that the program will completely re-draw the whole line every time it increases a level of complexity. It's easy to draw this design by hand just adding more triangles, but when I started thinking about redrawing the whole thing every time using coordinates, it seemed impossible to figure out the new coordinates. I guess I need to refresh my trig skills. – Jacqueline Connors Feb 26 '16 at 05:18