1. Member
Join Date
Mar 2012
Posts
3
Rep Power
0

## Fractals!

Hey everyone,

I'm trying to draw a snowflake fractal, but there's some sort of rounding error in my program and it keeps appearing lopsided. Can anyone see what's wrong? The picture class is just a basic program which allows me to draw pictures. I'm hoping the problem is in this program and not there.

Here's the program: (Pretty simple)

Java Code:
```public class Fractal {
public static void drawLine(Picture canvas,int x,int y,double length,double direction) {
canvas.drawLine(x,y,(int)(x+length*Math.cos(direction)),(int)(y+length*Math.sin(direction)));
}

public static void drawFractal(Picture canvas,int x, int y,double direction,double length,int level) {
if(level==1) {
drawLine(canvas,x,y,(length/3),direction);
}
else {
drawFractal(canvas,x,y,direction,length/3,(level-1));
x = (int)canvas.getX();
y = (int)canvas.getY();
drawFractal(canvas,x,y,direction,length/3,(level-1));
x = (int)canvas.getX();
y = (int)canvas.getY();
drawFractal(canvas,x,y,direction,length/3,(level-1));
x = (int)canvas.getX();
y = (int)canvas.getY();
drawFractal(canvas,x,y,direction,length/3,(level-1));
x = (int)canvas.getX();
y = (int)canvas.getY();
}

}

public static void main (String[] args) {
int width = 1000;
int height = 600;
Picture canvas = new Picture(width,height);
int x = width/2;
int y = height/2;
int level = 3;
double length = 200;

for(int i = 0; i<3; i++) {
drawFractal(canvas,x,y,direction,length,level);
x = (int)canvas.getX();
y = (int)canvas.getY();
}
canvas.display();
}
}```
Here's what is looks like for level = 2:

And for level = 3:

Thanks in advance for any help!
Last edited by nklein; 03-23-2012 at 08:45 AM.

2. ## Re: Fractals

Maybe it's the cumulative effect of your casting x and y to ints? Maybe you need to round them to the nearest int - a repeated casting of something like 1.9, 5.8, 7.9. 12.8 and so forth would have a cumulative effect of a noticeable number of pixels after several iterations. The Graphics2D object in java supports drawing with floating point coordinates, perhaps that would help?

3. Moderator
Join Date
Jul 2010
Location
California
Posts
1,638
Rep Power
12

## Re: Fractals

Forget all that casting and try using something like the Line2D class (assuming your canvas Picture class can draw a Shape)

4. Member
Join Date
Mar 2012
Posts
41
Rep Power
0

## Re: Fractals!

Hazard a guess, the Math.toRadians( as is stated by the API docs is approximate and if that's what is done to draw it is that for loop(3 times) then it's unnaccurate, unless created once then placed on the correct orienting pixel to draw three times as chosen(calculated by you).

5. Member
Join Date
Mar 2012
Posts
3
Rep Power
0

## Re: Fractals

Yeah, you guys are right. I actually got it to work using my picture class, although there were still tiny errors, so I decided to just go with GLine. Here's the program that I ended up using - it's pretty fun to play around with, and is a great example of recursion:

Java Code:
```import acm.graphics.GLine;
import acm.program.GraphicsProgram;

public class Fractal2 extends GraphicsProgram {
private static final long serialVersionUID = 10L;

public void drawLine(double x,double y,double length,double direction) {
GLine line = new GLine(x,y,(x+length*Math.cos(direction)),(y+length*Math.sin(direction)));
}

public void drawFractal(double x, double y,double direction,double length,int level) {
if(level==1) {
drawLine(x,y,length,direction);
x+=(length*Math.cos(direction));
y+=(length*Math.sin(direction));
}
else {
drawFractal(x,y,direction,length/3,(level-1));
x+=(length*Math.cos(direction)/3);
y+=(length*Math.sin(direction)/3);

drawFractal(x,y,direction,length/3,(level-1));
x+=(length*Math.cos(direction)/3);
y+=(length*Math.sin(direction)/3);

drawFractal(x,y,direction,length/3,(level-1));
x+=(length*Math.cos(direction)/3);
y+=(length*Math.sin(direction)/3);

drawFractal(x,y,direction,length/3,(level-1));
x+=(length*Math.cos(direction)/3);
y+=(length*Math.sin(direction)/3);
}

}
public void run(){
int width = getHeight();
int height = getWidth();
double x = width/2;
double y = height/2 + 100;
int level = 5;
double length = 500;
double direction = 0;

for(int i = 0; i<3; i++) {
drawFractal(x,y,direction,length,level);
x = x+length*Math.cos(direction);
y = y+length*Math.sin(direction);
}
}

}```

Some cool pictures:
Level = 4

Level = 7

Using a for loop, for(int z = 0; z<=level; z++) where z is the level,

Thanks everyone!

6. Member
Join Date
Mar 2012
Posts
3
Rep Power
0

## Re: Fractals!

Actually it turns out it's impossible to draw a fractal perfectly unless the class you use accepts doubles. I got it to work pretty well using the Picture class here, but in the end I decided it was better to avoid the problem altogether. (I posted the code in this thread if anyone is curious)

7. ## Re: Fractals!

Originally Posted by nklein
(I posted the code in this thread if anyone is curious)
Next time, don't double post. I'm merging the two threads.

db