Amazon.com Widgets yet another solution for 8.7
Welcome, Guest. Please login or register.
Did you miss your activation email?
July 28, 2014, 09:36:40 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
| | | |-+ yet another solution for 8.7
Pages: [1] Go Down
Print
Author Topic: yet another solution for 8.7 (Read 771 times)
electricb
Newbie
*
Posts: 6


Email




on: April 18, 2012, 09:16:19 PM

I wanted to try to get the intersecting variables by using pure mathematics instead of comparisons. I don't think that this solution is more efficient, I just wanted to solve it this way because I wanted to know for sure if it was possible. Everything that I've tried has worked so far, but that doesn't mean that it works perfectly. Please note that this has been modified a few times, so some of you might notice that parts of this code have changed. Here is the code, if anyone spots a scenario that doesn't work or found a means of making this more efficient mathematically, let me know. Here's the implementation and the main part of the code:

#import "Rectangle.h"

@implementation Rectangle


@synthesize width, height;


-(void) setWidth:(float) w andHeight: (float) h
{
    width = w;
    height = h;
}

-(void) setOrigin: (float) a: (float) b
{
    if (! origin)
    origin = [[XYPoint alloc] init];
    
    origin.x = a;
    origin.y = b;
}

-(XYPoint *) origin
{
    return origin;
}

-(Rectangle *) intersect: (Rectangle *) rect2;
{
    Rectangle *newRect = [[Rectangle alloc] init];
    [newRect setOrigin: origin.x : origin.y];
    
    float a = origin.x - rect2.origin.x;
    float b = origin.y - rect2.origin.y;
    float c = ((origin.x + width) - rect2.origin.x) - ((rect2.origin.x + rect2.width) - origin.x);
    float d = ((origin.y + height) - rect2.origin.y) - ((rect2.origin.y + rect2.height) - origin.y);
    float e = ((origin.x + width) - rect2.origin.x) - sqrt(((c + fabsf(c)) / 2) * c);                                                                   // Returns shortest "(origin.x + width) - opposing origin.x" value
    float f = ((origin.y + height) - rect2.origin.y) - sqrt(((d + fabsf(d)) / 2) * d);                                                                // Returns shortest "(origin.y + height) - opposing origin.y" value
    float g = width - sqrt((((width - rect2.width) + fabsf(width - rect2.width)) / 2) * (width - rect2.width));                        // Returns the shortest width value
    float h = height - sqrt((((height - rect2.height) + fabsf(height - rect2.height)) / 2) * (height - rect2.height));              // Returns the shortest height value
    float i = sqrt(((a - fabsf(a)) / 2) * a) + origin.x;                                                                                        // Returns potential newRect Xorigin of the highest positive or negative value
    float j = sqrt(((b - fabsf(b)) / 2) * b) + origin.y;                                                                                      // Returns potential newRect Yorigin of the highest positive or negative value
    float k = e - sqrt((((e - g) + fabsf(e - g)) / 2) * (e - g));                                                                         // Returns potential newRect width of the lowest positive or negative value
    float l = f - sqrt((((f - h) + fabsf(f - h)) / 2) * (f - h));                                                                           // Returns potential newRect height of the lowest positive or negative value
    float m = k - sqrtf((((k - l) + fabsf(k - l)) / 2) * (k - l));                                                                       // If either the newRectwidth or the newRect height <= 0, returns negative value, > 0 returns positive value
    float n = sqrtf(((m + fabsf(m)) / 2) * m);                                                                                          // If m <= 0, returns 0, otherwise it returns a positive value
    float o = (fabsf(i) * n) - sqrtf(((((fabsf(i) * n) - fabsf(i)) + fabsf((fabsf(i) * n) - fabsf(i))) / 2) * ((fabsf(i) * n) - fabsf(i)));            // Returns either 0 or absolute value of origin.x
    float p = (fabsf(j) * n) - sqrtf(((((fabsf(j) * n) - fabsf(j)) + fabsf((fabsf(j) * n) - fabsf(j))) / 2) * ((fabsf(j) * n) - fabsf(j)));         // Returns either 0 or absolute value of origin.y
    
    newRect.origin.x = o - (2 * sqrtf(((o * i) - fabs(o * i)) / -2));  
    newRect.origin.y = p - (2 * sqrtf(((p * j) - fabs(p * j)) / -2));    
    newRect.width = (fabsf(k) * n) - sqrtf(((((fabsf(k) * n) - fabsf(k)) + fabsf((fabsf(k) * n) - fabsf(k))) / 2) * ((fabsf(k) * n) - fabsf(k)));
    newRect.height = (fabsf(l) * n) - sqrtf(((((fabsf(l) * n) - fabsf(l)) + fabsf((fabsf(l) * n) - fabsf(l))) / 2) * ((fabsf(l) * n) - fabsf(l)));
    
    return newRect;
}
@end


#import "Rectangle.h"

int main(int argc, const char * argv[])
{
    
    @autoreleasepool {
        
        float x,y;
        
        NSLog(@"Input x and y coordinates of the origin of the the first rectangle:");
        scanf("%f %f", &x, &y);
        
        Rectangle *rect_1 = [[Rectangle alloc] init];
        [rect_1 setOrigin: x : y];
        
        NSLog(@"Input the Rectangles' width and height:");
        scanf(" %f %f", &x, &y);
        
        [rect_1 setWidth: x andHeight: y];
        
        NSLog(@"Input x and y coordinates of the origin of the the second rectangle:");
        scanf("%f %f", &x, &y);
        
        Rectangle *rect_2 = [[Rectangle alloc] init];
        [rect_2 setOrigin: x : y];
        
        NSLog(@"Input the Rectangles' width and height:");
        scanf(" %f %f", &x, &y);
        
        [rect_2 setWidth: x andHeight: y];
        
        Rectangle *newRect = [[Rectangle alloc] init];
        
        newRect = [(rect_1) intersect: (rect_2)];
        NSLog(@"The rectangles intersect at x %f and y %f and has a width of %f and a height of %f", newRect.origin.x, newRect.origin.y, newRect.width, newRect.height);
    }
    return 0;
}
Last Edit: April 27, 2012, 06:42:58 PM by electricb Logged
krislwright
Newbie
*
Posts: 7


Email




Reply #1 on: May 18, 2012, 08:24:47 AM

What is so hard about posting code the proper way?

Post code the proper way! You've got way too much information here to read as text!!!
Logged
mo7ionsickness
Jr. Member
**
Posts: 50






Reply #2 on: August 18, 2012, 02:00:38 PM

Code: (Objective-C)
-(Rectangle *) intersect:(Rectangle *)bRect
{
    Rectangle *aRect = [[Rectangle alloc] init];
   
    if (((self.origin.y + self.height) > bRect.origin.y) &&
        ((bRect.origin.x + bRect.width) > self.origin.x)) {
       
        XYPoint *aPoint = [[XYPoint alloc] init];
        [aPoint setX:self.origin.x andY:bRect.origin.y];
        aRect.origin = aPoint;
        aRect.width = (bRect.origin.x + bRect.width) - self.origin.x;
        aRect.height = (self.origin.y + self.height) - bRect.origin.y;
       
    }
    return aRect;
}

2012-08-18 22:07:12.539 Rectangle[2368:303] First rectangle origin (400.00, 300.00)
2012-08-18 22:07:12.539 Rectangle[2368:303] First rectangle w = 100.00, h = 180.00
2012-08-18 22:07:12.540 Rectangle[2368:303] Second rectangle origin (200.00, 420.00)
2012-08-18 22:07:12.540 Rectangle[2368:303] Second rectangle w = 250.00, h = 75.00
2012-08-18 22:07:12.541 Rectangle[2368:303] Overlaping rectangle origin (400.00, 420.00)
2012-08-18 22:07:12.541 Rectangle[2368:303] Overlaping rectangle w = 50.00, h = 60.00

2012-08-18 22:06:06.524 Rectangle[2360:303] First rectangle origin (4000.00, 300.00)
2012-08-18 22:06:06.525 Rectangle[2360:303] First rectangle w = 100.00, h = 180.00
2012-08-18 22:06:06.526 Rectangle[2360:303] Second rectangle origin (200.00, 4200.00)
2012-08-18 22:06:06.527 Rectangle[2360:303] Second rectangle w = 250.00, h = 75.00
2012-08-18 22:06:06.530 Rectangle[2360:303] Overlaping rectangle origin (0.00, 0.00)
2012-08-18 22:06:06.531 Rectangle[2360:303] Overlaping rectangle w = 0.00, h = 0.00
Last Edit: August 18, 2012, 02:10:44 PM by mo7ionsickness 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.