Results 1 to 5 of 5
  1. #1
    jrobw is offline Member
    Join Date
    Dec 2008
    Posts
    3
    Rep Power
    0

    Default recursion sierpinskys triangle - HELP!!

    hi!

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

    Java 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

    Java 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

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

  3. #3
    jrobw is offline Member
    Join Date
    Dec 2008
    Posts
    3
    Rep Power
    0

    Default

    @db: what's your/the problem with that?

    sorry, i didn't know, that it's not ok, to post on several forums! i thought posting in different categories in one forum is not ok, but why not in different forums??

    i do not mean to spam or something, i am just looking for help!!

    are there the same people watching all java-forums anyway?? in this case, i can of course just post in one forum.

    i do not understand, why using different forums parallel is not liked! maybe someone can explain that to me! and besides, i'd still be happy for just some help with my problem..

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    It's OK as long as you're up front about and provide links. No one likes wasting there time working on a solution for someone else only to find out that it was solved hours ago and posted on another site. Remember, we're all volunteers with families, jobs, and lives of our own. So out of consideration for those who might help you, we ask you to post links to any and all cross-posts.

    edit: as an example, I now see that I've just wasted my time since prometheuzz just posted a similar response to you elsewhere I see. Sigh....

    I'm out'a here.
    Last edited by Fubarable; 12-08-2008 at 05:14 PM.

  5. #5
    jrobw is offline Member
    Join Date
    Dec 2008
    Posts
    3
    Rep Power
    0

    Default

    I am not so experienced in forum usage and I didn't think to do something wrong with that.

    But I got the point now, I am sorry and the links, where I posted are added in alle posts now!!

    So, maybe there's still someone with a hint for my recursion problem. or maybe someone has a link to where I can learn about how the recursion is technically implemented in java (what exactly does the thread do at recusion, how is a stack used for this..)?

Similar Threads

  1. Triangle
    By jkswebsite in forum New To Java
    Replies: 8
    Last Post: 01-10-2009, 02:08 PM
  2. Is it a right triangle? (Code help)
    By TheApostle in forum New To Java
    Replies: 8
    Last Post: 10-07-2008, 08:39 PM
  3. Triangle Sides program
    By jamesov89 in forum New To Java
    Replies: 6
    Last Post: 10-06-2008, 03:36 AM
  4. Pascal Triangle help
    By Magic101 in forum New To Java
    Replies: 4
    Last Post: 05-01-2008, 07:51 PM
  5. Making triangle
    By banie in forum New To Java
    Replies: 4
    Last Post: 02-02-2008, 11:23 AM

Posting Permissions

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