0

我是编程和objective-c的新手(所以我可能会离开这里),我正在通过objective c第4版教科书进行编程,并且已经陷入其中一个练习。

谁能告诉下面的方法有什么问题?在我的程序中有一个矩形类,它具有设置它的宽度、高度和原点的方法(来自一个名为 XYPoint 的类)。

containsPoint 方法检查一个矩形的原点是否在另一个矩形内。当我测试此方法时,即使矩形确实包含该点,它也总是返回“否”。

intersects 方法将一个矩形作为参数 (aRect) 并在 if 语句中使用 containsPoint 方法来检查它是否与接收方相交,如果相交,则返回一个矩形,该矩形的原点位于相交处并具有正确的宽度和高度。

-(BOOL) containsPoint:(XYPoint *) aPoint
{
//create two variables to be used within the method 
float upperX, upperY;

//assign them values, add the height and width to the origin values to range which the XYPoint must fall into 
upperX = origin.x + height;
upperY = origin.y + width;    

//if the value of aPoint's x and y points fall between the object's origin and the upperX or upperY values then the rectangle must contain the XYPoint and a message is sent to NSLog

if ((aPoint.x >= origin.x) && (aPoint.x <= upperX) && (aPoint.y >= origin.y) && (aPoint.y <= upperY) ) 
{
    NSLog(@"Contains point");
    return YES;

}

else
{
    NSLog(@"Does not contain point");
    return NO;

}


}


-(Rectangle *) intersects: (Rectangle *) aRect
{
//create new  variables, Rectangle and XYPoint objects to use within the method
Rectangle *intersectRect = [[Rectangle alloc] init];
XYPoint *aRectOrigin = [[XYPoint alloc] init];
float wi, he;   //create some variables

if ([self containsPoint:aRect.origin]) {     //send the containsPoint method to self to test if the intersect
    [aRectOrigin setX:aRect.origin.x andY:origin.y];   //set the origin for the new intersecting rectangle
    [intersectRect setOrigin:aRectOrigin];
    wi = (origin.x + width) - aRect.origin.x;   //determine the width of the intersecting rectangle
    he = (origin.y + height) - aRect.origin.y;  //determine the height of the intersecting rectangle
    [intersectRect setWidth:wi andHeight:he];   //set the rectangle's width and height

    NSLog(@"The shapes intersect");
    return intersectRect;
}

//if the test returned NO then send back these values
else {
    [intersectRect setWidth:0. andHeight:0.];
    [aRectOrigin setX:0. andY:0.];
    [intersectRect setOrigin:aRectOrigin];

    NSLog(@"The shapes do not intersect");
    return intersectRect;
}
}

当我使用以下代码进行测试时

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

        Rectangle *aRectangle = [[Rectangle alloc] init];
        Rectangle *bRectangle = [[Rectangle alloc] init];
        Rectangle *intersectRectangle = [[Rectangle alloc] init];
        XYPoint *aPoint = [[XYPoint alloc] init];
        XYPoint *bPoint = [[XYPoint alloc] init];

        [aPoint setX:200.0 andY:420.00];
        [bPoint setX:400.0 andY:300.0];

        [aRectangle setWidth:250.00 andHeight:75.00];
        [aRectangle setOrigin:aPoint];

        [bRectangle setWidth:100.00 andHeight:180.00];
        [bRectangle setOrigin:bPoint];    

        printf("Are the points within the rectangle's borders?  ");  
        [aRectangle containsPoint: bPoint] ? printf("YES\n") : printf("NO\n");  
        intersectRectangle = [aRectangle intersects:bRectangle];

}
    return 0;   
}

我得到以下输出

Contains point  
The origin is at 0.000000,0.000000, the width is 250.000000 and the height is 75.000000  
4

4 回答 4

3

这是代码(我目前正在使用 Stephen Kochan 的“Objective-C 第 5 版编程”),根据您的变量和语法判断,看起来您也是。

    // Use Floats for XYPoint and Rectangular exercise 8.4 on pg. 166

导入“矩形.h”

导入“XYPoint.h”

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

@autoreleasepool {
    Rectangle *myRect = [[Rectangle alloc] init];
    Rectangle *yourRect = [[Rectangle alloc] init];
    XYPoint   *myPoint = [[XYPoint alloc] init];
    XYPoint   *yourPoint = [[XYPoint alloc] init];

    // For first Rectangle set w, h, origin
    [myRect setWidth:250 andHeight:75];
    [myPoint setX:200 andY:420];
    myRect.origin = myPoint;

    // Second Rectangle
    [yourRect setWidth:100 andHeight:180];
    [yourPoint setX:400 andY:300];
    yourRect.origin = yourPoint;

    // Find Points of intersection
    float x1, x2, y1, y2;
    x1 = MAX(myRect.origin.x, yourRect.origin.x);
    x2 = MIN(myRect.origin.x + myRect.width, yourRect.origin.x + yourRect.width);
    y1 = MAX(myRect.origin.y, yourRect.origin.y);
    y2 = MIN(myRect.origin.y + myRect.height, yourRect.origin.y + yourRect.height);

    // Make Intersecting Rectangle
    Rectangle *intrRect = [[Rectangle alloc] init];
    [intrRect setWidth: abs(x2 - x1) andHeight: abs(y2 - y1)];

    // Print Intersecting Rectangle's infor for a check
    NSLog(@"Width = %g, Height = %g, Bottom point = (%g, %g), Top point = (%g, %g)", intrRect.width, intrRect.height, x1, y1, x2, y2);

}
return 0;

}

于 2013-03-29T04:51:56.803 回答
2

为什么不直接使用包含的函数来确定交叉点和/或包含:

if (CGRectContainsPoint(CGRect rect, CGPoint point))
{
    // Contains point...
}

或者

if (CGRectIntersectsRect(CGRect rectOne, CGRect rectTwo))
{
    // Rects intersect...
}

或者

if (CGRectContainsRect(CGRect rectOne, CGRect rectTwo))
{
    // RectOne contains rectTwo...
}
于 2012-10-02T22:31:42.237 回答
0

您在几何和 Objective-C 中都犯了许多错误,但这就是您学习的方式!

  1. 几何:两个矩形相交一个的原点不需要在另一个的区域内。您的两个矩形相交,但交点不包含任何一个原点。所以你containsPoint:会回来的NO
  2. 只有在创建对象时才分配对象,而不是在您调用的代码创建并返回它们时分配。因此,您的分配Rectangle *intersectRectangle = [[Rectangle alloc] init];创建了一个对象,然后您的分配将其丢弃intersectRectangle = [aRectangle intersects:bRectangle];
  3. 对于诸如此类的简单类,通常创建init设置属性的方法,因为创建对象而不设置属性是没有用的。例如,对于您的XYPoint班级,您通常会有一个初始化方法- (id) initWithX:(float)x andY:(float)y;。然后,您可以[[XYPoint alloc] initWithX:200.0 andY:420.0]]一次性创建和初始化。
  4. 正如您正在试验的那样,这并不重要,但在具有值语义的非常简单和小类的实际代码中- 这意味着它们的行为很像整数和浮点数 - 通常使用结构 ( struct) 和函数而不是对象和方法。查找NSRectNSPoint作为示例的定义。

高温高压

于 2012-10-02T23:26:08.840 回答
0
 //intersect function
 -(Rectangle *)intersect:(Rectangle *)secondRec
 {
 int intersectRectWidth;
 int intersectRectHeight;
 int intersectRectOriginX;
 int intersectRectOriginY;
 Rectangle * intersectRec =[[Rectangle alloc]init];
 intersectRectOriginX = MAX(self.origin.x,secondRec.origin.x);

 intersectRectOriginY = MAX((self.origin.y) , (secondRec.origin.y));
int myWidth=self.width+self.origin.x;
int secondRecWidth=secondRec.width+secondRec.origin.x;
int tempWidth=secondRecWidth - myWidth;
if (tempWidth > 0) {
    intersectRectWidth=secondRec.width - tempWidth;
}
else
{
    tempWidth=abs(tempWidth);
    intersectRectWidth = self.width - tempWidth;
}

//height
int myHeight=self.height+self.origin.y;
int secondRecHeight=secondRec.height +secondRec.origin.y;
int tempHeight=secondRecHeight - myHeight;
if (tempHeight > 0) {
    intersectRectHeight=secondRec.height - tempHeight;
}
else
{
    tempHeight=abs(tempHeight);
    intersectRectHeight = self.height - tempHeight;
}
intersectRec.width=intersectRectWidth;
intersectRec.height=intersectRectHeight;
XYPoint * intersectOrigin =[[XYPoint alloc]init];
[intersectOrigin setX:intersectRectOriginX andY:intersectRectOriginY];
[intersectRec setOrigin:intersectOrigin];
return intersectRec;

}

    //main
    XYPoint * org1=[[XYPoint alloc]init];
    XYPoint * org2=[[XYPoint alloc]init];
    Rectangle * firstRec=[[Rectangle alloc]init];
    Rectangle * secondRec=[[Rectangle alloc]init];
    Rectangle * intersectRec=[[Rectangle alloc]init];
    [org1 setX:400 andY:300];
    [org2 setX:200 andY:420];
    [firstRec setOrigin:org1];
    [secondRec setOrigin:org2];
    [firstRec setWidth:100 andHeight:180];
    [secondRec setWidth:250 andHeight:75];
    intersectRec=[firstRec intersect:secondRec];
    NSLog(@"intersect rectangle origin.x= %i origin.y=%i width=%i height=%i",intersectRec.origin.x,intersectRec.origin.y,intersectRec.width,intersectRec.height);
于 2014-12-22T11:50:18.010 回答