1

[編集: 長方形の定義を下部に追加] [編集 2: XYPoint インターフェイスを下部に追加]

2 つの長方形が重なるかどうかをチェックするメソッドに取り組んでいます。(ええ、私は Kochan's Programming in Objective-Cで演習を行っていますが、これは非常に新しいものです。) これをコンパイルすると、エラー メッセージは次のようになります。「バイナリ + のオペランドが無効です」。最初のifステートメントとそれに続くif-elseで取得します。

ポインターに問題があると思いますが、こちゃんはあまり話さないです。

そして、これらの行を削除すると、メソッドの残りの部分は正常に機能します。また、関連する変数はすべて浮動小数点型です。

ヘルプ?

また、この方法に関する他の考えは大歓迎です。(例えば、どうすればコードの行がそれほど長くならないようにできますか。私が言ったように、これは痛いほど新しいです。)

-(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
4

1 に答える 1

3

おそらくタイプミスです:

// 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) && 
                         //^^^This is trying to add an XYPoint,
                         // which is an object, to a float.
   (origin.y + height) >= (r2.origin.y + r2.height) ) 
{
   overlapRectangle = r2;
}

// Test to see if Rectangle 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 && 
                            //^^^Same thing.
         origin.y + height <= r2.origin.y + r2.height ) 
{
...

コンパイラは、追加を求めていた型が何であるかを通知する必要があります。

エラー: バイナリ + に対するオペランドが無効です ('struct XYPoint *' と 'float' があります)

それが鍵です。2 つの float を追加するようにr2.origintoを変更するだけです。r2.origin.x

線の長さに関しては、できることが 2 つあります。私が行ったように、条件の各セグメントを異なる行に移動できますがRectangle、テストを実行するためのメソッドをいくつか作成するのがおそらく最善です。これにより、コードが読みやすくなるため、6 か月後にコードを読み直すと、次の行が表示されます。

if( [self containsRectangle:r2] || [self isEqualToRectangle:r2] ){

何が起こっているのかすぐにわかります。そのためのいくつかの提案を次に示します。

- (BOOL)containsRectangle:(Rectangle *)otherRect {
    BOOL originBelow = ((origin.x <= otherRect.origin.x) && 
                        (origin.y <= otherRect.origin.y));
    float maxX = origin.x + width;
    float otherMaxX = otherRect.origin.x + otherRect.width;
    BOOL maxXGreater = maxX >= otherMaxX;
    Bfloat maxY = origin.y + height;
    float otherMaxY = otherRect.origin.y + otherRect.height;
    BOOL maxYGreater = maxY >= otherMaxY;
    return originBelow && maxXGreater && maxYGreater;
}

- (BOOL)isEqualToRectangle:(Rectangle *)otherRect {
    BOOL sizeEqual = ((width == otherRect.width) && 
                      (height == otherRect.height));
    return sizeEqual && [origin isEqualToXYPoint:otherRect.origin];

}

注:これらはテストしていません。あなたの条件から貼り付けただけなifので、使用する前に再確認してください。誤字脱字ですが直しました。

XYPointここでもメソッドを作成したことに注意してくださいisEqualToXYPoint:。両方の s のandが等しいBOOL場合にa を返すように、それを実装することもできます。xyXYPoint

于 2011-05-28T04:37:53.537 に答える