Question: Write a program for testing whether or not two rectangles intersect. The rectangles are placed on the two-dimensional grad. For each rectangle, the program receives

Write a program for testing whether or not two rectangles intersect. The rectangles are placed on the two-dimensional grad. For each rectangle, the program receives the coordinates of the upper-left and the lower-left corners of the triangle from the user. Then the program receives a point from the user and answers whether the point is in the rectangles, if not, the distance of the point from each rectangle.

The main class of the application is given as RecMain.java and you should use it. Your task is to two object classes, Range and Rectangle. The class Range is for storing a range and the Rectangle uses two Range objects as its instance variables to encode a rectangle.

The class Range The class Range has two double instance variables, low and high. These are the lower and the higher ends of the interval represented by the object. The class has the following constructor and methods:

public Range( double l, double h )

public double getLow()

public double getHigh()

public boolean disjoint( Range o )

public boolean isIn( double p )

public double distance( double p )

The role of constructor is simple. It stores the values of the parameters in the two instance variables. If the order is reversed between the two formal parameters (that is, l > h), the constructor use l for high and h for low. The methods getLow and getHigh are getters of the two instance variables. The method disjoint checks whether or not the range represented by the object is disjoint with another Range object, o. The method isIn returns whether or not the value of p is in the range. The method distance returns the distance of p from the range, where the distance is defined as follows:

if is in the range, then the distance is 0;

if is greater than the higher end of the range, return minus the value of the higher end;

if is smaller than the lower end of the range, return the value of the lower end minus .

The Class Rectangle uses a range for x-coordinates and a range for y-coordinates to define a rectangle. The two Range objects are the instance variables of the class. The class has the following constructor and instance methods:

public Rectangle ( double xLow, double yLow, double xHigh, double yHigh )

public Range getXRange()

public Range getYRange()

public boolean disjoint( Rectangle o )

public boolean isIn( double x, double y )

public double distance( double x, double y )

The constructor receives the coordinates for the lower left corner and the upper left corner of the rectangle. The two getters are for retrieving the two ranges. The method disjoint returns whether the object is disjoint with another Rectangle object, o. The method should return true if the object is disjoint with o either with respect to the Range instance variable for the x-coordinates or with respect to the Range instance variable for the y-coordinates. The method isIn returns whether the points specified by x and y is in the rectangle. The method distance returns the distance of the rectangle from the point specified by x. The distance is 0, if the point is in the rectangle. Otherwise, the distance is the square root of the sum of squares of coordinate-wise distance values.

Example of Output

% java RecMain

Enter the two corners of rectangle 1: 1 1 5 5

Enter the two corners of rectangle 2: 3 3 7 7

The rectangles intersect.

Enter x and y: 0 5

The point is not in No.1.

Its distance is 1.000000.

The point is not in No.2.

Its distance is 3.000000.

% java RecMain

Enter the two corners of rectangle 1: 1 1 5 5

Enter the two corners of rectangle 2: 3 3 7 7

The rectangles intersect.

Enter x and y: 4 4

The point is in No.1.

The point is in No.2.

% java RecMain

Enter the two corners of rectangle 1: 1 1 5 5

Enter the two corners of rectangle 2: 10 10 17 16

The rectangles are disjoint.

Enter x and y: 2 3

The point is in No.1.

The point is not in No.2.

Its distance is 10.630146.

Java Script Please

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!