unexpected behavior with NSBezierPath's containsPoint:
unexpected behavior with NSBezierPath's containsPoint:
- Subject: unexpected behavior with NSBezierPath's containsPoint:
- From: email@hidden
- Date: Sat, 19 Aug 2006 18:46:20 -0400
Hello All,
I've been working up a simple interface for painting onto a vanilla subclass of NSView using NSBezierPaths. Nothing magic going on there except that I'm saving each path into an NSMutableArray so that I can check on mouseDown: to see if the user is clicking on something already drawn, and if so, it'll get drug around the screen instead of painting something else as the mouse moves. So far so good.
But what I've found is that after I've done a lot of painting on the view, I am able to mouse down somewhere off to the side, not anywhere near the bounding box of anything drawn on the screen, and for some reason, a seemingly arbitrary NSBezierPath returns YES in response to a containsPoint: query even though the mouse down point isn't even contained in the NSBezierPath's bounding box.
Below is a method that points out the unexpected behavior, and below it is some sample output that it produces. In one case, the output is what I'd expect (the mouse is at least in the bounding box when containsPoint returns YES) and one case, it isn't in the bounding box. In any event, if I replace the containsPoint: call with something to explicitly check if the mouse down has taken place in a path's bounding box as shown in the commented line of code, this unexpected behavior goes away.
Any ideas on what could be going on here? (And BTW, I do realize that the nonzero winding rule is applied by default to the containsPoint method, and so it's not truly an ideal way for determining if the user has selected an actual line on the view, nor are bounding boxes -- it's just that this strangeness has my attention at the moment and I want to either figure out that I'm doing something wrong or feel confident in filing a bug report before refining any further.)
Relevant Code:
- (BOOL) _tryToSelectLine:(NSEvent*)event
{
NSPoint loc = [self convertPoint:[event locationInWindow] fromView:nil];
NSEnumerator *enumerator = [paths objectEnumerator];
id e;
while (e = [enumerator nextObject])
{
//This check produces abnormal behavior
if ([e containsPoint:loc])
//This check behaves as I would expect it to
//if (NSPointInRect(loc, [e bounds]))
{
[selectedPath setLineWidth:DEFAULT_LINE_WIDTH];
selectedPath = e;
[e setLineWidth:SELECTED_LINE_WIDTH];
NSLog(@"mouse at (%f, %f)", loc.x, loc.y);
NSLog(@"selected path: %@", e);
NSLog(@"rect contains point: %d", NSPointInRect(loc, [e bounds]));
return YES;
}
}
return NO;
}
Sensible Output from that method:
2006-08-19 18:36:57.852 DrawingFun[16863] mouse at (172.000000, 201.000000)
2006-08-19 18:36:57.852 DrawingFun[16863] selected path: Path <003a3030>
Bounds: {{73, 145}, {359, 168}}
Control point bounds: {{73, 145}, {359, 168}}
137.000000 259.000000 moveto
139.000000 260.000000 lineto
152.000000 266.000000 lineto
180.000000 282.000000 lineto
215.000000 296.000000 lineto
253.000000 308.000000 lineto
281.000000 313.000000 lineto
335.000000 312.000000 lineto
365.000000 301.000000 lineto
386.000000 289.000000 lineto
417.000000 265.000000 lineto
426.000000 253.000000 lineto
431.000000 241.000000 lineto
432.000000 216.000000 lineto
415.000000 195.000000 lineto
378.000000 173.000000 lineto
328.000000 152.000000 lineto
252.000000 145.000000 lineto
182.000000 153.000000 lineto
112.000000 176.000000 lineto
79.000000 191.000000 lineto
74.000000 194.000000 lineto
73.000000 196.000000 lineto
73.000000 196.000000 lineto
closepath
137.000000 259.000000 moveto
2006-08-19 18:36:57.853 DrawingFun[16863] rect contains point: 1
Unexpected output from that method (because containsPoint: must have returned true for this to have been output, yet the mouse isn't even near any path's bounding box) BTW, this is the same path, it has just been translated by an NSAffineTransform.
2006-08-19 18:38:14.777 DrawingFun[16863] mouse at (81.000000, 297.000000)
2006-08-19 18:38:14.778 DrawingFun[16863] selected path: Path <003a3030>
Bounds: {{180, 227}, {359, 168}}
Control point bounds: {{180, 227}, {359, 168}}
244.000000 341.000000 moveto
246.000000 342.000000 lineto
259.000000 348.000000 lineto
287.000000 364.000000 lineto
322.000000 378.000000 lineto
360.000000 390.000000 lineto
388.000000 395.000000 lineto
442.000000 394.000000 lineto
472.000000 383.000000 lineto
493.000000 371.000000 lineto
524.000000 347.000000 lineto
533.000000 335.000000 lineto
538.000000 323.000000 lineto
539.000000 298.000000 lineto
522.000000 277.000000 lineto
485.000000 255.000000 lineto
435.000000 234.000000 lineto
359.000000 227.000000 lineto
289.000000 235.000000 lineto
219.000000 258.000000 lineto
186.000000 273.000000 lineto
181.000000 276.000000 lineto
180.000000 278.000000 lineto
180.000000 278.000000 lineto
closepath
279.000000 283.000000 moveto
2006-08-19 18:38:14.778 DrawingFun[16863] rect contains point: 0
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden