[編輯:加入在底部矩形定義] [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];
}
矩形定義:
//接口,Rectangle類
@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)的任何特定原因? – highlycaffeinated 2011-05-28 03:59:47
錯誤顯示在第一個* if *行和第一個* if-else *行上。我將抓住矩形定義。 – ctaggart 2011-05-28 04:07:54
「寬度」,「高度」和原點座標的類型是基本類型,比如「float」?還是一個對象?編輯:哦,你要發佈標題。這將回答我的問題。大! – 2011-05-28 04:11:58