go to previous page   go to home page   go to next page

Answer:

Yes.


Depth Limit

Recursive Triangles

The above is done recursively. But somehow the recursion has to end. One way to do this is with a counter that is incremented for each recursive call. When the counter reaches a limit, draw solid triangles and end the recursion. Here is the idea:


class STriangle extends Group
{
  STriangle( Color color, int order, int limit, 
    double x0, double y0, double x1, double y1, double x2, double y2 )
  {
     
    // Calculate midpoints of the three sides
     
    // If the depth limit has not been reached,
    // draw the current triangle as three hollow little triangles
    // recursively using this very method.
    if ( order < limit )
    {
      order++ ;
                // call STriangle for each of three little triangles 
    }
    
    // If the depth limit has been reached,
    // draw the current triangle as three solid little triangles
    else
    {
                // draw three solid triangles 
    }
  
  }
}


QUESTION 6:

Here is another possibility:

End the recursion (by drawing solid triangles) when the difference between x0 and x2 is smaller than some limit.

Would this work?


go to previous page   go to home page   go to next page