Move CGPathCreateMutable () so that the path remains the same?

I have 9 hexes of sprites on the screen, and I have to indicate a specific path around them to make their zone tangible. I don’t want to set the coordinates for each individual path, so I can’t use the first path (the hexagons are the same size) and move its beginning to another position (without breaking the shape)? (If I do this now, CGPathAddLineToPoint () will add the hexagon points from the previous hexagon. I hope I got an idea ... (see Figure ... NOTE: The gray octagon in the upper right corner is exactly the same size and shape as black)! [Moving coordinates and path shape] [1]

hex2TouchArea = CGPathCreateMutable ();

CGPathMoveToPoint(hex2TouchArea, NULL, 150,157); CGPathAddLineToPoint(hex2TouchArea, NULL, 130, 198); CGPathAddLineToPoint(hex2TouchArea, NULL, 146, 236); CGPathAddLineToPoint(hex2TouchArea, NULL, 195, 236); CGPathAddLineToPoint(hex2TouchArea, NULL, 218, 197); CGPathAddLineToPoint(hex2TouchArea, NULL, 193, 157); CGPathCloseSubpath(hex2TouchArea); 

here I put a picture that shows it in the image
http://666kb.com/i/bz2ysevuw8n65rh3i.gif

* EDIT: I got the solution from the post and changed it a bit: *

 -(CGMutablePathRef) drawHexagon:(CGPoint)origin { //create mutable path CGMutablePathRef path = CGPathCreateMutable(); CGPathMoveToPoint(path, nil, origin.x, origin.y); CGPoint newloc = CGPointMake(origin.x - 20, origin.y + 42); CGPathMoveToPoint(path, NULL, newloc.x, newloc.y); CGPathAddLineToPoint(path, NULL, newloc.x + 16,newloc.y + 38); CGPathAddLineToPoint(path, NULL, newloc.x + 49, newloc.y + 0); CGPathAddLineToPoint(path, NULL, newloc.x + 23, newloc.y - 39); CGPathAddLineToPoint(path, NULL, newloc.x - 25,newloc.y - 40); CGPathAddLineToPoint(path, NULL, newloc.x -43, newloc.y + 0); CGPathCloseSubpath(path); return path; } 
+4
source share
1 answer

The best way to reuse the path is probably the method for them. Make one where you add the starting coordinates, and return CGMutablePathRef so that you can draw it after the path is done.

Here's what it will look like based on the example you asked in your question:

 -(CGMutablePathRef) drawHexagon:(CGPoint)origin { //create mutable path CGMutablePathRef path = CGPathCreateMutable(); CGPathMoveToPoint(path, nil, origin.x, origin.y); CGPoint newloc = CGPointMake(origin.x - 20, origin.y + 42); CGPathMoveToPoint(path, nil, newloc.x, newloc.y); CGPoint newloc = CGPointMake(newloc.x + 16, newloc.y + 38); CGPathMoveToPoint(path, nil, newloc.x, newloc.y); CGPoint newloc = CGPointMake(newloc.x + 49, newloc.y + 0); CGPathMoveToPoint(path, nil, newloc.x, newloc.y); CGPoint newloc = CGPointMake(newloc.x + 23, newloc.y - 39); CGPathMoveToPoint(path, nil, newloc.x, newloc.y); CGPoint newloc = CGPointMake(newloc.x - 25, newloc.y - 40); CGPathMoveToPoint(path, nil, newloc.x, newloc.y); CGPoint newloc = CGPointMake(newloc.x - 43, newloc.y + 0); //which should be you origin CGPathMoveToPoint(path, nil, newloc.x, newloc.y); CGPathCloseSubpath(path); return path; } 

name it CGMutablePathRef path = [self drawHexagon:someStartingPoint];


Changed comments:

You can add the path to the context with: CGContextAddPath(context, path); Then draw it, as it seems to you, for example: CGContextDrawPath(context, kCGPathFill);

Having added the path to the context should not be difficult.

This should work for you. Good luck.

+6
source

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


All Articles