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

Answer:

Yes.


Recursive STriangle

One Triangle, made of Three Smaller

Here is an almost complete STriangle. The recursive calls are not quite finished.

  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
    double xL = (x0+x1)/2.0; double yL = (y0+y1)/2.0;
    double xR = (x1+x2)/2.0; double yR = (y1+y2)/2.0;
    double xB = (x0+x2)/2.0; double yB = (y0+y2)/2.0;
    
    // If the depth limit has not been reached,
    // construct the current triangle as three hollow little triangles
    // recursively using this very method.
    if ( order < limit )
    {
      order++ ;
      STriangle triL = new STriangle( color, order, limit,  ); // lower left
      
      STriangle triR = new STriangle( color, order, limit,  ); // lower right
      
      STriangle triT = new STriangle( color, order, limit,  ); // top
      
      getChildren().addAll( triL, triR, triT );  
    }
    
    // If the depth limit has been reached,
    // construct the current triangle as three solid little triangles
    else
    {
      Polygon triL = new Polygon( x0, y0, xL, yL, xB, yB ); // lower left
      triL.setFill( color );
      Polygon triR = new Polygon( xB, yB, xR, yR, x2, y2 ); // lower right
      triR.setFill( color );
      Polygon triT = new Polygon( xL, yL, x1, y1, xR, yR ); // top
      triT.setFill( color );
      getChildren().addAll( triL, triR, triT );  
    }
  
  }
}   

The variable TriL in the true-branch of the if-statement is a different variable than TriL in the false-branch because of Java scoping rules. Recall that the scope of a variable is the section of code in which it may be used. The scope of a variable declared in a block from where it was declared to the end of the block, only.


QUESTION 7:

Fill in the missing parameters.


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