What is the big problem that must be divided into smaller problems?
The big problem is drawing a long line.
If it is easy to draw a line of ten pixels length, then that is the easy problem that can be done in one step. The big problem is drawing a long line.
Solve the big problem by dividing the line into two halves, and then drawing each half using the same method. If a half is small enough, draw it immediately. Here is a program that does that.
The length of the line is calculated using the endpoints and the Pythagorean Theorem. If the length is short (less than 10, here) draw it immediately (add it to the scene graph). Otherwise, break the line in two, and use the same method to draw each half.
The color parameter is ignored until the line is short enough to be drawn.
import javafx.application.*; import javafx.stage.*; import javafx.scene.*; import javafx.scene.shape.*; import javafx.scene.paint.*; public class DrawLinesRecursive extends Application { void addLine( Group root, Color color, double startX, double startY, double endX, double endY ) { // Calculate the length of the complete line double length = Math.sqrt( (endX-startX)*(endX-startX) + (endY-startY)*(endY-startY)); // If length is small enough, immediately draw the line if ( length < 10.0 ) { Line line = new Line( startX, startY, endX, endY ); line.setStrokeWidth( 3.0 ); line.setStroke( color ); root.getChildren().add( line ); } // Otherwise, draw each half of the line (in different colors) else { double midX = startX + (endX-startX)/2; double midY = startY + (endY-startY)/2; addLine( root, Color.RED, startX, startY, midX, midY ); addLine( root, Color.GREEN, midX, midY, endX, endY ); } } 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( ); addLine( root, Color.BLUE, startX, startY, endX, endY ); Scene scene = new Scene(root, sceneWidth, sceneHeight, Color.BLANCHEDALMOND ); stage.setTitle("Many Connected Lines"); stage.setScene(scene); stage.show(); } }
How many nodes does the scene graph have?