Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   Java (http://www.programmingforums.org/forum17.html)
-   -   seirpinski's triangle help (http://www.programmingforums.org/showthread.php?t=11899)

dark_omen Nov 15th, 2006 8:45 PM

seirpinski's triangle help
 
OK so I have to make sierpinski's triangle recursively. I have most of it done, but for some reason it just shows the top and right triangle subdivisions.
Here is what I have:
:

public void Serpinski (int depth, int level, int x[], int y[], int num_points){
                  if(depth == level){//base case: if the depth equals the level then draw the triangle
                          g_parent.fillPolygon(x, y, num_points);
                          g_parent.setColor(Color.GREEN);
                  }
                  else{//recursive case
                          depth++;
                          //compute center of the sides the sides
                          //centers of x co-ordinates
                          int x0 = (x[0] + x[1])/2;
                          int x1 = (x[0] + x[2])/2;
                          int x2 = (x[1] + x[2])/2;
                          //centers of y co-ordinates
                          int y0 = (y[0] + y[1])/2;
                          int y1 = (y[0] + y[2])/2;
                          int y2 = (y[1] + y[2])/2;
                      //then create new x and y arrays with new points, and solve recursively
                          //need three recursive calls because you then divide the triangle into three more triangles
                          int[] newX0 = {x[0], x0, x1};
                          int[] newY0 = {y[0], y0, y1};
                          Serpinski(depth, level, newX0, newY0, num_points);
                         
                          int[] newX1 = {x[1], x0, x2};
                          int[] newY1 = {y[1], y0, y2};
                          Serpinski(depth, level, newX1, newY1, num_points);
                         
                          int[] newX2 = {x[2], x1, x2};
                          int[] newY2 = {y[2], y1, y2};
                          Serpinski(depth, level, newX2, newY2, num_points);
                  }
          }



All times are GMT -5. The time now is 5:14 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC