0

我一生都无法弄清楚为什么 myRect.origin.x 和 myRect.origin.y 显示的值为 0。为代码的广度道歉,我确实删除了一些显然不相关但不太确定的东西在其他事情上。另外,我知道我的 intersect 方法还不能正常工作,但我正在努力。

//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;
}

如您所见,我尝试使用 setX andY 方法以及显式设置 x 和 y 值,但程序仍然为 myRect.origin 的 x 和 y 值返回 0 值。

4

2 回答 2

2

您的原点始终为零,因为您从未创建 XYPoint 对象。将此添加到您的 Rectangle 类:

- (id)init
{
    self = [super init];
    if (self) {
        origin = [[XYPoint alloc] init];
    }
    return self;
}
于 2013-07-25T06:55:09.243 回答
1

看起来您从未创建过 的实例,XYPoint因此您向nil.

您应该使用CGRect(即使您的类是它的包装器),因为它会使您的代码和生活变得更加简单。

于 2013-07-25T06:56:36.110 回答