Java Reference
In-Depth Information
A method
contains(Triangle2D t)
that returns
true
if the specified
triangle is inside this triangle (see Figure 10.22b).
■
A method
overlaps(Triangle2D t)
that returns
true
if the specified
triangle overlaps with this triangle (see Figure 10.22c).
■
p
(a)
(b)
(c)
F
IGURE
10.22
(a) A point is inside the triangle. (b) A triangle is inside another triangle.
(c) A triangle overlaps another triangle.
Draw the UML diagram for the class and then implement the class. Write
a test program that creates a
Triangle2D
objects
t1
using the constructor
new Triangle2D(new MyPoint(2.5, 2), new MyPoint(4.2, 3),
new MyPoint(5, 3.5))
, displays its area and perimeter, and displays the
result of
t1.contains(3, 3)
,
r1.contains(new Triangle2D(new
MyPoint(2.9, 2), new MyPoint(4, 1), MyPoint(1, 3.4)))
, and
t1.
overlaps(new Triangle2D(new MyPoint(2, 5.5), new MyPoint(4,
-3), MyPoint(2, 6.5)))
.
(
Hint
: For the formula to compute the area of a triangle, see Programming Exer-
cise 2.19. To detect whether a point is inside a triangle, draw three dashed lines,
as shown in Figure 10.23. If the point is inside a triangle, each dashed line
should intersect a side only once. If a dashed line intersects a side twice, then
the point must be outside the triangle. For the algorithm of finding the intersect-
ing point of two lines, see Programming Exercise 3.25.)
p
p
(a)
(b)
F
IGURE
10.23
(a) A point is inside the triangle. (b) A point is outside the triangle.
*10.13
(
Geometry: the
MyRectangle2D
class
) Define the
MyRectangle2D
class that
contains:
Two
double
data fields named
x
and
y
that specify the center of the rec-
tangle with getter and setter methods. (Assume that the rectangle sides are
parallel to
x-
or
y-
axes.)
■
The data fields
width
and
height
with getter and setter methods.
■
A no-arg constructor that creates a default rectangle with (
0
,
0
) for (
x
,
y
) and
1
for both
width
and
height
.
■
A constructor that creates a rectangle with the specified
x
,
y
,
width
, and
height
.
■
Search WWH ::
Custom Search