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

Answer:

Of course.


Two Lines

Two connected lines diagonally across the scene, one half red, the other half blue

The two lines connect at the midpoint to form one line. The midpoint is calculated by

double midX = startX + (endX-startX)/2;
double midY = startY + (endY-startY)/2;

There is nothing recursive about it yet. This will come next.

import javafx.application.*;  
import javafx.stage.*;        
import javafx.scene.*;  
import javafx.scene.shape.*; 
import javafx.scene.paint.*;  
 
public class DrawTwoLines extends Application
{ 
  
  void addLine( Group root, Color color, double startX, double startY, double endX, double endY )
  { 
    Line line = new Line( startX, startY, endX, endY );
    line.setStrokeWidth( 3.0 );
    line.setStroke( color );
    root.getChildren().add( line );  
  }
  
  public void start(Stage stage) 
  { 
    double sceneWidth=400, sceneHeight= 300;  
    double startX = 20.0, startY= 250.0, endX = 380.0, endY = 50.0;
    Group  root = new Group( );   
    
    double midX = startX + (endX-startX)/2;
    double midY = startY + (endY-startY)/2;
    
    addLine( root, Color.RED, startX, startY, midX, midY );
    addLine( root, Color.BLUE, midX, midY, endX, endY );
    
    Scene scene = new Scene(root, sceneWidth, sceneHeight, Color.BLANCHEDALMOND ); 
    stage.setTitle("Two Connected Lines"); 
    stage.setScene(scene); 
    stage.show(); 
  }      

} 

QUESTION 11:

(You knew this was coming: )

Here are (again) the two parts to recursion:

  1. If the problem is easy, solve it immediately.
  2. If the problem can't be solved immediately, divide it into easier problems, then:
    • Solve the easier problems using this method.

In terms of drawing a line, what are the two parts? Think in terms of the length of the line.

Say that it is easy to draw a line of 10 pixels length.

What is the big problem that must be divided into smaller problems?



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