Surfer
Surfer

Reputation: 1380

How to find Intersection of curve and line?

I am drawing the curve and line by giving 11 points.Is it possible to detect the point of intersection of two lines (or) two curves (or) one line and one curve.

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

Curve Draw

CGMutablePathRef path = CGPathCreateMutable();

for (int i = 0; i < [_points count]; i++)
{
    CGPoint pt = [[_points objectAtIndex:i] CGPointValue];

    NSLog(@"%@",NSStringFromCGPoint(pt));

    if (i == 0)
    {
        CGPathMoveToPoint(path, NULL, pt.x, pt.y);
    }
    else
    {
        CGPoint curP = [[_points objectAtIndex:i-1] CGPointValue];
        float delta = 1.0f;
        for (float pointX = curP.x; fabs(pointX - pt.x) > 1e-5f; pointX += delta)
        {
            float pointY = func(i-1, pointX);
            CGPathAddLineToPoint(path, NULL, pointX, pointY);
        }
    }
CGContextSetLineWidth(context, 1.0f);
CGContextSetStrokeColorWithColor(context, curveColor.CGColor);
CGContextAddPath(context, path);
CGContextStrokePath(context);
CGPathRelease(path); 

By using these codes how to find the intersection points.

Upvotes: 2

Views: 1459

Answers (1)

Claudiu
Claudiu

Reputation: 229341

  • To find the intersection point of two lines, see this answer.
  • To intersect a line and a curve - well your curve is just a set of lines, so the line intersects the set of lines if the line intersects any one of the lines in the set.
  • To intersect a curve and a curve - check each line in one curve against each line in the other.

And there are various ways to optimize like colliding the bounding rectangles of the curves or lines first.

Upvotes: 3

Related Questions