1

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

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));

任何人请帮助我。

4

2 回答 2

2

我在绘制 Haxagon 的 Hexagone thish 代码中完成了 Draw Complet

    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);
于 2013-11-06T11:41:30.783 回答
1
- (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;

}

于 2017-04-07T12:49:54.890 回答