开发者

Objective C shapes and points

开发者 https://www.devze.com 2022-12-12 23:10 出处:网络
I\'m trying to detect if a rectangle or a circle contains a point. It is not so hard, but I want to know that is there an开发者_开发问答y built-in method in objective c for this? Thanks!For rectangles

I'm trying to detect if a rectangle or a circle contains a point. It is not so hard, but I want to know that is there an开发者_开发问答y built-in method in objective c for this? Thanks!


For rectangles (as NSRects) there is the Foundation function NSPointInRect():

NSPoint somePoint = //The point you want to test for
NSRect someRect = //The rectangle you want to test in

BOOL rectContainsPoint = NSPointInRect(somePoint, someRect);

For circles, you can use the NSBezierPath instance method containsPoint:

NSBezierPath *circlePath = //Assume this is instantiated to a circle path
NSPoint somePoint = //The point you want to test for

BOOL circleContainsPoint = [circlePath containsPoint:somePoint];

Equally if you have a rectangular path you could use containsPoint: to test whether the point is in that rectangle.

Edit: As NSResponder pointed out, creating a full path object may not always be the most efficient method – if you already have circle paths for some kind of drawing or something then yes, but there are probably other more efficient ways of doing it. However using paths is a built-in method of doing it.

0

精彩评论

暂无评论...
验证码 换一张
取 消