Happy 2013.
I have a bit of a problem with an intersection method for 2 rectangles, but I tripped with an issue that it won't allow me advance on this topic.
I'm using 4 objects, 2 points and 2 rectangles. Point1 is the origin of rectangle1 and point2 is the origin for rectangle2.
Then I call the intersect method for rectangle1 and send the rectangle2 object to see if there is an intersection or not.
The problem here, is that inside the method, the origin points of both rectangles wind up being the same coordinate, thus, I will not get good information if I do not have different origins.
Here is my .h file:
@interface XYPoint : NSObject @property int x,y; -(void) setX:(int)xVal andY: (int) yVal; @end @interface Rectangle: graphicObject @property float width, height; - (void) setWidth:(float)w andHeight: (float) h; - (void) setOrigin: (XYPoint *) pt; - (bool) containsPoint: (XYPoint *) aPoint; - (void) intersect: (Rectangle *) rect; @end Here is my .m file:
@implementation XYPoint @synthesize x,y; -(void) setX:(int)xVal andY:(int)yVal; { x = xVal; y = yVal; } @end @implementation Rectangle @synthesize width, height; XYPoint *origin; - (void) setWidth:(float) w andHeight: (float) h; { width = w; height = h; } - (float) area { return width * height; } - (float) perimeter { return (2*width) + (2*height); } -(void) setOrigin: (XYPoint *) pt { if (! origin) { origin = [[XYPoint alloc]init]; } origin.x = pt.x; origin.y = pt.y; } - (XYPoint *) origin { return origin; } -(void) intersect: (Rectangle *) rect // { int pointCount; pointCount = 0; /* R1o R2o R1F R2F */ /* origin.x rect.origin.x origin.x+w rect.origin.x+w */ if ( (origin.x <= rect.origin.x) && (rect.origin.x <= (origin.x + width) ) ) pointCount = pointCount +1; NSLog(@"width = %g height = %g origin = (%d,%d)", width, height, origin.x, origin.y); NSLog(@"rect.width = %g rect.height = %g rect.origin (%d,%d)", rect.width, rect.height, rect.origin.x, rect.origin.y); if ( (rect.origin.x <= ( origin.x + width ) ) && (origin.x + width <= rect.origin.x + rect.width) ) pointCount = pointCount + 1; if ( (origin.y <= rect.origin.y) && (rect.origin.y <= (origin.y + height) ) ) pointCount = pointCount + 1; if ( (rect.origin.y <= (origin.y + height) && ( origin.y + height <= rect.origin.y + rect.height)) ) pointCount = pointCount +1; if (pointCount == 4) NSLog (@"the rectangles intersect!"); else NSLog (@"The rectangles don't intersect."); } @end This is my main file:
int main(int argc, const char * argv[]) { @autoreleasepool { Rectangle * myRectangle1 = [[Rectangle alloc] init]; XYPoint * myPoint1 = [[XYPoint alloc]init]; [myPoint1 setX: 0 andY: 0]; [myRectangle1 setWidth: 3 andHeight: 3]; [myRectangle1 setOrigin : myPoint1]; Rectangle * myRectangle2 = [[Rectangle alloc] init]; XYPoint * myPoint2 = [[XYPoint alloc]init]; [myPoint2 setX: 2 andY: 2]; [myRectangle2 setWidth: 4 andHeight: 4]; [myRectangle2 setOrigin : myPoint2]; [myRectangle1 intersect: myRectangle2]; } return 0; } The last time I ran It, i've got this :
width = 3 height = 3 origin = (2,2)
rect.width = 4 rect.height = 4 rect.origin (2,2)
the rectangles intersect!
See? both rectangles have the same origin, even though they were both set different.
Thanks a lot!