Java Reference
In-Depth Information
16. Write a program that uses recursion to draw a Koch snowflake fractal of any
given order. A Koch snowflake of order 0 is an equilateral triangle. To
create the next-higher-order fractal, each line segment in the shape is
modified by replacing its middle third with a sharp protrusion made of
two line segments, each having the same length as the replaced one, as
shown in Figure 13-11.
( x 1 , y 1 )
( x 1 , y 1 )
( x 2 , y 2 )
( x 3 , y 3 )
( x 4 , y 4 )
( x 5 , y 5 )
( x 5 , y 5 )
FIGURE 13-11 Line segments for Koch snowflakes
The following is the necessary information to compute the three new points
(x 2 , y 2 ), (x 3 , y 3 ), and (x 4 , y 4 ) in terms of (x 1 , y 1 ) and (x 5 , y 5 ).
Let:
deltaX ¼ x 5 x 1
deltaY ¼ y 5 y 1
Then:
x 2 ¼ x 1 þ deltaX = 3 ;
y 2 ¼ y 1 þ deltaY = 3 ;
x 3 ¼ 0 : 5 ð x 1 þ x 5 Þþ 3
p
ð y 1 y 5 Þ= 6 ;
y 3 ¼ 0 : 5 ð y 1 þ y 5 Þþ 3
p
ð x 5 x 1 Þ= 6 ;
x 4 ¼ x 1 þ 2 deltaX = 3 ;
y 4 ¼ y 1 þ 2 deltaY = 3
1
3
 
Search WWH ::




Custom Search