- Notifications
You must be signed in to change notification settings - Fork 37
Expand file tree
/
Copy pathRectManager.java
More file actions
Latest commit
executable file
·142 lines (125 loc) · 3.52 KB
/
Copy pathRectManager.java
File metadata and controls
executable file
·142 lines (125 loc) · 3.52 KB
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
importjava.util.Scanner;
classRect{
intleft; //x1
inttop; //y1
intright; //x2
intbottom; //y2
}
publicclassRectManager {
floatarea;//Recode area
intnFlag; //recode the relation of two rectangle
publicstaticvoidmain(String[] args) {
RectA=newRect();
RectB=newRect();
Scannerscan = newScanner(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;
}
RectManagertest= newRectManager();
test.solve(A, B);
if (test.nFlag==0){
System.out.println("矩形不相交!");
return;
}elseif (test.nFlag==1){
System.out.println("矩形相交于一个区域!");
}elseif (test.nFlag==2){
System.out.println("矩形相交于一个区域且为包含关系!");
}elseif (test.nFlag==3){
System.out.println("矩形相交于一个区域且正好重合!");
}elseif (test.nFlag==4){
System.out.println("矩形相交于一个区域且交点为1个点!");
}elseif (test.nFlag==5){
System.out.println("矩形相交于一个区域且交点为1条线段!");
}
System.out.println("相交面积:"+test.area);
}
privatevoidsolve(RectA, RectB){
intnMaxLeft = 0;
intnMaxTop = 0;
intnMinRight = 0;
intnMinBottom = 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;
}
elseif (((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;
}
elseif ((nMaxLeft==nMinRight) && (nMaxTop == nMinBottom)){
//B2:交点为1个点
nFlag = 4;
}
elseif (((nMaxLeft==nMinRight) && (nMaxTop < nMinBottom))
|| ((nMaxLeft<nMinRight) && (nMaxTop == nMinBottom))){
//B3:交点为1条线段
nFlag = 5;
}
}
}
}