C++ method to determine the intersection of rectangles

  • 2020-04-02 03:10:54
  • OfStack

This paper illustrates the method of C++ to judge the intersection of rectangles. Share with you for your reference. The details are as follows:

Given the origin and width and height of 2 rectangles, judge that 2 rectangles intersect

Intersection judgment principle:

Suppose the rectangle is represented by a pair of points (minx, miny) (maxx, maxy), then two rectangles
      Rect1 {(minx1 miny1) (maxx1 maxy1)}
      Rect2 {(minx2 miny2) (maxx2 maxy2)}  
The result of intersection must be a rectangle. The point pair coordinate of rect{(minx, miny) (maxx, maxy)} is:  
      minx     =     Max (minx1,     Minx2)  
      miny     =     Max (miny1,     Miny2)  
      maxx     =     Min (maxx1,     Maxx2)  
      maxy     =     Min (maxy1,     Maxy2)  
If the two rectangles do not intersect, then the calculated point pair coordinates must satisfy:  
  (minx   >   Maxx) or (miny   >   Maxy)
You can use this method to determine if the rectangle intersects, and what the intersecting rectangle is. Okay
 
Design 3 classes:

1. Point class: x, y
2. Rectangle: point, width and height
3. Judge the intersection class

Program implementation:


CPoint.h 
#import <Foundation/Foundation.h>
@interface CPoint : NSObject 
{
  int x; //Point coordinates
  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; //Long rectangle
  int h; //High rectangle
}
-(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; //point
}
-(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; //Intersection of rectangles
-(CRect *) intersectRect: (CRect *) rect1 :(CRect *) rect2; //Intersection of the rectangle
@end
DoCRect.m
#import "DoCRect.h"
@implementation DoCRect
//Whether the rectangles intersect or not
-(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  test 
#import <Foundation/Foundation.h>
#import "DoCRect.h"
int main(int argc, const char * argv[])
{
  @autoreleasepool
  {
    NSLog(@"Hello, Determines that the rectangle intersects, and returns the original rectangle point And grow ");
    //Initialize object
    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];
    // The original point variable 
    [p1 setXY:200 :420];
    [p2 setXY:400 :300];
    // Set the rectangle point
    [myrect1 setOrigin:p1];
    [myrect1 setWH:250 :75];
    [myrect1 print];
    [myrect2 setOrigin:p2];
    [myrect2 setWH:100 :180];
    [myrect2 print];
    // judge 2Whether the rectangles intersect or not
    BOOL insersect = [dorect isIntersect:myrect1 :myrect1];
    NSLog(@" two rect is :%@",insersect?@"YES":@"NO");
    // return Intersection of the rectangle
    //CRect *inserectRect = [[CRect alloc] init];
    CRect *inserectRect = [dorect intersectRect:myrect1 :myrect2];
    [inserectRect print];
  }
  return 0;
}

Hope that the article described in the C++ programming to help you.


Related articles: