Re: NSBezierPath containsPoint question
Re: NSBezierPath containsPoint question
- Subject: Re: NSBezierPath containsPoint question
- From: Robert Clair <email@hidden>
- Date: Tue, 27 Apr 2004 08:14:50 -0400
Another way to express this:
NSPoint mouseLoc; // assume this exists
MyDrawableShape *shape; // assume this exists
NSColor *pointColor = nil;
[self lockFocus];
[[NSColor clearColor] set];
NSRectFill([shape boundingRect]);
[shape drawSelf];
pointColor = NSReadPixel(mouseLoc);
[self unlockFocus];
if ([pointColor alphaComponent] > 0.1) {
// you've got a hit...
}
I wouldn't do this, for a number of reasons. Why draw the whole
shape to get one pixel ? Anyway, pixels are small and getting smaller
as the resolution of monitors increases. In order not to drive
your users nuts you need to give them a margin of error ("pick trap")
of some number of pixels.
The scheme Pascal outlines works quite well. In practice you would
probably keep a bounding box for each item on your display list
and compare it with the rect of the image you are drawing. Most of
your display list will be culled without drawing. The overhead of
traversing a display list and doing the box comparison
is negligible, even for thousands of objects.
As a small refinement, if you are keeping a list of multiple hits, you
could examine the pixels spiraling out from the center and record the
distance center as well as the object so you could present the most
likely
candidate first.
I had an even nicer scheme which involved giving each object a serial
number, encoding the serial number in a color, and rendering the object
in
that color to the picking image. Unfortunately I can't get Quartz to
stop interpolating when compositing stencil masks so I may have
to give it up.
Bob Clair
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.