Write a Rectangle method called intersect: that takes a rectangle as an argument and returns a rectangle representing the overlapping area between the two rectangles. So, for example, given the two rectangles shown in Figure 8.9, the method should return a rectangle whose origin is at (400, 380), whose width is 50, and whose height is 60.
Rectangle.h
rectangle.m
XYPoint.h
XYPoint.m
main.h
Look forward to anyone's input.
Rectangle.h
Code:
#import <Foundation/Foundation.h>
#import "XYPoint.h"
@interface Rectangle : NSObject {
int height;
int width;
XYPoint *origin;
}
@property int height, width;
/* setters */
-(XYPoint *) origin;
-(void) SetOrigin: (XYPoint *) pt;
-(void) setWidth: (int) w andHeight: ( int) h;
/* utility */
-(void) dealloc;
-(int) area;
-(int) perimeter;
-(Rectangle *) intersect: (Rectangle *) myRect;
-(BOOL) rectangleIntersectsWith: (Rectangle *) theRect;
@end
rectangle.m
Code:
#import "Rectangle.h"
@implementation Rectangle
@synthesize width, height;
-(void) SetOrigin: (XYPoint *) pt
{
if (origin)
[origin release];
origin = [[ XYPoint alloc] init] ;
[origin setX: pt.x andY: pt.y];
}
-(XYPoint *) origin
{
return origin;
}
-(int) area
{
return width * height;
}
-(int) perimeter
{
return 2 * ( width + height );
}
-(void) setWidth: (int) w andHeight: ( int) h
{
width = w;
height = h;
}
-(Rectangle *) intersect: (Rectangle *) myRect
{
BOOL overLap;
BOOL argumentISLower = NO;
Rectangle *returnRect = [[Rectangle alloc] init];
XYPoint *retPt = [ [ XYPoint alloc ] init];
overLap = [self rectangleIntersectsWith: myRect];
argumentISLower = origin. y > myRect.origin.y;
if (overLap == NO)
{
returnRect.origin.x = 0;
returnRect.origin.y = 0;
returnRect.width = 0;
returnRect.height = 0;
}
else
{
retPt.x = (myRect.origin.x > origin.x ) ? myRect.origin.x : origin.x;
retPt.y = origin.y;
[returnRect SetOrigin: retPt];
returnRect.height = (argumentISLower == YES) ? myRect.origin.y + myRect.height - origin.y : origin.y \
+ height - myRect.origin.y;
returnRect.width = (myRect.origin.x > origin.x) ? origin.x + width - myRect.origin.x : myRect.origin.x \
+ myRect.width - origin.x;
}
[retPt release];
return returnRect;
}
-(BOOL) rectangleIntersectsWith: (Rectangle *) theRect
{
/* one rectangle is above or below the other */
if ( origin.y > theRect.origin.y + theRect.height || origin.y + height < theRect.origin.y)
return NO;
/* one rectangle is either to the left or right */
if ( origin.x + width < theRect.origin.x || theRect.origin.x + width < origin.x)
return NO;
return YES; /* all other conditions should overlap */
}
-(void) dealloc
{
[origin release];
[super dealloc];
}
@end
XYPoint.h
Code:
#import <Foundation/Foundation.h>
@interface XYPoint : NSObject {
int x;
int y;
}
@property int x, y;
-(void) setX: (int) xVal andY: (int) yVal;
@end
XYPoint.m
Code:
#import "XYPoint.h"
@implementation XYPoint
@synthesize x, y;
-(void) setX: (int) xVal andY: (int) yVal
{
x = xVal;
y = yVal;
}
@end
main.h
Code:
#import "Rectangle.h"
#import "XYPoint.h"
int main (int argc, const char * argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
Rectangle *resRectangle;
Rectangle * rect1 = [[ Rectangle alloc] init];
Rectangle * rect2 = [ [ Rectangle alloc] init];
XYPoint *myPt1 = [ [ XYPoint alloc] init];
XYPoint *myPt2 = [ [ XYPoint alloc] init];
myPt1.x = 200;
rect1.height = 75;
rect1.width = 250;
rect2.width = 100;
rect2.height = 180;
myPt2.x = 150;
myPt2.y = 300;
[rect2 SetOrigin: myPt2 ];
for (myPt1.y = 200; myPt1.y <= 500; myPt1.y ++){
[rect1 SetOrigin: myPt1];
resRectangle = [rect1 intersect: rect2 ];
NSLog(@" New rectangle: {Pt(%3i,%3i)} Width(%-2i), Height(%-3i), Origin(%3i,%3i)", myPt1.x, myPt1.y, resRectangle.width, resRectangle.height,\
resRectangle.origin.x, resRectangle.origin.y);
[resRectangle release];
}
[rect1 release];
[rect2 release];
[myPt1 release];
[myPt2 release];
[pool drain];
return 0;
}
Look forward to anyone's input.