我正在制作一个 CGPoint 和一个 CGPathRef,然后尝试查找 CGPoint 是否在 CGPathRef 内。这是代码:
CGMutablePathRef path = CGPathCreateMutable();
CGPathMoveToPoint(path, NULL, 0, 0);
CGPathMoveToPoint(path, NULL, 200, 0);
CGPathMoveToPoint(path, NULL, 200, 200);
CGPathMoveToPoint(path, NULL, 0, 200);
CGPathCloseSubpath(path);
CGPoint hitPoint = CGPointMake(77, 77);
if ( CGPathIsEmpty(path) )
NSLog(@"Path Is Empty!");
else
{
if ( CGPathIsRect(path, NULL) )
NSLog(@"Path is a Rectangle!");
else
{
NSLog(@"Path is NOT a Rectangle!");
if (CGPathContainsPoint(path, NULL, hitPoint, FALSE)) // FALSE or TRUE - same result
NSLog(@"Hit Point Inside: x=%f, y=%f", hitPoint.x, hitPoint.y);
else
NSLog(@"Hit Point Outside: x=%f, y=%f", hitPoint.x, hitPoint.y);
}
}
输出内容如下:
Path is NOT a Rectangle!
Hit Point Outside: x=77.000000, y=77.000000
路径显然是一个矩形,并且该点位于封闭路径内。请告诉我我在这里做错了什么。