views:

330

answers:

1

Hello,

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.

-Oscar

EDIT: I forgot to mention I don't have a GraphicsContext as I am not drawing on a UIView.

+2  A: 

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.

KennyTM
Wouldn't that move the whole graphic context and not just the path?. Also I forgot to mention I do not have a graphicscontext as I am not drawing on a UIView
OscarMk
@OscarMk: Oops. Sorry. See update.
KennyTM
Thank you that will help me a lot!. Instead of having to move all my polys I can just translate my points, I should have thought about that before. Just one more question in performance what is better to create a new cgpoint or to use the CGAffine transform?
OscarMk
I think create a new CGPoint is faster as applying the transform involves extra multiplications.
KennyTM
The CGAffineTransformMakeTranslation is not being applied to the point for some reason...
OscarMk