Add the following public instance methods (that do some calcs).
Method Name
Description
getWidth
Return (an int type) that is the width of the frame (right minus left)
getHeight
Return (an int type) that is the height of the frame (bottom minus top). Remember y-axis is oriented downward.
middle
Return a Point object that is the middle of the frame. The calculated middle point should contain the middle x and middle y values.
You can round in Java like this:
//given vars "a" and "b" (both ints)
int z = (int) Math.round(a / (double)b);
Note: Ties default to "inside". E.g. if a point is sitting right on a frame edge, consider it "inside". Similar with "other frame" edges - if matching consider that edge inside. This might also be called an "inclusive" approach
Add the following logical methods.
Method Name
Description
contains
Return true if the method param p (a Point) is inside the frame.
containsX
Return true if the method param x (an int) is inside the frame (ignoring y).
containsY
Return true if the method param y (an int) is inside the frame (ignoring x).
contains
Return true if the method param other (a Frame) is inside this frame.
containsVert
Return true if the method param other (a Frame) is inside this frame considering only y (ignore x).
containsHoriz
Return true if the method param other (a Frame) is inside this frame considering only x (ignore y).
intersects
Return true if the method param other (a Frame) intersects this frame. See Intersects below
Let the four corners of frame A be (a1, a2, a3, a4) and frame B be (b1, b2, b3, b4). Assume corners are clockwise from left-top corner.
We know the first case intersects because a1 is inside B -- one or more points of a frame A inside a frame B would indicate A and B intersect.
Example 2
Let the four corners of frame A be (a1, a2, a3, a4) and frame B be (b1, b2, b3, b4). Assume corners are clockwise from left-top corner.
We know the first case intersects because b1 is inside A -- one or more points of a frame B inside a frame A would indicate A and B intersect.
Example 3
This one is a good challenge.
Let the four corners of frame A be (a1, a2, a3, a4) and frame B be (b1, b2, b3, b4). Assume corners are clockwise from left-top corner.
No points of A are inside B
No points of B are inside A
However we see they intersect. How can we make that determination?
The top of A is above the top of B, AND the bottom of A is below the bottom of B.
So it's possible they intersect.
We now look at the left and right edges of A
Is EITHER the left edge of A or B between the left and right edges of B?
If true, we can conclude we have an intersection
If false, we can conclude that we do not have an intersection
More
There may be more cases. We leave that fun analysis to complete. Hint: use a whiteboard or scratch paper for your analysis.
Introduction To Objects With Java
(Chapter 602 - Logic)