Question

I cannot for the life of me figure out why myRect.origin.x and myRect.origin.y are displaying values of 0. Apologies for the breadth of code, I did remove some things that were clearly not relevant but wasn't so sure on other things. Also, I know that my intersect method doesn't yet work properly but I'm working on it.

//Rectangle.h
#import <Foundation/Foundation.h>

@class XYPoint;

@interface Rectangle: GraphicObject

@property float height, width;

-(XYPoint*) origin;
-(void) setOrigin: (XYPoint*) pt;
-(float) area;
-(float) perimeter;
-(void) setHeight: (float) h andWidth: (float) w;
-(void) translate: (XYPoint*) t;
-(BOOL) containspoint: (XYPoint*) aPoint;
-(Rectangle*) intersect: (Rectangle*) intr;

@end

@interface XYPoint: NSObject

@property float x,y;

-(void) setX:(float) xVal andY:(float) yVal;

@end


//Rectangle.m

#import "Rectangle.h"

@implementation Rectangle

{
    XYPoint *origin;
}

@synthesize width, height;

-(float) area
{
    return width*height;
}

-(float) perimeter
{
    return 2*(width*height);
}

-(void) setHeight:(float) h andWidth: (float) w
{
    width = w;
    height = h;
}

-(void) setOrigin:(XYPoint *)pt
{
    origin = pt;
}

-(XYPoint*) origin
{
    return origin;
}

-(void) translate:(XYPoint *)t
{
    origin = t;
}

-(BOOL) containspoint:(XYPoint *)aPoint
{
    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;

}

-(Rectangle*) intersect: (Rectangle*) intr

{

    Rectangle *zerorec = [[Rectangle alloc]init];
    Rectangle *rec1 = [[Rectangle alloc]init];
    Rectangle *rec2 = [[Rectangle alloc]init];

    zerorec.origin.x = 0;
    zerorec.origin.y = 0;
    zerorec.height = 0;
    zerorec.width = 0;

    if ((self.origin.x >= intr.origin.x && self.origin.x <= intr.origin.x + intr.width) || (intr.origin.x >= self.origin.x && intr.origin.x >= self.origin.x + self.width))
    {
        if ((self.origin.y >= intr.origin.y && self.origin.y <= intr.origin.y + intr.height) || (intr.origin.y >= self.origin.y && intr.origin.y >= self.origin.y + self.height))

            NSLog(@"The rectangles intersect");
            //rec 1 = the rectangle with the greater x value

            if (self.origin.x >= intr.origin.x && self.origin.x <= intr.origin.x + intr.width)
                rec1 = self, rec1.origin = self.origin, rec2 = intr, rec2.origin = intr.origin;

            else rec2 = self, rec2.origin = self.origin, rec1 = intr, rec1.origin = intr.origin;



            Rectangle *returnedRec = [[Rectangle alloc]init];

            returnedRec.origin.x = rec1.origin.x;
            returnedRec.origin.y = rec2.origin.y;

            //rec 1  = rectangle with greater y value

            if (self.origin.y > intr.origin.y)
                rec1 = self, rec2 = intr;
            else rec1 = intr, rec2 = self;

            returnedRec.height = rec1.height - (rec1.origin.y - rec2.origin.y);
            returnedRec.width = rec1.width - (rec2.origin.y - rec1.origin.y);

            return returnedRec;
    }



        else return zerorec;

    }

@end


@implementation XYPoint

@synthesize x, y;

-(void) setX:(float) xVal andY:(float) yVal

{
    x = xVal;
    y = yVal;
}

@end

//main.m

#import <Foundation/Foundation.h>
#import "Rectangle.h"

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

    @autoreleasepool {

        Rectangle *returnedRect = [[Rectangle alloc]init];

        Rectangle *myRect = [[Rectangle alloc]init];
        Rectangle *yourRect = [[Rectangle alloc]init];

        [myRect.origin setX:3 andY: 2];
        [myRect setHeight:4 andWidth:5];
        [yourRect.origin setX:6 andY:4];
        [yourRect setHeight:7 andWidth:9];

        myRect.origin.x = 2;
        myRect.origin.y = 3;

        returnedRect = [myRect intersect:yourRect];

        NSLog(@"x = %f, y = %f", returnedRect.origin.x, returnedRect.origin.y);

        NSLog(@"height = %f, width = %f", returnedRect.height, returnedRect.width);

        NSLog(@"%f, %f", myRect.origin.x, myRect.origin.y);


    }
    return 0;
}

As you can see, I tried using the setX andY method along with setting the x and y values explicitly and the program still returns a value of 0 for the x and y values of myRect.origin.

Was it helpful?

Solution

Your origin is always nil because you never create an object of XYPoint. Add this to your Rectangle class:

- (id)init
{
    self = [super init];
    if (self) {
        origin = [[XYPoint alloc] init];
    }
    return self;
}

OTHER TIPS

It looks like you never create an instance of XYPoint so you're sending a number of messages to nil.

You should use CGRect (even if your class is a wrapper around it) because it will make your code and life a lot simpler.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top