//Rect.java
/**
* This
class represents a rectangle. Its fields
represent the coordinates
* of
the corners of the rectangle. Its
methods define operations that can
* be
performed on Rect objects.
**/
public class Rect {
//
These are the data fields of the class
public int x1, y1, x2, y2;
/**
*
The is the main constructor for the class.
It simply uses its arguments
*
to initialize each of the fields of the new object. Note that it has
*
the same name as the class, and that it has no return value declared in
*
its signature.
**/
public Rect(int x1, int y1, int x2, int y2) {
this.x1 = x1;
this.y1 = y1;
this.x2 = x2;
this.y2 = y2;
}
/**
*
This is another constructor. It takes width and
height (in this case, (0,0) will be the bottom-left corner.)
**/
public Rect(int width, int height) {
this(0, 0, width, height);
}
/**
This is yet another constructor. */
public Rect( ) { this(0, 0, 0, 0); }
/** Move the rectangle by the specified
amounts */
public void move(int deltax, int deltay) {
x1 += deltax; x2 += deltax;
y1 += deltay; y2 += deltay;
}
/**
Test whether the specified point is inside the rectangle */
public boolean isInside(int x, int y) {
return ((x >= x1)&& (x <= x2)&& (y >=
y1)&& (y <= y2));
}
/**
*
Return the union of this rectangle with another. I.e. return the
*
smallest rectangle that includes them both.
**/
public Rect union(Rect r) {
return new Rect((this.x1 < r.x1) ? this.x1 : r.x1,
(this.y1 < r.y1) ?
this.y1 : r.y1,
(this.x2 > r.x2) ?
this.x2 : r.x2,
(this.y2 > r.y2) ?
this.y2 : r.y2);
}
/**
*
Return the intersection of this rectangle with another.
*
I.e. return their overlap.
**/
public Rect intersection(Rect r) {
Rect result = new Rect((this.x1
> r.x1) ? this.x1 : r.x1,
(this.y1 >
r.y1) ? this.y1 : r.y1,
(this.x2 <
r.x2) ? this.x2 : r.x2,
(this.y2 <
r.y2) ? this.y2 : r.y2);
if (result.x1 > result.x2) { result.x1 = result.x2 = 0; }
if (result.y1 > result.y2) { result.y1 = result.y2 = 0; }
return result;
}
/**
*
This is a method of our superclass, Object.
We override it so that
*
Rect objects can be meaningfully converted to strings, can be
*
concatenated to strings with the + operator, and can be passed to
*
methods like System.out.println( )
**/
public String toString( ) {
return "[" + x1 + "," + y1 + "; " + x2 +
"," + y2 + "]";
}
}
// RectTest.java
/** This class demonstrates how you might use
the Rect class */
public class RectTest {
public static void main(String[ ]
args) {
Rect rect =
new Rect(1, 3, 6, 8); // Create Rect objects
int
choice = Integer.parseInt(args[0]);
if
(choice == 1) {
int
delX;
int
delY;
//
move the rectangle rect by delx and dely
delX
= Integer.parseInt(args[0]);
delY
= Integer.parseInt(args[1]);
rect.move(delX,
delY);
System.out.println(rect);
}
else if (choice == 2) {
int
x;
int
y;
//
check if x , y lies inside the rectangle rect
x
= Integer.parseInt(args[1]);
y
= Integer.parseInt(args[2]);
if(rect.isInside(x,
y)){
System.out.println("Point
"+x+","+y+" Is Inside");
}else{
System.out.println("Point
"+x+","+y+" Is Not Inside");
}
}
else if (choice == 3) {
//
create a rectangle object named rect3 and find its union with rect1
int
x1;
int
y1;
int
x2;
int
y2;
x1
= Integer.parseInt(args[1]);
y1
= Integer.parseInt(args[2]);
x2
= Integer.parseInt(args[3]);
y2 = Integer.parseInt(args[4]);
Rect
rect2 = new Rect(x1, y1, x2, y2);
Rect
rect3 = rect.union(rect2);
System.out.println(rect3);
}
else if (choice == 4) {
//
create a rectangle object named rect3 with height 5, and width 6 and
// find its intersection
with rect
int
width;
int
height;
width
= Integer.parseInt(args[1]);
height
= Integer.parseInt(args[2]);
Rect
rect2 = new Rect(width, height);
Rect
rect3 = rect.intersection(rect2);
System.out.println(rect3);
}
else {
System.out.println("Invalid
choice");
}
}
}
No comments:
Post a Comment