【发布时间】:2011-05-28 03:48:29
【问题描述】:
[编辑:在底部添加矩形定义。] [Edit2:底部添加了XYPoint界面。]
我正在研究一种检查两个矩形是否重叠的方法。 (是的,我在 Kochan 的 Objective-C 编程 中,正在做练习,我对此非常陌生。)当我编译它时,错误消息是:“二进制操作数无效 + ”。我在第一个 if 语句和它后面的 if-else 中得到它。
我认为我对指针有疑问,但 Kochan 并没有谈论太多。
而且,如果我去掉这些行,该方法的其余部分就可以正常工作。并且相关变量都是浮点型。
帮助?
此外,完全欢迎对该方法提出任何其他想法。 (比如,我怎样才能让代码行不那么长。就像我说的那样,在这方面令人痛苦。)
-(void) overlap: (Rectangle *)r2
{
overlapRectangle = [[Rectangle alloc] init];
leftRectangle = [[Rectangle alloc] init];
rightRectangle = [[Rectangle alloc] init];
lowerRectangle = [[Rectangle alloc] init];
upperRectangle = [[Rectangle alloc] init];
BOOL xIntersect = NO;
BOOL yIntersect = NO;
// Test to see if the Rectangle contains, or is equal to, Rectangle b
if (origin.x <= r2.origin.x && origin.y <= r2.origin.y && (origin.x + width) >= (r2.origin + r2.width) && (origin.y + height) >= (r2.origin.y + r2.height) )
{
overlapRectangle = r2;
}
// Test to see if Retangle b contains, or is equal to, the Rectangle
else if (origin.x >= r2.origin.x && origin.y >= r2.origin.y && origin.x + width <= r2.origin + r2.width && origin.y + height <= r2.origin.y + r2.height )
{
overlapRectangle = self;
}
// I should add tests for triangles overlapping on three
// sides or overlapping on two sides, but I'm not going
// to right now. Just learning objects and methods.
// Test to see if rectangles overlap on the x-axis
// Current is an if, because I wanted to run the code below
// to see if it worked, and it did.
if (origin.x <= r2.origin.x)
{
leftRectangle = self;
rightRectangle = r2;
}
else
{
rightRectangle = self;
leftRectangle = r2;
}
if (rightRectangle.origin.x + rightRectangle.width > leftRectangle.origin.x)
{
xIntersect = YES;
}
// Test to see if rectangles overlap on the y-axis
if (origin.y <= r2.origin.y)
{
lowerRectangle = self;
upperRectangle = r2;
}
else
{
lowerRectangle = self;
upperRectangle = r2;
}
if (lowerRectangle.origin.y + lowerRectangle.height > upperRectangle.origin.y)
{
yIntersect = YES;
}
// If retangles overlap on both the x-axis and y-axis,
// determination of overlapping rectangle's origin, height, and width
// and display same.
if (xIntersect == YES && yIntersect == YES)
{
overlapRectangle.origin.y = upperRectangle.origin.y;
overlapRectangle.origin.x = rightRectangle.origin.x;
overlapRectangle.height = lowerRectangle.height - (upperRectangle.origin.y - lowerRectangle.origin.y);
overlapRectangle.width = leftRectangle.width - (rightRectangle.origin.x - leftRectangle.origin.x);
NSLog (@"Your rectangles overlap.");
NSLog (@"Rectangle: w = %g, h = %g", overlapRectangle.width, overlapRectangle.height);
NSLog (@"Area = %g, Perimeter = %g", [overlapRectangle area], [overlapRectangle perimeter]);
NSLog (@"Origin at (%g, %g)", overlapRectangle.origin.x, overlapRectangle.origin.y);
}
else
{
NSLog (@"Your rectangles do not overlap.");
}
[overlapRectangle autorelease];
[leftRectangle autorelease];
[rightRectangle autorelease];
[lowerRectangle autorelease];
[upperRectangle autorelease];
}
矩形定义:
// 接口,矩形类
@interface Rectangle : NSObject
{
float width;
float height;
XYPoint *origin;
// For overlapping calculations
Rectangle *overlapRectangle;
Rectangle *leftRectangle;
Rectangle *rightRectangle;
Rectangle *lowerRectangle;
Rectangle *upperRectangle;
}
@property float width, height;
-(XYPoint *) origin;
-(void) setOrigin: (XYPoint *) pt;
-(void) setWidth: (float) w andHeight: (float) h;
-(float) area;
-(float) perimeter;
-(void) print;
-(void) translate;
-(void) overlap: (Rectangle *)r2;
-(void) draw;
@end
XYPoint接口:
#import <Foundation/Foundation.h>
@interface XYPoint : NSObject
{
float x;
float y;
}
@property float x, y;
-(void) setX: (float) xVal andY: (float) yVal;
@end
【问题讨论】:
-
报错在哪一行?你能发布你对矩形的定义吗?有什么特别的理由不使用 CGGeometry 中的 CGRect 和 CGRectIntersectsRect(CGRect rect1, CGRect rect2)?
-
你的
width、height和原点坐标的类型是基本类型,比如float?还是一个对象?编辑:哦,你要发布标题。那将回答我的问题。太好了! -
好的,现在
XYPoint的界面是什么? :) -
Re:
CGRectIntersectsRect-- 这是一个很好的问题,但这些函数仅适用于 Apple 的CGRect结构。它们不能在此处与您的自定义类一起使用。 -
好的,我想我知道出了什么问题。正在寻找答案...
标签: objective-c binary operands