Java Recursion Triangle with Deviation
Asked Answered
U

2

9

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

Untwine answered 5/11, 2012 at 23:24 Comment(3)
Have Edge and Vertex classes so you can split edges and have triangles refer to shared vertices, etc.Galegalea
Why do you need Random numbers? What is the purpose of dev? Please don't answer here, edit your original post. ThanksChurch
I heartily recommend you create a getMidpoint(Point a, Point b, double deviation) method (or similiar), which should help simplify this somewhat. Also, why is your deviation only in the y direction? I don't know for sure, but I suspect that your problem may be due to not keeping points in consistent order when passing to the next level.Dunsinane
H
1

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.

Hamamelidaceous answered 20/11, 2012 at 11:38 Comment(0)
C
0

You compute the midpoints of any vertex again and again in the different paths of your recursion. As long as you do not change them by random, you get the same midpoint for every path so there's no problem. But of course, if you modify the midpoints by random, you'll end with two different midpoints in two different paths of recursion.

You could modify your algorithm in a way that you not only pass the 3 corners of the triangle along, but also the modified midpoints of each vertex. Or you keep them in a separate list or map or something and only compute them one time and look them up otherwise.

Chiquita answered 6/11, 2012 at 14:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.