Java Reference
In-Depth Information
80 // Get the midpoint on each edge in the triangle
81 Point2D p12 = p1.midpoint(p2);
82 Point2D p23 = p2.midpoint(p3);
83 Point2D p31 = p3.midpoint(p1);
84
85
// Recursively display three triangles
86
displayTriangles(order - 1 , p1, p12, p31);
top subtriangle
left subtriangle
right subtriangle
87
displayTriangles(order - 1 , p12, p2, p23);
88
displayTriangles(order - 1 , p31, p23, p3);
89 }
90 }
91 }
92 }
The initial triangle has three points set in proportion to the pane size (lines 58-60). If order
== 0 , the displayTriangles(order, p1, p2, p3) method displays a triangle that con-
nects the three points p1 , p2 , and p3 in lines 71-77, as shown in Figure 18.10a. Otherwise, it
performs the following tasks:
displayTriangle method
1. Obtain the midpoint between p1 and p2 (line 81), the midpoint between p2 and p3 (line
82), and the midpoint between p3 and p1 (line 83), as shown in Figure 18.10b.
2. Recursively invoke displayTriangles with a reduced order to display three smaller
Sierpinski triangles (lines 86-88). Note that each small Sierpinski triangle is structurally
identical to the original big Sierpinski triangle except that the order of a small triangle
is one less, as shown in Figure 18.10b.
p1
Draw the Sierpinski triangle
displayTriangles(order, p1, p2, p3)
p2
p3
(a)
Recursively draw the small Sierpinski triangle
displayTriangles(
order - 1, p1, p12, p31)
p1
p12
p31
Recursively draw the small
Sierpinski triangle
displayTriangles(
order - 1, p12, p2, p23)
Recursively draw the
small Sierpinski triangle
displayTriangles(
order - 1, p31, p23, p3)
p2
p3
p23
(b)
F IGURE 18.10
Drawing a Sierpinski triangle spawns calls to draw three small Sierpinski triangles recursively.
A Sierpinski triangle is displayed in a SierpinskiTrianglePane . The order property in
the inner class SierpinskiTrianglePane specifies the order for the Sierpinski triangle.
The Point2D class, introduced in Section 9.8, The Point2D Class, represents a point with
 
 
Search WWH ::




Custom Search