Quick Index
Problem Statement


Add a new class named Frame as described below.

Remember to write a nice "toString" method (for debugging purposes).

The "Notes" in Challenge #1 apply.

Getting Started


Add a public class named Frame.

Add the following private ivars (all type int):


Add the following constructor:
Method Name
Description
Constructor
Add a four-arg constructor that receives method parameters (params) matching the ivars (in order shown above for ivars).


Getter Methods


We define a getter method as simply a method that returns an ivar value (like getX returns x for Point). The getter has no method params.
Getter Method Name
Description
getLeftX
Return ivar leftX
getTopY
Return ivar topY
getRightX
Return ivar rightX
getBottomY
Return ivar bottomY


Calculation Methods


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);


Logical Methods


All of these methods return a boolean.

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


Intersects


Solve this one algrorithmically. I.e., paper and pencil in sketches and/or simpler human language.

Also see Venn Diagram.

Example 1
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.