19.09.2015 Views

Prentice.Hall.Introduction.to.Java.Programming,.Brief.Version.9th.(2014).[sharethefiles.com]

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

756 Chapter 20 Recursion<br />

20.20<br />

✓Point✓ How do you obtain the midpoint between two points?<br />

20.21 What is the base case for the displayTriangles method?<br />

48 displayTriangles(g, order, p1, p2, p3);<br />

49 }<br />

50<br />

51<br />

52<br />

53<br />

private static void displayTriangles(Graphics g, int order,<br />

Point p1, Point p2, Point p3) {<br />

if (order == 0) {<br />

54 // Draw a triangle <strong>to</strong> connect three points<br />

draw a triangle<br />

55 g.drawLine(p1.x, p1.y, p2.x, p2.y);<br />

56 g.drawLine(p1.x, p1.y, p3.x, p3.y);<br />

57 g.drawLine(p2.x, p2.y, p3.x, p3.y);<br />

58 }<br />

59 else {<br />

60 // Get the midpoint on each edge of the triangle<br />

61 Point p12 = midpoint(p1, p2);<br />

62 Point p23 = midpoint(p2, p3);<br />

63 Point p31 = midpoint(p3, p1);<br />

64<br />

65 // Recursively display three triangles<br />

<strong>to</strong>p subtriangle<br />

left subtriangle<br />

right subtriangle<br />

66<br />

67<br />

68<br />

69<br />

displayTriangles(g, order - 1, p1, p12, p31);<br />

displayTriangles(g, order - 1, p12, p2, p23);<br />

displayTriangles(g, order - 1, p31, p23, p3);<br />

}<br />

70 }<br />

71<br />

72 private static Point midpoint(Point p1, Point p2) {<br />

73 return new Point((p1.x + p2.x) / 2, (p1.y + p2.y) / 2);<br />

74 }<br />

75 }<br />

main method omitted 76 }<br />

The initial triangle has three points set in proportion <strong>to</strong> the panel size (lines 44–46). If<br />

displayTriangle method order == 0, the displayTriangles(g, order, p1, p2, p3) method displays a<br />

triangle that connects the three points p1, p2, and p3 in lines 55–57, as shown in<br />

Figure 20.10a. Otherwise, it performs the following tasks:<br />

1. Obtain the midpoint between p1 and p2 (line 61), the midpoint between p2 and p3 (line<br />

62), and the midpoint between p3 and p1 (line 63), as shown in Figure 20.10b.<br />

2. Recursively invoke displayTriangles with a reduced order <strong>to</strong> display three smaller<br />

Sierpinski triangles (lines 66–68). Note that each small Sierpinski triangle is structurally<br />

identical <strong>to</strong> the original big Sierpinski triangle except that the order of a small<br />

triangle is one less, as shown in Figure 20.10b.<br />

A Sierpinski triangle is displayed in a SierpinskiTrianglePanel. The order property in<br />

the inner class SierpinskiTrianglePanel specifies the order for the Sierpinski triangle.<br />

The Point class, introduced in Section 16.8, Mouse Events, represents a point on a <strong>com</strong>ponent.<br />

The midpoint(Point p1, Point p2) method returns the midpoint between p1 and<br />

p2 (lines 72–74).<br />

20.22 How many times is the displayTriangles method invoked for a Sierpinski triangle<br />

of order 0, order 1, order 2, and order n?

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!