On 1 Feb 2009, at 4:41 pm, Seth Willits wrote:



When inside of -[NSView drawRect:], what's the difference between

- (void)drawRect:(NSRect)rect;
{
     if ([self needsToDrawRect:someRect])
         ....
}


... and ...


- (void)drawRect:(NSRect)rect;
{
     if (NSIntersectsRect(rect, someRect))
         ....
}


... ?


AFAICT from the documentation, there isn't a difference. Am I misreading something important?


As others have mentioned, there is potentially a huge difference. If you invalidate a lot of small rectangles, the update region might be quite complex in shape; the <rect> passed in -drawRect: is only the bounds of this complex area.

But there is also a difference in intent between -needsToDrawRect: and -getRectsBeingDrawn:count:, even though they also appear to amount to much the same thing. The first method implies you'll be iterating a list and testing each element against the view to see if it needs to be drawn or not. In many (most?) cases that's going to work out just fine. But if you have a huge number of possible objects to draw, merely iterating the list and testing for every update could incur a noticeable performance penalty. The second method offers the possibility of using a different approach that allows you to efficiently search and locate the objects needing to be drawn without iterating the entire list to do so. (An analogy would be the difference between a linear search and a binary search). That can have great benefits (though as I say, typically only for large numbers of objects). Algorithms such as BSP, R*-Trees and so forth can be used with the second approach. My own tests indicate that the number at which a BSP storage approach starts being worth considering is around the 1000 object mark, depending on what exactly it is you're drawing. YMMV.

This is probably more than you need or care to know at this point but I thought it might be worth mentioning ;-)

--Graham


_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to