2014-01-09 112 views
0

我使用如何在兩個方向上使用CGMutablePathRef繪製線條?

CGMutablePathRef path = CGPathCreateMutable(); 

for (int i = 0; i < [_points count]; i++) 
{ 
    CGPoint pt = [[_points objectAtIndex:i] CGPointValue]; 
    if (i == 0) 
    { 
     CGPathMoveToPoint(path, NULL, pt.x+1, pt.y+1); 
    } 
    else 
    { 
     CGPathAddLineToPoint(path, NULL, pt.x+1, pt.y+1); 
    } 
} 

CGContextSetLineWidth(context, 1.0f); 
CGContextSetStrokeColorWithColor(context, curveColor.CGColor); 
CGContextAddPath(context, path); 
CGContextStrokePath(context); 
CGPathRelease(path); 

我可以程度在一個方向上的線條畫線。但是我想在一定程度上延伸這兩個方向(上行和下行)。如何在兩個方向上延長線?

回答

0

在終於和@Wain評論的幫助下答案,

- (NSArray *)lineDrawingPoints:(CGFloat)slope pointX1:(CGFloat)ptX1 pointY1:(CGFloat)ptY1 maxValX:(CGFloat)biggestNumberX maxValY:(CGFloat)biggestNumberY { 

    NSMutableArray *infiniteLinePoints = [NSMutableArray array]; 
    float y; 

    for(int x = -biggestNumberX ; x <= biggestNumberX ; x++){ 
     y = slope * (x - ptX1)+ptY1; 
     if(y >= -biggestNumberY && y <= biggestNumberY) 
      [infiniteLinePoints addObject:[NSValue valueWithCGPoint:CGPointMake(x, y)]]; 
    } 

    return infiniteLinePoints; 
} 

感謝北斗星。

1

延長一端:

[_points insertObject:newStartPoint atIndex:0]; 

另:

[_points addObject:newEndPoint]; 
+0

我無法找到newStartPoint和newEndPoint ...所以我想擴展遞歸,直到點達到一定限制..如何做到這一點? – Surfer

+0

http://www.mathsisfun.com/equation_of_line.html – Wain

相關問題