Re: Appending CGPath to CGPath
Re: Appending CGPath to CGPath
- Subject: Re: Appending CGPath to CGPath
- From: Graham Cox <email@hidden>
- Date: Sat, 16 Apr 2011 14:23:54 +1000
On 16/04/2011, at 1:55 PM, Roland King wrote:
> I have a CGPath which represents a small (but somewhat complicated) shape and I want to append a number of them together, connected with straight lines, to form a complete closed shape which I then want to use as a clip region. I thought that CGPathAddPath would do what I wanted as I can easily give it an affine transform matrix to move and rotate the template path and add it several times, however if I understand the documentation correctly, that adds the path as a new subpath, not connecting it up. As I want to use the final path as a clip region that's not going to work, I need one continuous path.
>
> Is there a way to append two CGPaths together such that they really connect up to be one path? I can think of a way to do this with CGPathApply but it seems a bit hokey.
>
> The motivation is that the small, complicated, oft-repeated path is pretty slow to draw, it has a number of connected arcs and I'm drawing several thousand of them when all I really need to do is draw it once, and then add it translated and rotated, over and over again and I can construct the transformation matrices very quickly. And yes that is measured performance, not premature optimization.
Taking your last paragraph first, I think you are deluding yourself regarding the performance gain you'll get. A transform doesn't magically avoid the need to iterate over all the path's points, it merely moves all its points elsewhere, and draws it again. It's certainly much faster than recomputing the path, but drawing? No, it won't help you there. Combining the paths into one may help, because the test for self-intersection and so on only needs to be performed once, but then again, each added path adds points which slows that down. Usually you find that combining paths gains performance up to a point, then starts to get slower again.
Extending a path without adding a subpath will need to be done by copying the existing path elements to a new path, removing any ending close path and/or new moveto, removing the initial move to on the added path, and adding the elements of the second path. You can only do this using CGPathApply. NSBezierPath makes it a bit easier to iterate the path elements but the basic process is the same.
For a clipping path, adding subpaths is likely to give you the same result anyway (subject to the winding rule you use), so CGPathAddPath is the way to go, IMO. If you can cache your drawing to an image of some kind (CGLayer?) that is likely to speed things up far more, if you can handle the resolution issues.
--Graham
_______________________________________________
Cocoa-dev mailing list (email@hidden)
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:
This email sent to email@hidden