这篇文章主要介绍了C++判断矩形相交的方法,涉及C++针对平面坐标数学运算的相关技巧,具有一定参考借鉴价值,需要的朋友可以参考下
本文实例讲述了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. 判断相交类
程序实现:
- 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++程序设计有所帮助。
新闻热点
疑难解答