【问题标题】:Objective C Object to MethodObjective C 对象到方法
【发布时间】:2012-12-16 07:29:07
【问题描述】:

2013 年快乐。

我对 2 个矩形的交集方法有点问题,但我遇到了一个问题,它不允许我在这个主题上继续前进。

我使用了 4 个对象、2 个点和 2 个矩形。 Point1 是 rectangle1 的原点,point2 是 rectangle2 的原点。

然后我调用 rectangle1 的 intersect 方法,发送 rectangle2 对象,看是否有交集。

这里的问题是,在方法内部,两个矩形的原点最终是相同的坐标,因此,如果我没有不同的原点,我将无法获得好的信息。

这是我的 .h 文件:

@interface XYPoint : NSObject

@property int x,y;

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

@end

@interface Rectangle: graphicObject

@property float width, height;

- (void) setWidth:(float)w andHeight: (float) h;
- (void) setOrigin: (XYPoint *) pt;
- (bool) containsPoint: (XYPoint *) aPoint;
- (void) intersect: (Rectangle *) rect;

@end

这是我的 .m 文件:

@implementation XYPoint

@synthesize x,y;

-(void) setX:(int)xVal andY:(int)yVal;
{
    x = xVal;
    y = yVal;
}

@end

@implementation Rectangle

@synthesize width, height;

XYPoint *origin;

- (void) setWidth:(float) w andHeight: (float) h;
{
    width = w;
    height = h;
}
- (float) area
{
    return width * height;
}
- (float) perimeter
{
    return (2*width) + (2*height);
}
-(void) setOrigin: (XYPoint *) pt
{
    if (! origin)
    {
        origin = [[XYPoint alloc]init];
    }
    origin.x = pt.x;
    origin.y = pt.y;
}

- (XYPoint *) origin

{
    return origin;
}

-(void) intersect: (Rectangle *) rect // 
{
    int pointCount;
    pointCount = 0;

    /*  R1o           R2o                   R1F             R2F */
    /* origin.x       rect.origin.x      origin.x+w       rect.origin.x+w */


    if (    (origin.x <= rect.origin.x) && (rect.origin.x <= (origin.x + width) )  )
        pointCount = pointCount +1;
    NSLog(@"width = %g height = %g origin  = (%d,%d)", width, height, origin.x, origin.y);


    NSLog(@"rect.width = %g rect.height = %g rect.origin  (%d,%d)", rect.width, rect.height, rect.origin.x, rect.origin.y);

    if (    (rect.origin.x <= ( origin.x + width ) ) && (origin.x + width <= rect.origin.x + rect.width)       )
        pointCount = pointCount + 1;

    if (    (origin.y <= rect.origin.y) && (rect.origin.y <= (origin.y + height) ) )
        pointCount = pointCount + 1;

    if (    (rect.origin.y <= (origin.y + height) && ( origin.y + height <= rect.origin.y + rect.height)) )
        pointCount = pointCount +1;

    if (pointCount == 4)
        NSLog (@"the rectangles intersect!");
    else
        NSLog (@"The rectangles don't intersect.");
}

@end

这是我的主文件:

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

    @autoreleasepool {

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


        XYPoint * myPoint1 = [[XYPoint alloc]init];
        [myPoint1 setX: 0 andY: 0];
        [myRectangle1 setWidth: 3 andHeight: 3];
        [myRectangle1 setOrigin : myPoint1];

        Rectangle * myRectangle2 = [[Rectangle alloc] init];
        XYPoint * myPoint2 = [[XYPoint alloc]init];
        [myPoint2 setX: 2 andY: 2];
        [myRectangle2 setWidth: 4 andHeight: 4];
        [myRectangle2 setOrigin : myPoint2];

        [myRectangle1 intersect: myRectangle2];

    }
    return 0;
}

我上次运行它时,我得到了这个:

宽度 = 3 高度 = 3 原点 = (2,2)

rect.width = 4 rect.height = 4 rect.origin (2,2)

矩形相交!

看到了吗?两个矩形具有相同的原点,即使它们的设置不同。

非常感谢!

【问题讨论】:

  • 这只是一个智力练习,你故意不使用任何已经可用的几何结构或函数吗?
  • jrTurton:是的,这只是一个智力示例,我是一个新手,我只是为了好玩而尝试自己学习 Objective-C!谢谢。
  • 那就玩得开心吧!祝你好运!

标签: objective-c object methods


【解决方案1】:

您在矩形类 .m 文件中将 origin 声明为文件级全局变量,这意味着每个 Rectangle 实例将为 origin 使用相同的变量实例。您需要在 .h 文件内的接口中将其声明为成员变量。

更新了@Chuck 的更正。 (谢谢)

【讨论】:

  • 实际上,他将其声明为一个全局变量。没有静态关键字。
  • 嗨 Chuck,对不起,我是新手,我必须阅读 CRD 的答案才能理解你的答案,现在我明白了,非常感谢您的回答!
【解决方案2】:

您目前拥有:

@implementation Rectangle

@synthesize width, height;

XYPoint *origin;

这将origin 声明为全局 变量而不是实例 变量;因此,不是每个矩形都有一个变量,而是所有矩形共享一个变量。将这些行更改为:

@implementation Rectangle
{
   XYPoint *origin;
}

@synthesize width, height;

这使得origin 成为一个实例变量。

[注意:较旧的编译器要求您在 @interface 中声明实例变量,但对于当前的编译器,您可以在 @implementation 中声明它们。从设计的角度来看,这要好得多。]

【讨论】:

  • CRD,非常感谢,它确实解决了我的问题,我已经检查了几天。再次感谢!
猜你喜欢
  • 2015-02-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-04-13
  • 2012-04-11
  • 1970-01-01
  • 1970-01-01
  • 2014-11-13
相关资源
最近更新 更多