2013-11-01 53 views
1

我想畫一個六邊形,這是我的代碼如何在ios中使用CGMutablePathRef繪製Hexagon?

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

有人請幫助我。

回答

2

我有完滿成功繪製複合劑在Hexagone的thish代碼平局工作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); 
0
- (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; 

}

相關問題