Design Procedure
When writing graphic interface software, we often encounter the relationship between two rectangles.
As shown in [1], the intersection of rectangles refers to the rectangles in the overlapping areas of two rectangles. Of course, they may not exist (see [2 ]). The union of two rectangles refers to the smallest rectangle that can contain the two rectangles, which must exist.
The purpose of this question is: the user enters the coordinates of two rectangles, and the program outputs their intersection and Union rectangles.
The input format of rectangular coordinates is to input two diagonal coordinate points. Note that there is no guarantee on which corner is or the order. (You can drag the mouse to pull the rectangle on the desktop, ).
Input data format:
X1, y1, x2, y2
X1, y1, x2, y2
There are two rows of data. Each row represents a rectangle. Each line is the coordinate of two points. The x coordinate is on the left, and the y coordinate is on the right. Coordinate System: the upper left corner of the screen is (0, 0), the x coordinate increases horizontally to the right, and the y coordinate increases vertically down.
Required program output format:
X1, y1, length, height
X1, y1, length, height
It is also two rows of data, indicating the intersection and Union respectively. If the intersection does not exist, the output "does not exist"
The first two items are the coordinates in the upper left corner. The rear is the length and height of the rectangle.
For example, user input:
100,220,300,100
150,150,300,300
Program output:
150,150,150, 70
100,100,200,200
For example, user input:
10, 10, 20, 20
30, 30, 40, 40
Program output:
Does not exist
10, 10, 30, 30
// Drag two rectangles on the plane with the mouse. // calculate the "intersection" area and "and" area. // drag the information with the mouse and press it with the mouse, the coordinates of the two points are given in import java. util. *; class MyRect {private int left; private int top; private int right; private int bottom; public MyRect () {} public MyRect (int x1, int y1, int x2, int y2) {left = x1
X2? X1: x2; bottom = y1> y2? Y1: y2;} public MyRect getOverlap (MyRect rect) {MyRect t = new MyRect (); t. left = left> rect. left? Left: rect. left; t. top = top> rect. top? Top: rect. top; t. right = right <rect. right? Right: rect. right; t. bottom = bottom <rect. bottom? Bottom: rect. bottom; return t;} public MyRect getUnion (MyRect rect) {MyRect t = new MyRect (); t. left = left <rect. left? Left: rect. left; t. top = top <rect. top? Top: rect. top; t. right = right> rect. right? Right: rect. right; t. bottom = bottom> rect. bottom? Bottom: rect. bottom; return t;} public String toString () {int width = right-left; int height = bottom-top; if (width <= 0 | height <= 0) return "does not exist"; return left + "," + top + "," + width + "," + height ;}} public class MyTest {public static void main (String [] args) {external scan = new external (System. in); String [] ss1 = scan. next (). split (","); String [] ss2 = scan. next (). split (","); MyRect a = new MyRect (Integer. parseInt (ss1 [0]), Integer. parseInt (ss1 [1]), Integer. parseInt (ss1 [2]), Integer. parseInt (ss1 [3]); MyRect B = new MyRect (Integer. parseInt (ss2 [0]), Integer. parseInt (ss2 [1]), Integer. parseInt (ss2 [2]), Integer. parseInt (ss2 [3]); System. out. println (. getOverlap (B); System. out. println (. getUnion (B ));}}