How can I move CGPath without creating a new one

I create CGPathto define the area in my game as follows:

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 I could just use a different one CGRect, but the path I want to create is not really a rectangle (I'm just testing now). And then detecting the touch area simply with:

if (CGPathContainsPoint(myPath, nil, location, YES))

Everything works fine, the problem is that it CGPathcan move up to 40 times per second. How can I move it without creating a new one? I know that 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 free and create a new path up to 40 times per second, which, I think, may have a performance limit; it's true?

, CGRects, , CGPath?

.

EDIT: , GraphicsContext, UIView.

+3
2

CGPath .

CGPoint adjusted_point = CGPointMake(location.x - center.x, location.y - center.y);
if (CGPathContainsPoint(myPath, NULL, adjusted_point, YES)) 

CGPathContainsPoint CGAffineTransform ( NULL -ed it),

CGAffineTransform transf = CGAffineTransformMakeTranslation(-center.x, -center.y);
if (CGPathContainsPoint(myPath, &transf, location, YES)) 

, , CTM .

CGContextSaveGState(c);
CGContextTranslateCTM(c, center.x, center.y);
// draw your path
CGContextRestoreGState(c);

CAShapeLayer, .

+5

@KennyTM OP, :

CGPath ?

, :

UIBezierPath* path = [UIBezierPath bezierPathWithCGPath:cgPath];
[path applyTransform:CGAffineTransformMakeTranslation(2.f, 0.f)];
+2

Source: https://habr.com/ru/post/1732604/


All Articles