程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> 關於C++ >> C++斷定矩形訂交的辦法

C++斷定矩形訂交的辦法

編輯:關於C++

C++斷定矩形訂交的辦法。本站提示廣大學習愛好者:(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++法式設計有所贊助。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved