Surfer
Surfer

Reputation: 1380

How to extend the line drawn using CGMutablePathRef in both direction?

I am drawing line using

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

I can extent the line in one direction. But i want to extend the line in both direction (upside and downside) to certain extent. How to extend the line in Both direction?

Upvotes: 0

Views: 502

Answers (2)

Surfer
Surfer

Reputation: 1380

At finally got the answer with the help of @Wain comment,

- (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;
}

Thanks Wain.

Upvotes: 0

Wain
Wain

Reputation: 119031

To extend one end:

[_points insertObject:newStartPoint atIndex:0];

and the other:

[_points addObject:newEndPoint];

Upvotes: 1

Related Questions