Results 1 to 8 of 8
Like Tree1Likes
  • 1 Post By DarrylBurke

Thread: Fractals!

  1. #1
    nklein is offline Member
    Join Date
    Mar 2012
    Posts
    3
    Rep Power
    0

    Default 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();
    			direction+=(Math.toRadians(60));
    			drawFractal(canvas,x,y,direction,length/3,(level-1));
    			x = (int)canvas.getX();
    			y = (int)canvas.getY();
    			direction-=(Math.toRadians(120));
    			drawFractal(canvas,x,y,direction,length/3,(level-1));
    			x = (int)canvas.getX();
    			y = (int)canvas.getY();
    			direction+=(Math.toRadians(60));
    			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;
    		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();
    	}
    }
    Here's what is looks like for level = 2:
    Fractals!-screen-shot-2012-03-23-12.47.08-am.png

    And for level = 3:
    Fractals!-screen-shot-2012-03-23-12.47.26-am.png

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

  2. #2
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default 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. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

  4. #4
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    7

    Default Re: Fractals

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

  5. #5
    nicephotog is offline Member
    Join Date
    Mar 2012
    Posts
    41
    Rep Power
    0

    Default 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).

  6. #6
    nklein is offline Member
    Join Date
    Mar 2012
    Posts
    3
    Rep Power
    0

    Default 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,(level-1));
    		x+=(length*Math.cos(direction)/3);
    		y+=(length*Math.sin(direction)/3);
    		direction = direction + (Math.toRadians(60));
    		
    		drawFractal(x,y,direction,length/3,(level-1));
    		x+=(length*Math.cos(direction)/3);
    		y+=(length*Math.sin(direction)/3);
    		direction-=(Math.toRadians(120));
    		
    		drawFractal(x,y,direction,length/3,(level-1));
    		x+=(length*Math.cos(direction)/3);
    		y+=(length*Math.sin(direction)/3);
    		direction+=(Math.toRadians(60));
    		
    		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);
    				direction-=(Math.toRadians(120));
    		}
    	}
    	
    }

    Some cool pictures:
    Level = 4
    Fractals!-screen-shot-2012-03-27-8.22.27-pm.png

    Level = 7
    Fractals!-screen-shot-2012-03-27-8.22.48-pm.png

    Using a for loop, for(int z = 0; z<=level; z++) where z is the level,
    Fractals!-screen-shot-2012-03-27-8.21.52-pm.png

    Thanks everyone!

  7. #7
    nklein is offline Member
    Join Date
    Mar 2012
    Posts
    3
    Rep Power
    0

    Default 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)

  8. #8
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,184
    Rep Power
    19

    Default Re: Fractals!

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

    db
    Fubarable likes this.
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •