Results 1 to 8 of 8
 03232012, 05:51 AM #1Member
 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,(level1)); x = (int)canvas.getX(); y = (int)canvas.getY(); direction+=(Math.toRadians(60)); drawFractal(canvas,x,y,direction,length/3,(level1)); x = (int)canvas.getX(); y = (int)canvas.getY(); direction=(Math.toRadians(120)); drawFractal(canvas,x,y,direction,length/3,(level1)); x = (int)canvas.getX(); y = (int)canvas.getY(); direction+=(Math.toRadians(60)); drawFractal(canvas,x,y,direction,length/3,(level1)); 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; double direction = Math.toRadians(0); for(int i = 0; i<3; i++) { drawFractal(canvas,x,y,direction,length,level); direction=(Math.toRadians(120)); x = (int)canvas.getX(); y = (int)canvas.getY(); } canvas.display(); } }
And for level = 3:
Thanks in advance for any help!Last edited by nklein; 03232012 at 07:45 AM.
 03232012, 05:49 PM #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?

Re: Fractals
Where is your Picture class?
 03242012, 12:22 AM #4Moderator
 Join Date
 Jul 2010
 Location
 California
 Posts
 1,642
 Rep Power
 7
Re: Fractals
Forget all that casting and try using something like the Line2D class (assuming your canvas Picture class can draw a Shape)
 03262012, 12:55 AM #5Member
 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).
 03282012, 02:24 AM #6Member
 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))); add(line); } 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,(level1)); x+=(length*Math.cos(direction)/3); y+=(length*Math.sin(direction)/3); direction = direction + (Math.toRadians(60)); drawFractal(x,y,direction,length/3,(level1)); x+=(length*Math.cos(direction)/3); y+=(length*Math.sin(direction)/3); direction=(Math.toRadians(120)); drawFractal(x,y,direction,length/3,(level1)); x+=(length*Math.cos(direction)/3); y+=(length*Math.sin(direction)/3); direction+=(Math.toRadians(60)); drawFractal(x,y,direction,length/3,(level1)); 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); direction=(Math.toRadians(120)); } } }
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!
 03282012, 02:35 AM #7Member
 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)
 03282012, 03:16 AM #8
Bookmarks