-[NSBezierPath containsPoint:] (again)
-[NSBezierPath containsPoint:] (again)
- Subject: -[NSBezierPath containsPoint:] (again)
- From: Simon Stapleton <email@hidden>
- Date: Thu, 04 Oct 2001 09:58:53 +0100 (BST)
OK, guys, gals.
Back to the never-ending saga of NSBezierPath.
I was looking for some hit detection stuff for my object modeller
project[1] last night, and I'd kind of misread what 'containsPoint'
should do. I was looking for something that would detect whether a
point was _on_ a path, and it appears that containsPoint detects
points _bounded by_ a path. The docs are a little unclear on this
matter, I think, but I'll let that pass.
Anyroadup. First attempt.
- (void) mouseDown:(NSEvent *)event {
NSPoint point = [self convertPoint:[event locationInWindow]
fromView:nil];
NSBezierPath * path = [NSBezierPath bezierPath];
[path moveToPoint:[self startPoint]];
[path lineToPoint:[self endPoint]];
if ([path containsPoint:point])
NSLog (@"Gotcha");
}
Now, no matter how close I click to the line (drawn in my view from
startpoint to endpoint, and yes I know I should just allocate one
bezierpath and modify it as I go along to save on allocations - this
is a dirty hack for testing), I never get the path containing the
point.
So, still floundering along under my delusions as to what the method
in question does, I thought 'Hmmm. Buggy. Let's try the GNUStep
version'. So I took my own advice, created a category on
NSBezierPath, and used that instead. Now the behaviour is that if
the point is on one side of the line, I get hits, and on the other,
none.
I then realised what I'd done wrong (and I'm writing a generalised
[NSBezierPath outlineHitsPoint:] type method).
So. I _think_ that the GNUStep implementation is buggy WRT open
paths (and I'll probably 'have a go at it' myself), but this might
explain the differences between the Apple implementation and the GNU
implementation that people were mentioning earlier when we were
talking about Apple's propensity for allocating autoreleased
flattened paths. The GNUStep implementation _seems_ OK for closed
paths, though, given some very minor testing, and it's very
definitely faster.
Hope this rather long mail helps someone.
Simon
[1] Yes, it's still going, no I haven't released anything yet, yes
I will as soon as I can[2]
[2] 18 hour (paid work) days permitting
--
PGP Key ID : 0x50D0698D
--
If the answer isn't obvious, the question is a distraction. Go find
an easier question.