Re: NSBezierPath containsPoint question
Re: NSBezierPath containsPoint question
- Subject: Re: NSBezierPath containsPoint question
- From: Mark Onyschuk <email@hidden>
- Date: Tue, 27 Apr 2004 05:50:35 -0400
The example at cocoadev is interesting but soon or later you may
need=20
to do hit testing for other things than NSBezierPath=85
I am currently experimenting the following and currently have=20
satisfying results, even if optimization is still to be done.
(the classic draw in a small bitmap and see if pixels have been
changed=20=
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...
}
So, assuming something drawable (you could substitute for an image
composited to a position, a cell, or whatever, so long as it draws):
1. first lock focus on yourself, you're doing some on-the-spot drawing
2. clear your background to alpha 0, to give clear color to test
against later
3. draw your drawable thing in its normal location
4. use NSReadPixel to discover the pixel color at the given point
5. unlock focus since this is all the drawing you need to do
If the point's alpha component at mouseLoc is clear, then you've
missed. If it's opaque then you've got a hit, since it will have been
made opaque by the on-the-spot drawing you just did.
-Mark
_______________________________________________
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.