Amazon.com Widgets Work in progress... partial answer to 8.7 (using method of 8.6)
Welcome, Guest. Please login or register.
Did you miss your activation email?
May 21, 2013, 04:15:55 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
|-+  Programming in Objective-C, 4th edition
| |-+  Exercises
| | |-+  Chapter 8
| | | |-+  Work in progress... partial answer to 8.7 (using method of 8.6)
Pages: [1]   Go Down
Print
Author Topic: Work in progress... partial answer to 8.7 (using method of 8.6)  (Read 273 times)
rue
Jr. Member
**
Posts: 53






« on: April 28, 2012, 12:28:12 AM »

So in Rectangle.m, I have these methods.

Code: (Objective-C)
-(BOOL) containsPoint: (XYPoint*) aPoint
{
    // returns TRUE if aPoint is inside this Rectangle
    if ((aPoint.x >= self.origin.x) && (aPoint.x <= self.origin.x + self.width) &&
        (aPoint.y >= self.origin.y) && (aPoint.y <= self.origin.y + self.height))
        return YES;
    else
        return NO;
}

-(BOOL) isIntersect: (Rectangle*) aRect;
{
    // Returns YES if ANY (i.e. one or more) points of aRect is inside Rectangle.self
    // check each corner of aRect if inside Rectangle.self, start at x,y and go counterclockwise
    
    XYPoint *tempPoint;
    tempPoint = [XYPoint new];
    
    // lower left corner
    tempPoint.x = aRect.origin.x;
    tempPoint.y = aRect.origin.y;
    if ([self containsPoint:tempPoint])
        return YES;

    // lower right corner
    tempPoint.x = aRect.origin.x + aRect.width;
    tempPoint.y = aRect.origin.y;
    if ([self containsPoint:tempPoint])
        return YES;
    
    // upper right corner
    tempPoint.x = aRect.origin.x + aRect.width;
    tempPoint.y = aRect.origin.y + aRect.height;
    if ([self containsPoint:tempPoint])
        return YES;
    
    // upper left corner
    tempPoint.x = aRect.origin.x;
    tempPoint.y = aRect.origin.y + aRect.height;
    if ([self containsPoint:tempPoint])
        return YES;

    // the two rectangles don't intersect
    return NO;  
    
}

Right now, the program just returns TRUE if the the (2) rectangles intersect.
I still need to work on returning the region of intersection.

Code: (Objective-C)
#import <Foundation/Foundation.h>
#import "GraphicObject.h"
#import "Rectangle.h"
#import "Circle.h"
#import "Triangle.h"


int main(int argc, const char * argv[])
{

    @autoreleasepool {
        
        XYPoint *rectOrigin;      
        Rectangle *myRect1,*myRect2;
        BOOL    rectangleIntersect;
        
        rectOrigin = [XYPoint new];
        myRect1 = [Rectangle new];
        myRect2 = [Rectangle new];
        
        [rectOrigin setX:200 andY:420];
        [myRect1 setOrigin:rectOrigin];
        [myRect1 setWidth:250 andHeight:75];
        
        [rectOrigin setX:400 andY:300];
        [myRect2 setOrigin:rectOrigin];
        [myRect2 setWidth:100 andHeight:180];
        
        rectangleIntersect = [myRect1 isIntersect:myRect2];
              
        NSLog(@"The Rectangle2 (%g,%g,%g,%g) is %@ Rectangle1 (%g,%g,%g,%g)",myRect2.origin.x,myRect2.origin.y,myRect2.origin.x+myRect2.width, myRect2.origin.y+myRect2.height,rectangleIntersect==YES?@"INTERSECTING":@"DOES NOT INTERSECT",myRect1.origin.x,myRect1.origin.y,myRect1.origin.x+myRect1.width, myRect1.origin.y+myRect1.height);
        
    }
    return 0;
}

Sample run:
The Rectangle2 (400,300,500,480) is INTERSECTING Rectangle1 (200,420,450,495)
Logged
rue
Jr. Member
**
Posts: 53






« Reply #1 on: April 28, 2012, 09:48:53 AM »

Sorry, there's a bug with this logic.  If 2 points of a rectangle are inside the other rectangle, the method may return YES or NO, depending on which one you assign as Rect1 or Rect2.

Code: (Objective-C)
        [rectOrigin setX:10 andY:10];
        [myRect1 setOrigin:rectOrigin];
        [myRect1 setWidth:100 andHeight:100];
        
        [rectOrigin setX:80 andY:20];
        [myRect2 setOrigin:rectOrigin];
        [myRect2 setWidth:200 andHeight:20];
        
        rectangleIntersect = [myRect2 isIntersect:myRect1];
        
        NSLog(@"The Rectangle2 (%g,%g,%g,%g) is %@ Rectangle1 (%g,%g,%g,%g)",myRect2.origin.x,myRect2.origin.y,myRect2.origin.x+myRect2.width, myRect2.origin.y+myRect2.height,rectangleIntersect==YES?@"INTERSECTING":@"DOES NOT INTERSECT",myRect1.origin.x,myRect1.origin.y,myRect1.origin.x+myRect1.width, myRect1.origin.y+myRect1.height);

The Rectangle2 (80,20,280,40) is DOES NOT INTERSECT Rectangle1 (10,10,110,110)


But if you do

Code: (Objective-C)
 rectangleIntersect = [myRect1 isIntersect:myRect2];

The result is INTERSECTING.
        
« Last Edit: April 28, 2012, 09:55:40 AM by rue » Logged
rue
Jr. Member
**
Posts: 53






« Reply #2 on: April 28, 2012, 09:55:18 AM »

Here's the fix... basically, since there are (2) rectangles, I need to check for 4+4 corners = 8 corners total to see if they're inside a rectangle

Code: (Objective-C)
-(BOOL) isIntersect: (Rectangle*) aRect;
{
    // Returns YES if ANY (i.e. one or more) points of aRect is inside Rectangle.self
    // check each corner of aRect if inside Rectangle.self, start at x,y and go counterclockwise
   
    XYPoint *tempPoint;
    tempPoint = [XYPoint new];
   
    // lower left corner
    tempPoint.x = aRect.origin.x;
    tempPoint.y = aRect.origin.y;
    if ([self containsPoint:tempPoint])
        return YES;

    // lower right corner
    tempPoint.x = aRect.origin.x + aRect.width;
    tempPoint.y = aRect.origin.y;
    if ([self containsPoint:tempPoint])
        return YES;
   
    // upper right corner
    tempPoint.x = aRect.origin.x + aRect.width;
    tempPoint.y = aRect.origin.y + aRect.height;
    if ([self containsPoint:tempPoint])
        return YES;
   
    // upper left corner
    tempPoint.x = aRect.origin.x;
    tempPoint.y = aRect.origin.y + aRect.height;
    if ([self containsPoint:tempPoint])
        return YES;
   
   
    // NOW check from the point of view of the other rectangle
    // for cases where the rectangle is inside the other rectangle,
    // or at least 2 points of one rectangle are inside the other rectangle.
   
    // lower left corner
    tempPoint.x = self.origin.x;
    tempPoint.y = self.origin.y;
    if ([aRect containsPoint:tempPoint])
        return YES;
   
    // lower right corner
    tempPoint.x = self.origin.x + self.width;
    tempPoint.y = self.origin.y;
    if ([aRect containsPoint:tempPoint])
        return YES;
   
    // upper right corner
    tempPoint.x = self.origin.x + self.width;
    tempPoint.y = self.origin.y + self.height;
    if ([aRect containsPoint:tempPoint])
        return YES;
   
    // upper left corner
    tempPoint.x = self.origin.x;
    tempPoint.y = self.origin.y + self.height;
    if ([aRect containsPoint:tempPoint])
        return YES;
   

    // the two rectangles don't intersect
    return NO; 
   
}

Still need to figure out how to return the dimension and origin of intersection area.
Logged
rue
Jr. Member
**
Posts: 53






« Reply #3 on: April 29, 2012, 06:12:53 PM »

Final answer to 8.7 here
http://classroomm.com/objective-c/index.php?topic=7106.msg19293#msg19293
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.