views:

168

answers:

1

I'm drawing a big scheme that consist of a lot of lines. I do it in the drawRect: method of UIView. The scheme is larger than the layer of view and I check each line and draw it only if it intersects the visible rect. But at one moment I thought, should I do this? Maybe Quartz is already doing this test?

So the question is: When I use function CGContextAddLineToPoint() does the Core Graphics tests this line for intersection with layer rect or it just draw it anyway?

A: 

When I use function CGContextAddLineToPoint() does the Core Graphics tests this line for intersection with layer rect or it just draw it anyway?

No, it will not suppress the lineto, because that's just one segment in a subpath. A subpath may intersect the rect even if one of its line segments does not. You may be drawing only one line per subpath, but Quartz doesn't know that until you moveto for a new subpath.

As for whether you should test the lines' intersection of the rect, the documentation says:

Views that implement a drawRect: method should always check the rectangle passed to the method and use it to limit the scope of their drawing operations. Because drawing is a relatively expensive operation, limiting drawing in this way is a good way to improve performance.

I think we can reasonably interpret this to mean that you should, indeed, check whether the lines intersect the rect and skip those that don't.

Peter Hosey