Java Reference
In-Depth Information
A
A
P
Q
B
C
B
R
(b)
C
(a)
A
A
P
P
Q
Q
B
R
(c)
C
B
R
(d)
C
FIGURE 13-9 Sierpinski gaskets of various orders (levels)
Input:
A nonnegative integer indicating the level of the Sierpinski gasket.
Output: A triangle shape displaying a Sierpinski gasket of the given order.
The problem is as described previously. Initially, we specify the coordinates of the
first triangle and then draw the triangle. We use the class Point to store the x-y
coordinates of a point. (The class point is a predefined Java class and is contained
in the package java.awt .) We also use the method drawLine , as described in
Chapter 12, to draw a line between two points.
For each triangle, we need three objects of the class Point to store the vertices of
the triangle, and three more objects to store the midpoints of each side. Because we
frequently need to find the midpoints of a line, we write the method midPoint ,
which returns the coordinates of the midpoint of a line. Its definition is:
PROBLEM
ANALYSIS
AND
ALGORITHM
DESIGN
1
3
Search WWH ::




Custom Search