| | |
recursion sierpinskys triangle - HELP!!
![]() |
•
•
Join Date: Dec 2008
Posts: 13
Reputation:
Solved Threads: 0
hi!
i have to write a gui window, which draws sierpinskys triangle recursively, i have one main class:
AND MORE IMPORTANT: the triangle class with the recursion
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
btw: i know that the condition in the while loop is not the right recirsive base condition, but i didn't think about the right one yet and just used it like it is there for testing, i want to fix that if my method principally works correct
i have to write a gui window, which draws sierpinskys triangle recursively, i have one main class:
Java Syntax (Toggle Plain Text)
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; //frame setup taken from: //http://java.sun.com/docs/books/tutorial/uiswing/examples/components/FrameDemoProject/src/components/FrameDemo.java 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 Syntax (Toggle Plain Text)
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
btw: i know that the condition in the while loop is not the right recirsive base condition, but i didn't think about the right one yet and just used it like it is there for testing, i want to fix that if my method principally works correct
•
•
Join Date: Dec 2008
Posts: 13
Reputation:
Solved Threads: 0
thats what i do in the method, i use the parameter points ro define the points A1, A2... they are not 6, but 9 points, but 3 of them are the old ones, so its them same..
my problem was, that i declared the points A1, A2... as fields... now i declare them inside the method and it works!
but there is a question left: I am useing int i for the termination condition in the while loop, but thats not so good, as its not flexible.
do you know, what would be a good termnination condition for the while loop/for the recursion, maybe something relating to the points??
thanks a lot
my problem was, that i declared the points A1, A2... as fields... now i declare them inside the method and it works!
but there is a question left: I am useing int i for the termination condition in the while loop, but thats not so good, as its not flexible.
do you know, what would be a good termnination condition for the while loop/for the recursion, maybe something relating to the points??
thanks a lot
•
•
Join Date: Dec 2008
Posts: 13
Reputation:
Solved Threads: 0
ok, as I am curious about what you want to show me (I always want to learn, especially as recursion is really tough!) I'll try and heres the method which just describes the 3 new triangles from the parameter triangles:
besides: after I changed the class variable points to local method variables my code drew the fractal correctly thats why I am sure.
but I wrote this fragment as you wished, because I would like to know what you were going to explain, cause I stil don't feel to have really understood recursion and I don't know how to determine the termination condition.
thanks a lot for your help
Java Syntax (Toggle Plain Text)
public void drawFractal(Graphics g, Point a, Point b, Point c){ //first triangle Point A1 = new Point(a); Point B1 = new Point((b.x-a.x)/2, b.y); Point C1 = new Point((a.x+c.x)/2, (c.y+a.y)/2); g.drawLine(A1.x, A1.y, B1.x, B1.y); g.drawLine(A1.x, A1.y, C1.x, C1.y); g.drawLine(B1.x, B1.y, C1.x, C1.y); //second triangle Point A2 = new Point(c.x, b.y); Point B2 = new Point(b.x, b.y); Point C2 = new Point((c.x+b.x)/2, (c.y+a.y)/2); g.drawLine(A2.x, A2.y, B2.x, B2.y); g.drawLine(A2.x, A2.y, C2.x, C2.y); g.drawLine(B2.x, B2.y, C2.x, C2.y); //third triangle Point A3 = new Point((a.x+c.x)/2, (c.y+a.y)/2); Point B3 = new Point((c.x+b.x)/2, (c.y+a.y)/2); Point C3 = new Point(c.x, c.y); g.drawLine(A3.x, A3.y, B3.x, B3.y); g.drawLine(A3.x, A3.y, C3.x, C3.y); g.drawLine(B3.x, B3.y, C3.x, C3.y); }
besides: after I changed the class variable points to local method variables my code drew the fractal correctly thats why I am sure.
but I wrote this fragment as you wished, because I would like to know what you were going to explain, cause I stil don't feel to have really understood recursion and I don't know how to determine the termination condition.
thanks a lot for your help
•
•
Join Date: Dec 2008
Posts: 13
Reputation:
Solved Threads: 0
Java Syntax (Toggle Plain Text)
Put i as "level" inside public void drawFractal(int level,Graphics g, Point a, Point b, Point c)
ah, ok. so using a level seems to the right way. i was thinking the bascase (or termination condition) must be defined in relation to the decreasing elements (in this case the length of the triangle lines..). for example in the while loop:
Java Syntax (Toggle Plain Text)
while(b.x-a.x>5){ ... }
but it seemed to produce an infinite loop...
•
•
Join Date: Nov 2008
Posts: 332
Reputation:
Solved Threads: 53
ok. we divides triangles recursively, but we put on screen triangles, for whos level==1. Try apply for
public void drawFractal(int level,Graphics g, Point a, Point b, Point c){
if (level == 1) {
/**
* draw inner reversed 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);
} else {
/// calculation of new points
drawFractal(level-1,g, A1, B1, C1);
drawFractal(level-1,g, A2, B2, C2);
drawFractal(level-1,g, A3, B3, C3);
}
}•
•
Join Date: Dec 2008
Posts: 13
Reputation:
Solved Threads: 0
uhh, I do not completely understand your approach..
I implemented the class with your suggestion as follows:
I called it with 100 for level in the paint, because I didn't know how to choose a good value for it, how can I?
if the lengst of the hypothenuse of the first triangle (the distance AB) always splits with every recursive step, can I call the method with something logarithmic relating to the width of the gui window? maybe(pseudocode
level = log2(frameWidth); ??
or how do I determine, how many levels of recursion I actually need???
It doesn't draw anything with this implementation, I probably missed something in the recursive method, maybe didn't see how you really wanted me to program it...
sorry for so many beginer questions..
I implemented the class with your suggestion as follows:
Java Syntax (Toggle Plain Text)
package triangleMod; import java.awt.Color; import java.awt.Component; import java.awt.Graphics; import java.awt.Point; class TriangleQuu extends Component{ private static final long serialVersionUID = 1L; //A, B, C initial triangle-points, assigned in the constructor private Point A, B, C; public TriangleQuu(Point a, Point b, Point c){ this.A = a; this.B = b; this.C = c; } public void drawFractal(int level,Graphics g, Point a, Point b, Point c){ if (level == 1) { /** * draw inner reversed 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); } else { //lower left triangle coordinates Point A1 = new Point(a.x, a.y); Point B1 = new Point(c.x, b.y); Point C1 = new Point((a.x+c.x)/2, (c.y+a.y)/2); //lower right triangle coordinates Point A2 = new Point(c.x, b.y); Point B2 = new Point(b.x, b.y); Point C2 = new Point((c.x+b.x)/2, (c.y+a.y)/2); //upper triangle coordinates Point A3 = new Point((a.x+c.x)/2, (c.y+a.y)/2); Point B3 = new Point((c.x+b.x)/2, (c.y+a.y)/2); Point C3 = new Point(c.x, c.y); drawFractal(level-1,g, A1, B1, C1); drawFractal(level-1,g, A2, B2, C2); drawFractal(level-1,g, A3, B3, C3); } } public void paint(Graphics g){ drawFractal(100, g, A, B, C); } }
I called it with 100 for level in the paint, because I didn't know how to choose a good value for it, how can I?
if the lengst of the hypothenuse of the first triangle (the distance AB) always splits with every recursive step, can I call the method with something logarithmic relating to the width of the gui window? maybe(pseudocode
level = log2(frameWidth); ??or how do I determine, how many levels of recursion I actually need???
It doesn't draw anything with this implementation, I probably missed something in the recursive method, maybe didn't see how you really wanted me to program it...
sorry for so many beginer questions..
•
•
Join Date: Nov 2008
Posts: 332
Reputation:
Solved Threads: 53
I invoked with
it is why i want from You once more write new coordinates of triangle. Remark new points are in half of triangle edge. Write external function
level=8[ & i recived this thumbit is why i want from You once more write new coordinates of triangle. Remark new points are in half of triangle edge. Write external function
Point midPoint(Point a,Point b){
//..
return new Point(....);
}![]() |
Other Threads in the Java Forum
- Previous Thread: Help on arrays!! Progress Made!!
- Next Thread: Loan repayment calculator
| Thread Tools | Search this Thread |
-xlint add android api applet application applications array arrays automation bank bi binary blackberry bluetooth chat class clear client code compile compiler component database development dice digit eclipse equation error event formatingtextintooltipjava fractal freeze functiontesting game gameprogramming givemetehcodez graphics gui health html hyper ide idea image infinite int integer j2me java javame javaprojects jetbrains jni jpanel jtable julia learningresources linux list main map method methods mobile myregfun netbeans nonstatic notdisplaying openjavafx pearl problem program project qt recursion repositories scanner screen scrollbar server set sms sort sorting spamblocker sql sqlserver storm string superclass swing system thread threads tree variablebinding windows xor





