Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Recursion Triangle with Deviation

Hello I am fairly new to programming and I am trying, in Java, to create a function that creates recursive triangles from a larger triangles midpoints between corners where the new triangles points are deviated from the normal position in y-value. See the pictures below for a visualization.

Visualization - Image 1

Visualization - Image 2

The first picture shows the progression of the recursive algorithm without any deviation (order 0,1,2) and the second picture shows it with(order 0,1).

I have managed to produce a working piece of code that creates just what I want for the first couple of orders but when we reach order 2 and above I run into the problem where the smaller triangles don't use the same midpoints and therefore looks like the picture below.

work in progress

So I need help with a way to store and call the correct midpoints for each of the triangles. I have been thinking of implementing a new class that controls the calculation of the midpoints and stores them and etc, but as I have said I need help with this.

Below is my current code

The point class stores a x and y value for a point

lineBetween creates a line between the the selected points

void fractalLine(TurtleGraphics turtle, int order, Point ett, Point tva, Point tre, int dev) {


    if(order == 0){
        lineBetween(ett,tva,turtle);
        lineBetween(tva,tre,turtle);
        lineBetween(tre,ett,turtle);
    } else {

            double deltaX = tva.getX() - ett.getX();
            double deltaY = tva.getY() - ett.getY();

            double deltaXtre = tre.getX() - ett.getX();
            double deltaYtre = tre.getY() - ett.getY();

            double deltaXtva = tva.getX() - tre.getX();
            double deltaYtva = tva.getY() - tre.getY();

            Point one;
            Point two;
            Point three;

            double xt = ((deltaX/2))+ett.getX();
            double yt = ((deltaY/2))+ett.getY() +RandomUtilities.randFunc(dev);
            one = new Point(xt,yt);

            xt = (deltaXtre/2)+ett.getX();
            yt = (deltaYtre/2)+ett.getY() +RandomUtilities.randFunc(dev);
            two = new Point(xt,yt);

            xt = ((deltaXtva/2))+tre.getX();
            yt = ((deltaYtva/2))+tre.getY() +RandomUtilities.randFunc(dev);
            three = new Point(xt,yt);

            fractalLine(turtle,order-1,one,tva,three,dev/2);
            fractalLine(turtle,order-1,ett,one,two,dev/2);
            fractalLine(turtle,order-1,two,three,tre,dev/2);
            fractalLine(turtle,order-1,one,two,three,dev/2);            
    }
}

Thanks in Advance

Victor

like image 873
Verzus Avatar asked Nov 05 '12 23:11

Verzus


1 Answers

You can define a triangle by 3 points(vertexes). So the vertexes a, b, and c will form a triangle. The combinations ab,ac and bc will be the edges. So the algorithm goes:

  1. First start with the three vertexes a,b and c
  2. Get the midpoints of the 3 edges p1,p2 and p3 and get the 4 sets of vertexes for the 4 smaller triangles. i.e. (a,p1,p2),(b,p1,p3),(c,p2,p3) and (p1,p2,p3)
  3. Recursively find the sub-triangles of the 4 triangles till the depth is reached.

So as a rough guide, the code goes

findTriangles(Vertexes[] triangle, int currentDepth) {
    //Depth is reached.
    if(currentDepth == depth) {
          store(triangle);
          return;
    }
    Vertexes[] first = getFirstTriangle(triangle); 
    Vertexes[] second = getSecondTriangle(triangle);
    Vertexes[] third = getThirdTriangle(triangle);;
    Vertexes[] fourth = getFourthTriangle(triangle)

    findTriangles(first, currentDepth+1);  
    findTriangles(second, currentDepth+1);
    findTriangles(third, currentDepth+1);
    findTriangles(fourth, currentDepth+1);
}     

You have to store the relevant triangles in a Data structure.

like image 60
Jeewantha Avatar answered Oct 13 '22 17:10

Jeewantha