Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

mdeh

macrumors 6502
Original poster
Jan 3, 2009
345
2
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

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.
 

TotalLuck

macrumors newbie
Jan 3, 2009
21
0
Moreno Vallley
Here was what i got.

Hey Mdeh,
you went way deeper than i did and now i need to relook at my code. thanks for posting that. I'm posting what i wrote just for kicks and mockery.

Rectangle.m
Code:
-(Rectangle *) intersect: (Rectangle *) bRect
{
	Rectangle *overlapRect = [[ Rectangle alloc] init];
	XYpoint *overlapPT = [[ XYpoint alloc] init];
	
	overlapRect.height = self.origin.y - bRect.origin.y - bRect.height;
	if (overlapRect.height <= 0) 
		overlapRect.height *= -1;
	
	overlapRect.width = self.width - bRect.origin.y;
	if (overlapRect.width <= 0)
		overlapRect.width *= -1;
	
	[overlapPT setX: bRect.origin.x andY:self.origin.y];
	[overlapRect setOrigin: overlapPT];
	
	NSLog(@"The Intersect Rectangle  is  %gw x %gh with origin at %g,%g", overlapRect.width, overlapRect.height, overlapPT.x, overlapPT.y);

	[overlapPT release];
	[overlapRect release];
	return 0;
}

rectangle.h
Code:
@interface Rectangle : GraphicObject
{
	double width;
	double height;
	XYpoint *origin;
}

@property double  width, height;

-(XYpoint *) origin;
-(void) setOrigin: (XYpoint *) pt;
-(double) area;
-(double) perimiter;
-(void) setwidth: (double) w andheight: (double) h;
-(XYpoint *) translate: (XYpoint *) mypt; 
-(void) draw;
-(Rectangle *) intersect: (Rectangle *) bRect;
@end

Main.m
Code:
int main(int argc, char *argv[])
{
	NSAutoreleasePool * pool = [[NSAutoreleasePool alloc]init];
	Rectangle *rectA = [[ Rectangle alloc] init];
	Rectangle *rectB = [[ Rectangle alloc] init];
	XYpoint *rectAPT = [[ XYpoint alloc] init];
	XYpoint *rectBPT = [[ XYpoint alloc] init];

	
	[rectAPT setX:200 andY: 420];
	[rectA setwidth:250 andheight:75];
	[rectA setOrigin: rectAPT];
	
	NSLog(@"Rectangle A  is  %gw x %gh with origin at %g,%g", rectA.width, rectA.height, rectA.origin.x, rectA.origin.y);

	[rectBPT setX:400	andY:300];
	[rectB	setwidth:100 andheight:180 ];
	[rectB	setOrigin: rectBPT];
	
	NSLog(@"Rectangle B  is  %gw x %gh with origin at %g,%g", rectB.width, rectB.height, rectB.origin.x, rectB.origin.y);
	
	[rectA intersect: rectB];
	
	[rectB release];
	[rectA release];
	[rectBPT release];
	[rectAPT release];

	
	return 0;
	[pool release];
	
}

my result:
Rectangle A is 250w x 75h with origin at 200,420
Rectangle B is 100w x 180h with origin at 400,300
The Intersect Rectangle is 50w x 60h with origin at 400,420


***ok I didnt red the part at the bottom where it said to return a 0,0 for origin and 0 for w and h if they dont intersect. DUH
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.