Question

I have got 9 hexagon sprites across the screen and I have to specify a certain path around them to make their area touchable. I don't want to set the coordinates for every single path respectively, so can't I just use the first path (the hexagons are all the same size) and move its origin to another position (without destroying the shape)? (If i do it now, the CGPathAddLineToPoint(); adds the points of the hexagon from the previous hexagon. I hope I got the idea across ... (see picture... NOTE : The grey octagon on the upper right is the exact same size and shape as the black one)![Move path coordinates and 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 an image
http://666kb.com/i/bz2ysevuw8n65rh3i.gif

*EDIT: I got the solution from the post and changed it a little 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;   

}
Was it helpful?

Solution

The best way of reusing a path is probably make a method for them. Make one where you add the start coordinates and return a CGMutablePathRef so you can draw it after the path is done.

Here is what is would look like based on the example path you put 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;   
}

call it with CGMutablePathRef path = [self drawHexagon:someStartingPoint];


Editted due comments:

You can add the path to the context with: CGContextAddPath(context, path); Then draw it however you feel like, for example like this: CGContextDrawPath(context, kCGPathFill);

It shouldn't be hard after you added the path to the context.

This should work for you. Good luck.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top