Amazon.com Widgets Exercise 8.7 - solution
Welcome, Guest. Please login or register.
Did you miss your activation email?
October 21, 2014, 02:34:38 PM
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
| | | |-+ Exercise 8.7 - solution
Pages: [1] Go Down
Print
Author Topic: Exercise 8.7 - solution (Read 824 times)
john67
Newbie
*
Posts: 32






on: February 04, 2012, 04:11:28 PM

I have a love and hate relationship with this question, attempted in 2nd edition and didn't quite get my algorithm to work for all cases, however after another attempt on this latest edition - this is a solution that works every time and will provide a zero result for no overlap.

How does it work? Essentially for each axis it looks for an overlap between the 2 rectangles, once it has found commonality (i == n), it sets the origin and starts counting the height (or width). It could easily be extended to 3 or more rectangles - just keep nesting the 'for' loops.

Code: (Objective-C)
-(Rectangle *) intersect: (Rectangle *) r2  
{    
    int x1 = self.origin.x;  
    int y1 = self.origin.y;  
    int h1 = self.height;  
    int w1 = self.width;  
      
    int x2 = r2.origin.x;  
    int y2 = r2.origin.y;  
    int h2 = r2.height;  
    int w2 = r2.width;    
      
    int x3 = 0, y3 = 0, h3 = 0, w3 = 0;  
      
    BOOL boolY = true, boolX = true;  
      
    // finding y axis data  
    for (int i = y1; i < (y1 + h1); i++)  
    {  
        for (int n = y2; n < (y2 + h2); n++)  
        {  
            if (i == n)  
            {  
                h3++;  
                if (boolY) {  
                    y3 = i;  
                    boolY = false;  
                }  
            }  
        }  
    }  
      
    NSLog(@"h3 is %i", h3);  
    NSLog(@"y3 is %i", y3);  
      
    // finding x axis data  
    for (int i = x1; i < (x1 + w1); i++)  
    {  
        for (int n = x2; n < (x2 + w2); n++)  
        {  
            if (i == n)  
            {  
                w3++;  
                if (boolX) {  
                    x3 = i;  
                    boolX = false;  
                }  
            }  
        }  
    }  
      
    NSLog(@"w3 is %i", w3);  
    NSLog(@"x3 is %i", x3);  
      
    Rectangle *returnRect = [[Rectangle alloc]init];  
    XYPoint *returnPoint = [[XYPoint alloc] init];  
    [returnPoint setX:x3 andY:y3];  
    returnRect.origin = returnPoint;  
    [returnRect setWidth:w3 andHeight:h3];  
      
    return returnRect;  
      
}  
Logged
Hesadanza
Newbie
*
Posts: 28






Reply #1 on: February 05, 2012, 10:44:34 AM

Does this method work only for integer-based rectangle class, or does it also work if you've converted the class to floating-point coordinates and dimensions. (I think converting the class to floating points is one of the previous exercises.)
Logged
Sebastiaan76
Newbie
*
Posts: 7


Email




Reply #2 on: February 08, 2012, 03:13:33 PM

John,
This is a nice way to solve the problem without manually covering off many possible scenarios & therefore generating 100+ lines of code.

Whilst I get the general gist of what is happening in the outer loops, I'm a little confused about the inner loops and what happens when it finds commonality.

I know from this point it starts to increment the height, but I cant seem to get my head around what is happening with the 'BoolX' variable/condition.

The way I see it, is that it would just increment height by 1, and then stop ( as BoolY is true already ), meaning that Height will just stay at '1'.

I know this probably isn't the case, but perhaps you ( or someone who gets it ) could walk me through the mechanics of the loops - especially the inner ones after i == n.



Code: (Objective-C)
     
            if (i == n)   
            {   
                h3++;   
                if (boolY) {   
                    y3 = i;   
                    boolY = false;   
                }   
            }   
           
Logged
Eloise
Newbie
*
Posts: 8






Reply #3 on: March 14, 2012, 10:26:24 AM

I like this solution for integer values, but it will fail for non-integers sadly.

Sebastiaan, you've got the wrong end of the stick I'm afraid. What the if(boolY) section is doing is putting the value of the intersection point (on the Y-axis in this case obviously) into a variable - y3 - and then saying don't check any more. This gives you the y-value for the origin of the intersection rectangle. Keeping on counting (h3++ is outside the if scope remember) means you get a value for the height of the rectangle - it keeps counting while the point is inside both rectangles.

If you look at the example in the question, you start at y1=420, y2=300. You loop through y2=300, 301, 302... 420 and you hit i==n is TRUE.

So, h3++ gets triggered, boolY is true, so y3=420, boolY set to FALSE. Next time it triggers y1=y2=421. h3 increments, but boolY is already false so we don't update y3.

Hope that helps.
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.