View Single Post
  #1 (permalink)  
Old 12-08-2008, 03:48 PM
jrobw jrobw is offline
Member
 
Join Date: Dec 2008
Posts: 3
Rep Power: 0
jrobw is on a distinguished road
Default recursion sierpinskys triangle - HELP!!
hi!

i have to write a gui window, which draws sierpinskys triangle recursively, i have one main class:

Code:
package triangleMod;
import java.awt.Dimension;
import java.awt.Frame;
import java.awt.Point;

import javax.swing.JFrame;


public class Main extends Frame{
	
	static Point a;
	static Point b;
	static Point c;
	static Triangle tryAngle;
	static JFrame frame;
	

	private static final long serialVersionUID = 1L;

    private static void createAndShowGUI() {
    	//Create and set up the window.
        frame = new JFrame("Fractal: Sierpinski's Triangle");
    	a = new Point(0, 443);
    	b = new Point(512, 443);
    	c = new Point(256, 0);
    	tryAngle = new Triangle(a, b, c);
        
        tryAngle.setPreferredSize(new Dimension(514, 446));
        //frame.add(tryAngle);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        frame.getContentPane().add(tryAngle);

        //Display the window.
        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String[] args) {
           //Schedule a job for the event-dispatching thread:
        //creating and showing this application's GUI.
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
            }
        });
    }
}
AND MORE IMPORTANT: the triangle class with the recursion

Code:
package triangleMod;
import java.awt.Component;
import java.awt.Graphics;
import java.awt.Point;

class Triangle extends Component{

	private static final long serialVersionUID = 1L;

	//A, B, C initial triangle-points, assigned in the constructor
	private Point A, B, C;
	//points for the 3 new inner triangles in the current recursive step
	private Point A1, B1, C1, A2, B2, C2, A3, B3, C3;

	public Triangle(Point a, Point b, Point c){
		this.A = a; 
		this.B = b; 
		this.C = c;
		}
	
	//helper var for recursion termination
	int i= 300;
	
	//draws fractal
	public void drawFractal(Graphics g, Point a, Point b, Point c){
		
		/**
		 * draw outer triangle
		 **/
		
		//AB
		g.drawLine(a.x, a.y, b.x, b.y);
		//AC
		g.drawLine(a.x, a.y, c.x, c.y);
		//BC
		g.drawLine(b.x, b.y, c.x, c.y);
		
		/** 
		 * Points of the 3 new triangles get assigned
		 **/
		
		//lower left triangle coordinates
		A1 = new Point(A.x, A.y);
		B1 = new Point(c.x, b.y);
		C1 = new Point((a.x+c.x)/2, (c.y+a.y)/2);
		
		//lower right triangle coordinates
		A2 = new Point(c.x, b.y);
		B2 = new Point(B.x, B.y);
		C2 = new Point((c.x+b.x)/2, (c.y+a.y)/2);
		
		//upper triangle coordinates
		A3 =  new Point((a.x+c.x)/2, (c.y+a.y)/2);
		B3 = new Point((c.x+b.x)/2, (c.y+a.y)/2);
		C3 = new Point(C.x, C.y);
		
		/** 
		 * draw inner reversed triangle
		 **/
		
		//AB
		g.drawLine(A3.x, A3.y, B3.x, B3.y);
		//AC
		g.drawLine(A3.x, A3.y, A2.x, A2.y);
		//BC
		g.drawLine(B3.x, B3.y, A2.x, A2.y);
		
		/**
		 * rekusive method calls for the three new triangles
		 **/
		
	//termination condition not set correctly, helper var i used for testing
		while (i-->0) {			
			
			drawFractal(g, A1, B1, C1);
			//drawFractal(g, A2, B2, C2);
			//drawFractal(g, A3, B3, C3);
			
		}
	}

	public void paint(Graphics g){
		drawFractal(g, A, B, C);
	}
}
THE PROBLEM:

as you see, in the whle loop i commented two of the recursive calls out, if I just have one of the 3 calls (no matter which one of the three), it works, the triangles in that direction are drawn. but if I have two or three of the recursive call, it does not work, it's not drawing the triangles i want. the three recursive calls seem to affect each other in a way i don't want.

i just don't get each of the recursive drawing just the triangles they do draw, when i just have the one recursive call in the while loop. i thought it should work, as i have separated, always newly defined points for A1, B1, C1 and so on...

maybe i am misunderstanding how the java stack recursion works, can anyone maybe test my code and give any help??

would really be cool, thx a lot
Reply With Quote