I am creating a CGPath
to define an area in my game like this:
CGPathMoveToPoint ( myPath, NULL, center.x, center.y );
CGPathAddLineToPoint( myPath, NULL,center.x + 100, center.y);
CGPathAddLineToPoint( myPath, NULL, center.x + 100, center.y + 100);
CGPathAddLineToPoint( myPath, NULL, center.x, center.y + 100);
CGPathCloseSubpath ( myPath );
I know this is just a square and that I could just use another CGRect
but the path I wish to actually create is not actually a rectangle (I am just testing at the moment). And then detecting the touch area simply with a:
if (CGPathContainsPoint(myPath, nil, location, YES))
This all works fine, the problem is that the CGPath
may be moving up to 40 times per second. How can I move it without having to create a new one? I know I can do something like this to "move" it:
center.y += x;
CGPathRelease(myPath);
myPath = CGPathCreateMutable();
CGPathMoveToPoint ( myPath, NULL, center.x, center.y );
CGPathAddLineToPoint( myPath, NULL,center.x + 100, center.y);
CGPathAddLineToPoint( myPath, NULL, center.x + 100, center.y + 100);
CGPathAddLineToPoint( myPath, NULL, center.x, center.y + 100);
CGPathCloseSubpath ( myPath );
But I have to release and create a new path up to 40 times per second, which I think might have a performance penalty; is this true?
I would like to be able to move it just like I am currently moving some CGRects
by simply setting the origin to a different value, is this possible with CGPath
?
Thank you.
EDIT: I forgot to mention I don't have a GraphicsContext as I am not drawing on a UIView
.
Applying a transformation to the CGPath and test against a point, is equivalent to applying the inverse transformation to the point.
Therefore, you can use
CGPoint adjusted_point = CGPointMake(location.x - center.x, location.y - center.y);
if (CGPathContainsPoint(myPath, NULL, adjusted_point, YES))
But CGPathContainsPoint
already takes a CGAffineTransform
parameter (which you have NULL
-ed it), so you can also use
CGAffineTransform transf = CGAffineTransformMakeTranslation(-center.x, -center.y);
if (CGPathContainsPoint(myPath, &transf, location, YES))
If you are drawing, instead of changing the path, you can change the CTM in your drawing code directly.
CGContextSaveGState(c);
CGContextTranslateCTM(c, center.x, center.y);
// draw your path
CGContextRestoreGState(c);
Use a CAShapeLayer
if you need performance.