Amazon.com Widgets Exercise 7 intersect method
Welcome, Guest. Please login or register.
Did you miss your activation email?
May 25, 2013, 10:32:24 AM
Home Help Search chat Login Register   
News: Read this please.The Great Kangaroo Escape Looking for reviews of the 4th ed on Amazon!   Twitter:  @skochan
                     

+  Official Forum for Programming in Objective-C (the iPhone Programming Language) - Stephen Kochan
|-+  Old Stuff
| |-+  Answers to Exercises
| | |-+  Chapter 8
| | | |-+  Exercise 7 intersect method
Pages: [1]   Go Down
Print
Author Topic: Exercise 7 intersect method  (Read 605 times)
marks
Newbie
*
Posts: 12






« on: October 10, 2010, 07:47:35 AM »

Hello, this is my take on Exercise 6 intersect method, I'd tired this on different scenarios, but perhaps it still need improvement to cover area. Please feel free to try it, and let me know what is the problem, thanks.

Code: (Objective-C)
-(Rectangle *) intersect: (Rectangle *) rect
{
Rectangle *overlap = [[Rectangle alloc] init];

XYPoint *overlapPoint = [[XYPoint alloc] init];


BOOL xIntersect = NO, yIntersect = NO;

overlap.origin = overlapPoint;

if (self.origin.x + self.width > rect.origin.x || rect.origin.x + rect.width > self.origin.x) {
if (self.origin.x + self.width > rect.origin.x && rect.origin.x + rect.width > self.origin.x && self.origin.x <= rect.origin.x) {
if (self.origin.x <= rect.origin.x){
overlap.origin.x = rect.origin.x;
}
if (self.origin.x + self.width <= rect.origin.x + rect.width){
overlap.width = (self.origin.x + self.width) - overlap.origin.x;
} else if (self.origin.x + self.width > rect.origin.x + rect.width){
overlap.width = (rect.origin.x + rect.width) - overlap.origin.x;
}
xIntersect = YES;
} else if (rect.origin.x + rect.width > self.origin.x && self.origin.x + self.width > rect.origin.x && rect.origin.x <= self.origin.x) {
if (rect.origin.x <= self.origin.x){
overlap.origin.x = self.origin.x;
}
if (rect.origin.x + rect.width <= self.origin.x + self.width){
overlap.width = (rect.origin.x + rect.width) - overlap.origin.x;
} else if (rect.origin.x + rect.width > self.origin.x + self.width){
overlap.width = (self.origin.x + self.width) - overlap.origin.x;
}
xIntersect = YES;
}
} else {
xIntersect = NO;
}
if (self.origin.y + self.height > rect.origin.y || rect.origin.y + rect.height > self.origin.y) {
if (self.origin.y + self.height > rect.origin.y && rect.origin.y + rect.height > self.origin.y && self.origin.y <= rect.origin.y) {
if (self.origin.y <= rect.origin.y){
overlap.origin.y = rect.origin.y;
}
if (rect.origin.y + rect.height <= self.origin.y + self.height){
overlap.height = (rect.origin.y + rect.height) - overlap.origin.y;
} else if (rect.origin.y + rect.height > self.origin.y + self.height){
overlap.height = (self.origin.y + self.height) - overlap.origin.y;
}
yIntersect = YES;
} else if (rect.origin.y + rect.height > self.origin.y && self.origin.y + self.height > rect.origin.y && rect.origin.y <= self.origin.y) {
if (rect.origin.y <= self.origin.y){
overlap.origin.y = self.origin.y;
}
if (self.origin.y + self.height <= rect.origin.y + rect.height){
overlap.height = (self.origin.y + self.height) - overlap.origin.y;
} else if (self.origin.y + self.height > rect.origin.y + rect.height){
overlap.height = (rect.origin.y + rect.height) - overlap.origin.y;
}
yIntersect = YES;
}
} else {
yIntersect = NO;
}

if (!xIntersect || !yIntersect) {
overlap.width = 0;
overlap.height = 0;
overlap.origin.x = 0;
overlap.origin.y = 0;
}

[overlapPoint release];

return overlap;

}
« Last Edit: August 19, 2011, 10:37:34 AM by fujilla » Logged
Pages: [1]   Go Up
Print
Jump to:  



Login with username, password and session length

Powered by MySQL Powered by PHP Powered by SMF 1.1.11 | SMF © 2006-2009, Simple Machines LLC Valid XHTML 1.0! Valid CSS!
Entire forum contents (c) 2009 classroomM.com. All rights reserved.