Changing the code to what is below will create the bumpy line out of smaller bumpy lines, and each of the smaller bumpy lines out of yet smaller bumpy lines, recursively without limit.
The problem is that there is no limit, and the recursion will never end.
// Construct this line out of four smaller lines KLine lineA = new KLine( x0, y0, x1, y1 ); KLine lineB = new KLine( x1, y1, x2, y2 ); KLine lineC = new KLine( x2, y2, x3, y3 ); KLine lineD = new KLine( x3, y3, x4, y4 ); getChildren().addAll( lineA, lineB, lineC, lineD );
We need a way to end the recursion. One way is to draw a straight line when the distance is small. Here is the plan:
// Construct a Koch line between (x0, y0) and (x4, y4) public KLine( double x0, double y0, double x4, double y4 ) { ...... dist = Math.sqrt( (x4-x0)*(x4-x0) + (y4-y0)*(y4-y0) ); if ( dist < LINELIMIT ) { Line line = new Line( x0, y0, x4, y4 ); getChildren().addAll( line ); } else { // Construct this line out of four smaller lines ....... KLine lineA = new KLine( x0, y0, x1, y1 ); KLine lineB = new KLine( x1, y1, x2, y2 ); KLine lineC = new KLine( x2, y2, x3, y3 ); KLine lineD = new KLine( x3, y3, x4, y4 ); getChildren().addAll( lineA, lineB, lineC, lineD ); } }
Another way to end the recursion would be to use a depth counter, as was done with the Sierpinski Triangle.
If you think of the line as drawn from (x0,y0)
to (x4,y4)
,
which side of the line has the bump?