recursion sierpinskys triangle - HELP!!

Reply

Join Date: Dec 2008
Posts: 13
Reputation: jrobw is an unknown quantity at this point 
Solved Threads: 0
jrobw jrobw is offline Offline
Newbie Poster

recursion sierpinskys triangle - HELP!!

 
0
  #1
Dec 8th, 2008
hi!

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

  1. package triangleMod;
  2. import java.awt.Dimension;
  3. import java.awt.Frame;
  4. import java.awt.Point;
  5.  
  6. import javax.swing.JFrame;
  7.  
  8.  
  9. public class Main extends Frame{
  10.  
  11. static Point a;
  12. static Point b;
  13. static Point c;
  14. static Triangle tryAngle;
  15. static JFrame frame;
  16.  
  17.  
  18. private static final long serialVersionUID = 1L;
  19.  
  20. //frame setup taken from:
  21. //http://java.sun.com/docs/books/tutorial/uiswing/examples/components/FrameDemoProject/src/components/FrameDemo.java
  22. private static void createAndShowGUI() {
  23. //Create and set up the window.
  24. frame = new JFrame("Fractal: Sierpinski's Triangle");
  25. a = new Point(0, 443);
  26. b = new Point(512, 443);
  27. c = new Point(256, 0);
  28. tryAngle = new Triangle(a, b, c);
  29.  
  30. tryAngle.setPreferredSize(new Dimension(514, 446));
  31. //frame.add(tryAngle);
  32. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  33.  
  34. frame.getContentPane().add(tryAngle);
  35.  
  36. //Display the window.
  37. frame.pack();
  38. frame.setVisible(true);
  39. }
  40.  
  41. public static void main(String[] args) {
  42. //Schedule a job for the event-dispatching thread:
  43. //creating and showing this application's GUI.
  44. javax.swing.SwingUtilities.invokeLater(new Runnable() {
  45. public void run() {
  46. createAndShowGUI();
  47. }
  48. });
  49. }
  50. }

AND MORE IMPORTANT: the triangle class with the recursion

  1. package triangleMod;
  2. import java.awt.Component;
  3. import java.awt.Graphics;
  4. import java.awt.Point;
  5.  
  6. class Triangle extends Component{
  7.  
  8. private static final long serialVersionUID = 1L;
  9.  
  10. //A, B, C initial triangle-points, assigned in the constructor
  11. private Point A, B, C;
  12. //points for the 3 new inner triangles in the current recursive step
  13. private Point A1, B1, C1, A2, B2, C2, A3, B3, C3;
  14.  
  15. public Triangle(Point a, Point b, Point c){
  16. this.A = a;
  17. this.B = b;
  18. this.C = c;
  19. }
  20.  
  21. //helper var for recursion termination
  22. int i= 300;
  23.  
  24. //draws fractal
  25. public void drawFractal(Graphics g, Point a, Point b, Point c){
  26.  
  27. /**
  28.   * draw outer triangle
  29.   **/
  30.  
  31. //AB
  32. g.drawLine(a.x, a.y, b.x, b.y);
  33. //AC
  34. g.drawLine(a.x, a.y, c.x, c.y);
  35. //BC
  36. g.drawLine(b.x, b.y, c.x, c.y);
  37.  
  38. /**
  39.   * Points of the 3 new triangles get assigned
  40.   **/
  41.  
  42. //lower left triangle coordinates
  43. A1 = new Point(A.x, A.y);
  44. B1 = new Point(c.x, b.y);
  45. C1 = new Point((a.x+c.x)/2, (c.y+a.y)/2);
  46.  
  47. //lower right triangle coordinates
  48. A2 = new Point(c.x, b.y);
  49. B2 = new Point(B.x, B.y);
  50. C2 = new Point((c.x+b.x)/2, (c.y+a.y)/2);
  51.  
  52. //upper triangle coordinates
  53. A3 = new Point((a.x+c.x)/2, (c.y+a.y)/2);
  54. B3 = new Point((c.x+b.x)/2, (c.y+a.y)/2);
  55. C3 = new Point(C.x, C.y);
  56.  
  57. /**
  58.   * draw inner reversed triangle
  59.   **/
  60.  
  61. //AB
  62. g.drawLine(A3.x, A3.y, B3.x, B3.y);
  63. //AC
  64. g.drawLine(A3.x, A3.y, A2.x, A2.y);
  65. //BC
  66. g.drawLine(B3.x, B3.y, A2.x, A2.y);
  67.  
  68. /**
  69.   * rekusive method calls for the three new triangles
  70.   **/
  71.  
  72. //termination condition not set correctly, helper var i used for testing
  73. while (i-->0) {
  74.  
  75. drawFractal(g, A1, B1, C1);
  76. //drawFractal(g, A2, B2, C2);
  77. //drawFractal(g, A3, B3, C3);
  78.  
  79. }
  80. }
  81.  
  82. public void paint(Graphics g){
  83. drawFractal(g, A, B, C);
  84. }
  85. }

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
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 332
Reputation: quuba is on a distinguished road 
Solved Threads: 53
quuba quuba is offline Offline
Posting Whiz

Re: recursion sierpinskys triangle - HELP!!

 
0
  #2
Dec 8th, 2008
In public void drawFractal(Graphics g, Point a, Point b, Point c){ use a,b,c as base to set a 3 new (a1,a2,a3) points of division. Use this 6 points to describe 3 new triangles. Post this fragment.
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 13
Reputation: jrobw is an unknown quantity at this point 
Solved Threads: 0
jrobw jrobw is offline Offline
Newbie Poster

Re: recursion sierpinskys triangle - HELP!!

 
0
  #3
Dec 8th, 2008
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
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 332
Reputation: quuba is on a distinguished road 
Solved Threads: 53
quuba quuba is offline Offline
Posting Whiz

Re: recursion sierpinskys triangle - HELP!!

 
0
  #4
Dec 8th, 2008
Hola, hola.
Are You absolutely sure?
Describe 3 new triangles from attached thumb.
Other things later.
Attached Thumbnails
tri.gif  
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 332
Reputation: quuba is on a distinguished road 
Solved Threads: 53
quuba quuba is offline Offline
Posting Whiz

Re: recursion sierpinskys triangle - HELP!!

 
0
  #5
Dec 8th, 2008
Originally Posted by jrobw
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.
Put i as "level" inside public void drawFractal(int level,Graphics g, Point a, Point b, Point c)
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 13
Reputation: jrobw is an unknown quantity at this point 
Solved Threads: 0
jrobw jrobw is offline Offline
Newbie Poster

Re: recursion sierpinskys triangle - HELP!!

 
0
  #6
Dec 8th, 2008
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:
  1. public void drawFractal(Graphics g, Point a, Point b, Point c){
  2.  
  3. //first triangle
  4. Point A1 = new Point(a);
  5. Point B1 = new Point((b.x-a.x)/2, b.y);
  6. Point C1 = new Point((a.x+c.x)/2, (c.y+a.y)/2);
  7.  
  8. g.drawLine(A1.x, A1.y, B1.x, B1.y);
  9. g.drawLine(A1.x, A1.y, C1.x, C1.y);
  10. g.drawLine(B1.x, B1.y, C1.x, C1.y);
  11.  
  12. //second triangle
  13. Point A2 = new Point(c.x, b.y);
  14. Point B2 = new Point(b.x, b.y);
  15. Point C2 = new Point((c.x+b.x)/2, (c.y+a.y)/2);
  16.  
  17. g.drawLine(A2.x, A2.y, B2.x, B2.y);
  18. g.drawLine(A2.x, A2.y, C2.x, C2.y);
  19. g.drawLine(B2.x, B2.y, C2.x, C2.y);
  20.  
  21. //third triangle
  22. Point A3 = new Point((a.x+c.x)/2, (c.y+a.y)/2);
  23. Point B3 = new Point((c.x+b.x)/2, (c.y+a.y)/2);
  24. Point C3 = new Point(c.x, c.y);
  25.  
  26. g.drawLine(A3.x, A3.y, B3.x, B3.y);
  27. g.drawLine(A3.x, A3.y, C3.x, C3.y);
  28. g.drawLine(B3.x, B3.y, C3.x, C3.y);
  29. }

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
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 13
Reputation: jrobw is an unknown quantity at this point 
Solved Threads: 0
jrobw jrobw is offline Offline
Newbie Poster

Re: recursion sierpinskys triangle - HELP!!

 
0
  #7
Dec 8th, 2008
  1. 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:
  1. while(b.x-a.x>5){
  2. ...
  3. }

but it seemed to produce an infinite loop...
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 332
Reputation: quuba is on a distinguished road 
Solved Threads: 53
quuba quuba is offline Offline
Posting Whiz

Re: recursion sierpinskys triangle - HELP!!

 
0
  #8
Dec 8th, 2008
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);
   }
}
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 13
Reputation: jrobw is an unknown quantity at this point 
Solved Threads: 0
jrobw jrobw is offline Offline
Newbie Poster

Re: recursion sierpinskys triangle - HELP!!

 
0
  #9
Dec 8th, 2008
uhh, I do not completely understand your approach..
I implemented the class with your suggestion as follows:

  1. package triangleMod;
  2.  
  3. import java.awt.Color;
  4. import java.awt.Component;
  5. import java.awt.Graphics;
  6. import java.awt.Point;
  7.  
  8. class TriangleQuu extends Component{
  9.  
  10. private static final long serialVersionUID = 1L;
  11.  
  12. //A, B, C initial triangle-points, assigned in the constructor
  13. private Point A, B, C;
  14.  
  15. public TriangleQuu(Point a, Point b, Point c){
  16. this.A = a;
  17. this.B = b;
  18. this.C = c;
  19. }
  20.  
  21. public void drawFractal(int level,Graphics g, Point a, Point b, Point c){
  22. if (level == 1) {
  23. /**
  24. * draw inner reversed triangle
  25. **/
  26. //AB
  27. g.drawLine(a.x, a.y, b.x, b.y);
  28. //AC
  29. g.drawLine(a.x, a.y, c.x, c.y);
  30. //BC
  31. g.drawLine(b.x, b.y, c.x, c.y);
  32. } else {
  33.  
  34. //lower left triangle coordinates
  35. Point A1 = new Point(a.x, a.y);
  36. Point B1 = new Point(c.x, b.y);
  37. Point C1 = new Point((a.x+c.x)/2, (c.y+a.y)/2);
  38.  
  39. //lower right triangle coordinates
  40. Point A2 = new Point(c.x, b.y);
  41. Point B2 = new Point(b.x, b.y);
  42. Point C2 = new Point((c.x+b.x)/2, (c.y+a.y)/2);
  43.  
  44. //upper triangle coordinates
  45. Point A3 = new Point((a.x+c.x)/2, (c.y+a.y)/2);
  46. Point B3 = new Point((c.x+b.x)/2, (c.y+a.y)/2);
  47. Point C3 = new Point(c.x, c.y);
  48.  
  49.  
  50. drawFractal(level-1,g, A1, B1, C1);
  51. drawFractal(level-1,g, A2, B2, C2);
  52. drawFractal(level-1,g, A3, B3, C3);
  53. }
  54. }
  55.  
  56.  
  57. public void paint(Graphics g){
  58. drawFractal(100, g, A, B, C);
  59. }
  60. }

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..
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 332
Reputation: quuba is on a distinguished road 
Solved Threads: 53
quuba quuba is offline Offline
Posting Whiz

Re: recursion sierpinskys triangle - HELP!!

 
0
  #10
Dec 8th, 2008
I invoked with level=8[ & i recived this thumb
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
Point midPoint(Point a,Point b){
//..
return new Point(....);
}
Attached Thumbnails
screen.png  
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the Java Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC