Skip to content

CCRect::containsPoint not compatible with CGRectContainsPoint #2423

@jotel

Description

@jotel

While porting my game from cocos2d for iPhone to cocos2d-x I faced an issue in CCRect::containsPoint() method, it looks like it improperly handles rectangle boundaries. Here is a simple test which shows the issue:

#import <SenTestingKit/SenTestingKit.h>
#import <UIKit/UIKit.h>
#import "cocos2d.h"

@interface RectTest : SenTestCase
@end

@implementation RectTest

- (void) testRect {
    CGRect rect1 = CGRectMake(0, 0, 10, 10);

    STAssertTrue(CGRectContainsPoint(rect1, CGPointMake(0,0)), @"");
    STAssertFalse(CGRectContainsPoint(rect1, CGPointMake(10,10)), @"");

    cocos2d::CCRect rect2 = cocos2d::CCRectMake(0, 0, 10, 10);

    STAssertTrue(rect2.containsPoint(cocos2d::CCPointMake(0,0)), @"");
    STAssertFalse(rect2.containsPoint(cocos2d::CCPointMake(10,10)), @"");
}

@end

so the method implementation should be:

bool CCRect::containsPoint(const CCPoint& point) const
{
    bool bRet = false;

    if (point.x >= getMinX() && point.x < getMaxX()
        && point.y >= getMinY() && point.y < getMaxY())
    {
        bRet = true;
    }

    return bRet;
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions