Java Reference
In-Depth Information
p2
p4
p5
p1
p3
p6
Figure 12.6
Triangle split into subtriangles
We then need to compute three new points that are the midpoints of the three sides
of this triangle as shown in Figure 12.6.
From Figure 12.6, we see that
p4 is the midpoint of p1 and p2
p5 is the midpoint of p2 and p3
p6 is the midpoint of p1 and p3
Once we have computed those points, we can describe the smaller triangles as
follows:
In the lower-left corner is the triangle formed by p1, p4, and p6.
On top is the triangle formed by p4, p2, and p5.
In the lower-right corner is the triangle formed by p6, p5, and p3.
There are three different midpoint computations involved here, so clearly it will be
helpful to first write a method that will compute the midpoint of a segment given two
endpoints. Computing the midpoints involves finding the arithmetic average (halfway
point) of the x values and the y values:
public static Point midpoint(Point p1, Point p2) {
return new Point((p1.x + p2.x) / 2, (p1.y + p2.y) / 2);
}
Given this method, we can easily compute the three midpoints.
The final detail we have to think about is the level. If you look again at the level 2
version of the figure (Figure 12.2), you will notice that it is composed of three simple
 
Search WWH ::




Custom Search