Questions regarding NSRect...In the Hillegass book we are creating an NSRect into which we are drawing an oval (NSBezierPath *). Depending on where in our view we mouse down and subsequently drag, the NSRect's size.width and/or size.height may be negative (i.e. if we start in upper right, drag lower left - both are negative). When actually drawing, does the system use our negative width and/or height to merely locate the NSPoint of where we dragged? Thus updating the NSRect? And if we ever need the size of the NSRect, should we just use the absolute values?
In the chapter, the authors used the MIN() and MAX() macros to create an NSRect. However, in the challenge solution they provide these three methods in response to mouse events:
- (void)mouseDown:(NSEvent *)theEvent
{
NSPoint pointInView = [self convertPoint:[theEvent locationInWindow] fromView:nil];
// Why do we offset by 0.5? Because lines drawn exactly on the .0 will end up spread over two pixels.
workingOval = NSMakeRect(pointInView.x + 0.5, pointInView.y + 0.5, 0, 0);
[self setNeedsDisplay:YES];
}
- (void)mouseDragged:(NSEvent *)theEvent
{
NSPoint pointInView = [self convertPoint:[theEvent locationInWindow] fromView:nil];
workingOval.size.width = pointInView.x - (workingOval.origin.x - 0.5);
workingOval.size.height = pointInView.y - (workingOval.origin.y - 0.5);
[self setNeedsDisplay:YES];
}
- (void)mouseUp:(NSEvent *)theEvent
{
[[self document] addOvalWithRect:workingOval];
workingOval = NSZeroRect; // zero rect indicates we are not presently drawing
[self setNeedsDisplay:YES];
}
This code produces a successful rectangle regardless of the potential negative values. I understand that the negative values merely reflect the shift left with respect to the origin ( the point from which we "mouse downed"). What is going on behind the scenes in properly calculating the NSPoint to which we dragged?