如何在ios中使用CGMutablePathRef绘制六边形?

如何在ios中使用CGMutablePathRef绘制六边形?,ios,objective-c,Ios,Objective C,我想画一个六边形,这是我的代码 int mgX = penThickness * 6; int mgY = penThickness * 2; CGPoint st = CGPointMake(MIN(startingPt.x, endingPt.x), MIN(startingPt.y, endingPt.y)); CGPoint et = CGPointMake(MAX(startingPt.x, endingPt.x),

我想画一个六边形,这是我的代码

int mgX = penThickness * 6;
int mgY = penThickness * 2;

CGPoint st = CGPointMake(MIN(startingPt.x, endingPt.x), 
                        MIN(startingPt.y, endingPt.y));
CGPoint et = CGPointMake(MAX(startingPt.x, endingPt.x), 
                        MAX(startingPt.y, endingPt.y));

CGRect outsideRect = CGRectMake(st.x,st.y, MAX(mgX, et.x-st.x), 
                        MAX(mgY, et.y-st.y));
CGRect insideRect  = CGRectInset(outsideRect, outsideRect.size.width * 0.40f, 
                        outsideRect.size.height * 0.30f);


CGPathMoveToPoint(pathRef, nil, CGRectGetMinX(outsideRect), 
                        CGRectGetMinY(outsideRect));
//0
CGPathAddLineToPoint(pathRef, nil, CGRectGetMaxX(insideRect), 
                        CGRectGetMinY(outsideRect));
//1
CGPathAddLineToPoint(pathRef, nil, CGRectGetMaxX(outsideRect), 
                        CGRectGetMidY(insideRect));
//2
CGPathAddLineToPoint(pathRef, nil, CGRectGetMaxX(insideRect), 
                        CGRectGetMaxY(outsideRect));
//3
CGPathAddLineToPoint(pathRef, nil, CGRectGetMinX(outsideRect), 
                        CGRectGetMaxY(outsideRect));
//las line
 CGPathAddLineToPoint(pathRef, nil, CGRectGetMidX(outsideRect), 
                        CGRectGetMidY(outsideRect));

任何人请帮帮我。

我有Complett Draw Complett在Hexagone中,这段代码在Draw Haxagon中工作

    int mgX = penThickness * 6;
    int mgY = penThickness * 6;

    CGPoint st = CGPointMake(MIN(startingPt.x, endingPt.x), MIN(startingPt.y, endingPt.y));
    CGPoint et = CGPointMake(MAX(startingPt.x, endingPt.x), MAX(startingPt.y, endingPt.y));

    CGRect outsideRect = CGRectMake(st.x,st.y, MAX(mgX, et.x-st.x), MAX(mgY, et.y-st.y));
    CGRect insideRect  = CGRectInset(outsideRect, outsideRect.size.width * 0.30f, outsideRect.size.height * 0.30f);

    CGPathMoveToPoint(pathRef, nil, CGRectGetMinX(insideRect), CGRectGetMinY(outsideRect));

    //0 line
    CGPathAddLineToPoint(pathRef, nil, CGRectGetMaxX(insideRect), CGRectGetMinY(outsideRect));


    //1 line
    CGPathAddLineToPoint(pathRef, nil, CGRectGetMaxX(outsideRect), CGRectGetMidY(insideRect));

    //2 line
     CGPathAddLineToPoint(pathRef, nil, CGRectGetMaxX(insideRect), CGRectGetMaxY(outsideRect));


    //3 line
     CGPathAddLineToPoint(pathRef, nil, CGRectGetMinX(insideRect), CGRectGetMaxY(outsideRect));

    //4 line

    CGPathAddLineToPoint(pathRef, nil, CGRectGetMinX(outsideRect), CGRectGetMidY(insideRect));

    CGPathCloseSubpath(pathRef);
-(CGPathRef)roundedPolygonPathWithRect:(CGRect)rect线宽:(CGFloat)线宽边:(NSInteger)边角半径:(CGFloat)角半径{
/*
//根据参考系,将您的poligon居中
rect=CGRectMake(rect.origin.x-(rect.size.width-lineWidth)/2.0,
矩形原点y-(矩形尺寸高度-线宽)/2.0,
rect.size.width,
矩形尺寸、高度);
*/
CGMutablePathRef pathRef=CGPathCreateMutable();
CGFloat theta=2.0*M_PI/sides;//在每个拐角处转弯多少
//CGFloat offset=cornerRadius*tanf(θ/2.0);//开始圆角的偏移量
CGFloat width=MIN(rect.size.width,rect.size.height);//正方形的宽度
//计算中心
CGPoint center=CGPointMake(rect.origin.x+width/2.0,rect.origin.y+width/2.0);
CGFloat半径=(宽度-线宽+拐角半径-(cos(θ)*拐角半径))/2.0;
//从一个点开始绘图,默认情况下,该点位于右侧边缘
//但是可以抵消
CGFloat角度=M_PI/2;
CGPoint corner=CGPointMake(中心x+(半径-角半径)*cos(角度),中心y+(半径-角半径)*sin(角度));
CGPathMoveToPoint(路径参考,零,角点.x+角点半径*cos(角度+θ),角点.y+角点半径*sin(角度+θ));
//绘制多边形的边和圆角
对于(NSInteger side=0;side

}

下面的链接解释了如何使用CGMutablePathRef绘制六边形:
- (CGPathRef)roundedPolygonPathWithRect:(CGRect)rect lineWidth:(CGFloat)lineWidth sides:(NSInteger)sides cornerRadius:(CGFloat)cornerRadius {

/*
//Center your poligon, depends from reference system
rect = CGRectMake(rect.origin.x - (rect.size.width - lineWidth)/2.0,
                  rect.origin.y - (rect.size.height - lineWidth)/2.0,
                  rect.size.width,
                  rect.size.height);
 */

CGMutablePathRef pathRef  = CGPathCreateMutable();

CGFloat theta       = 2.0 * M_PI / sides;                           // how much to turn at every corner
//CGFloat offset      = cornerRadius * tanf(theta / 2.0);             // offset from which to start rounding corners
CGFloat width = MIN(rect.size.width, rect.size.height);   // width of the square

// Calculate Center
CGPoint center = CGPointMake(rect.origin.x + width / 2.0, rect.origin.y + width / 2.0);
CGFloat radius = (width - lineWidth + cornerRadius - (cos(theta) * cornerRadius)) / 2.0;

// Start drawing at a point, which by default is at the right hand edge
// but can be offset
CGFloat angle = M_PI / 2;

CGPoint corner = CGPointMake(center.x + (radius - cornerRadius) * cos(angle), center.y + (radius - cornerRadius) * sin(angle));
CGPathMoveToPoint(pathRef, nil, corner.x + cornerRadius * cos(angle + theta), corner.y + cornerRadius * sin(angle + theta));

// draw the sides and rounded corners of the polygon
for (NSInteger side = 0; side < sides; side++) {

    angle += theta;

    CGPoint corner = CGPointMake(center.x + (radius - cornerRadius) * cos(angle), center.y + (radius - cornerRadius) * sin(angle));
    CGPoint tip = CGPointMake(center.x + radius * cos(angle), center.y + radius * sin(angle));
    CGPoint start = CGPointMake(corner.x + cornerRadius * cos(angle - theta), corner.y + cornerRadius * sin(angle - theta));
    CGPoint end = CGPointMake(corner.x + cornerRadius * cos(angle + theta), corner.y + cornerRadius * sin(angle + theta));

    //[path addLineToPoint:start];
    CGPathAddLineToPoint(pathRef, nil, start.x, start.y);

    //[path addQuadCurveToPoint:end controlPoint:tip];
    CGPathAddQuadCurveToPoint(pathRef, nil, tip.x, tip.y, end.x, end.y);
}

CGPathCloseSubpath(pathRef);

return pathRef;