本文实例讲述了C++判断矩形相交的方法。分享给大家供大家参考。具体如下:
已知2矩形原点和宽高,判断2矩形相交,相交矩形
相交判断原理:
假定矩形是用一对点表达的(minx, miny) (maxx, maxy),那么两个矩形
rect1{(minx1, miny1)(maxx1, maxy1)}
rect2{(minx2, miny2)(maxx2, maxy2)}
相交的结果一定是个矩形,构成这个相交矩形rect{(minx, miny) (maxx, maxy)}的点对坐标是:
minx = max(minx1, minx2)
miny = max(miny1, miny2)
maxx = min(maxx1, maxx2)
maxy = min(maxy1, maxy2)
如果两个矩形不相交,那么计算得到的点对坐标必然满足:
( minx > maxx ) 或者 ( miny > maxy )
判定是否相交,以及相交矩形是什么都可以用这个方法一体计算完成
设计3个类:
1. 点类:x,y
2. 矩形类:点,宽,高
3. 判断相交类
程序实现:
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
|
CPoint.h #import <Foundation/Foundation.h> @interface CPoint : NSObject { int x; //点坐标 int y; } -( void ) print; -( void ) setX: ( int ) vx; -( void ) setY: ( int ) vy; -( void ) setXY:( int ) vx :( int ) vy; -( int ) x; -( int ) y; @end CPoint.m #import "CPoint.h" @implementation CPoint -( void ) print { NSLog(@ "the point is (%i, %i)" ,x,y); } -( void ) setX: ( int ) vx { x = vx; } -( void ) setY: ( int ) vy { y = vy; } -( void ) setXY:( int )vx :( int )vy { x = vx; y = vy; } -( int ) x { return x; } -( int ) y { return y; } @end CRect.h #import <Foundation/Foundation.h> #import "CPoint.h" @interface CRect : NSObject { int w; //矩形长 int h; //矩形高 } -( void ) print; -( int ) w; -( int ) h; -( void ) setW: ( int ) vw; -( void ) setH: ( int ) vh; -( void ) setWH: ( int ) vw :( int ) vh; -(CPoint *) origin; -( void ) setOrigin: (CPoint *) pt; @end CRect.m #import "CRect.h" @implementation CRect { CPoint *origin; //点 } -( void ) print { NSLog(@ "the rect:(x:%i, y:%i, w:%i,h:%i)" ,origin.x, origin.y, w, h); } -( int ) w { return w; } -( int ) h { return h; } -( void ) setW:( int )vw { w = vw; } -( void ) setH:( int )vh { h = vh; } -( void ) setWH:( int )vw :( int )vh { w = vw; h = vh; } -(CPoint *) origin { return origin; } -( void ) setOrigin:(CPoint *)pt { origin = pt; } @end DoCRect.h #import <Foundation/Foundation.h> #import "CRect.h" @interface DoCRect : NSObject -( BOOL ) isIntersect:(CRect *) rect1 :(CRect *) rect2; //矩形相交否 -(CRect *) intersectRect: (CRect *) rect1 :(CRect *) rect2; //相交矩形 @end DoCRect.m #import "DoCRect.h" @implementation DoCRect //矩形是否相交 -( BOOL ) isIntersect:(CRect *)rect1 :(CRect *)rect2 { int minx = MAX(rect1.origin.x, rect2.origin.x); int miny = MAX(rect1.origin.y, rect2.origin.y); int maxx = MIN(rect1.origin.x+rect1.w, rect2.origin.x+rect2.w); int maxy = MIN(rect1.origin.y+rect1.h, rect2.origin.y+rect2.h); if (minx>maxx || miny>maxy) { return NO; } return YES; } -(CRect *) intersectRect:(CRect *)rect1 :(CRect *)rect2 { int minx = MAX(rect1.origin.x, rect2.origin.x); int miny = MAX(rect1.origin.y, rect2.origin.y); int maxx = MIN(rect1.origin.x+rect1.w, rect2.origin.x+rect2.w); int maxy = MIN(rect1.origin.y+rect1.h, rect2.origin.y+rect2.h); CRect * rect = [[CRect alloc] init]; CPoint *p = [[CPoint alloc] init]; if (NO == [self isIntersect:rect1 :rect2]) //no isIntersect { [p setXY:minx :miny]; [rect setOrigin:p]; rect.w = 0; rect.h = 0; return rect; } [p setXY:minx :miny]; [rect setOrigin:p]; rect.w = ABS(maxx-minx); rect.h = ABS(maxy - miny); return rect; } @end main.m 测试 #import <Foundation/Foundation.h> #import "DoCRect.h" int main( int argc, const char * argv[]) { @autoreleasepool { NSLog(@ "Hello,判断矩形相交,返回矩形的原点和长高" ); //初始化对象 CRect *myrect1 = [[CRect alloc] init]; CRect *myrect2 = [[CRect alloc] init]; CPoint *p1 = [[CPoint alloc] init]; CPoint *p2 = [[CPoint alloc] init]; DoCRect *dorect = [[DoCRect alloc] init]; //原点变量 [p1 setXY:200 :420]; [p2 setXY:400 :300]; //设置矩形原点 [myrect1 setOrigin:p1]; [myrect1 setWH:250 :75]; [myrect1 print]; [myrect2 setOrigin:p2]; [myrect2 setWH:100 :180]; [myrect2 print]; //判断2矩形是否相交 BOOL insersect = [dorect isIntersect:myrect1 :myrect1]; NSLog(@ " two rect is :%@" ,insersect?@ "YES" :@ "NO" ); //返回相交矩形 //CRect *inserectRect = [[CRect alloc] init]; CRect *inserectRect = [dorect intersectRect:myrect1 :myrect2]; [inserectRect print]; } return 0; } |
希望本文所述对大家的C++程序设计有所帮助。