-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathRectManager.java
executable file
·142 lines (125 loc) · 3.52 KB
/
RectManager.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
import java.util.Scanner;
class Rect{
int left; //x1
int top; //y1
int right; //x2
int bottom; //y2
}
public class RectManager {
float area;//Recode area
int nFlag; //recode the relation of two rectangle
public static void main(String[] args) {
Rect A=new Rect();
Rect B=new Rect();
Scanner scan = new Scanner(System.in);
System.out.print("Input rectangle A(Format:left top right bottom):");
A.left = scan.nextInt();
A.top = scan.nextInt();
A.right = scan.nextInt();
A.bottom = scan.nextInt();
System.out.print("Input rectangle B(Format:left top right bottom):");
B.left = scan.nextInt();
B.top = scan.nextInt();
B.right = scan.nextInt();
B.bottom = scan.nextInt();
//Check the input
if (!(A.left>=0 && A.right<640)|| !(A.top>=0 && A.bottom<960)
||!(A.right>=A.left) || !(A.bottom>=A.top)){
System.out.println("Input error in Rectangle A");
return;
}
if (!(B.left>=0 && B.right<640)|| !(B.top>=0 && B.bottom<960)
||!(B.right>=B.left) || !(B.bottom>=B.top)){
System.out.println("Input error in Rectangle B");
return;
}
RectManager test= new RectManager();
test.solve(A, B);
if (test.nFlag==0){
System.out.println("矩形不相交!");
return;
}else if (test.nFlag==1){
System.out.println("矩形相交于一个区域!");
}else if (test.nFlag==2){
System.out.println("矩形相交于一个区域且为包含关系!");
}else if (test.nFlag==3){
System.out.println("矩形相交于一个区域且正好重合!");
}else if (test.nFlag==4){
System.out.println("矩形相交于一个区域且交点为1个点!");
}else if (test.nFlag==5){
System.out.println("矩形相交于一个区域且交点为1条线段!");
}
System.out.println("相交面积:"+test.area);
}
private void solve(Rect A, Rect B){
int nMaxLeft = 0;
int nMaxTop = 0;
int nMinRight = 0;
int nMinBottom = 0;
// Get the max left.
if (A.left >= B.left)
{
nMaxLeft = A.left;
}
else
{
nMaxLeft = B.left;
}
// Get the max top.
if (A.top >= B.top)
{
nMaxTop = A.top;
}
else
{
nMaxTop = B.top;
}
// Get the min right.
if (A.right <= B.right)
{
nMinRight = A.right;
}
else
{
nMinRight = B.right;
}
// Get the min bottom.
if (A.bottom <= B.bottom)
{
nMinBottom = A.bottom;
}
else
{
nMinBottom = B.bottom;
}
// Judge whether intersects.
if ((nMaxLeft > nMinRight) || (nMaxTop > nMinBottom))
{//不相交
nFlag=0;
}
else
{//相交
//B1:相交为一个区域
nFlag = 1;
area = (nMinRight - nMaxLeft + 1 ) * (nMinBottom - nMaxTop + 1);
if ((B.left==A.left) && (B.right==A.right) && (B.top==A.top) && (B.bottom==A.bottom)){
//B13:完全重合
nFlag = 3;
}
else if (((nMaxLeft==A.left) && (nMinRight==A.right) && (nMaxTop==A.top) && (nMinBottom==A.bottom))
||((nMaxLeft==B.left) && (nMinRight==B.right) && (nMaxTop==B.top) && (nMinBottom==B.bottom))){
//B12:包含
nFlag = 2;
}
else if ((nMaxLeft==nMinRight) && (nMaxTop == nMinBottom)){
//B2:交点为1个点
nFlag = 4;
}
else if (((nMaxLeft==nMinRight) && (nMaxTop < nMinBottom))
|| ((nMaxLeft<nMinRight) && (nMaxTop == nMinBottom))){
//B3:交点为1条线段
nFlag = 5;
}
}
}
}