Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Line Segment and Circle Intersection [closed]

I have a Line Segment (x1, y1, x2, y2) intersecting a circle of radius r. How can I determine which intersection point is closest to (x1, y1)?

circle-line

like image 680
Vali Rosca Avatar asked Apr 11 '14 15:04

Vali Rosca


People also ask

What is the intersection of a circle and a line segment?

In geometry, a line meeting a circle in exactly one point is known as a tangent line, while a line meeting a circle in exactly two points in known as a secant line (Rhoad et al. 1984, p. 429).

How do you know if a line segment intersects a circle?

To determine the position of a line with respect to a circle, all we need to do is find its distance from the center of the circle, and compare it with its radius. Then, if the distance is less than the radius, the line must intersect the circle at two distinct points.

What are the 3 segments that intersect a circle given its definition?

A chord is a segment whose endpoints are on a circle. A diameter is a chord that contains the center of the circle. A secant is a line that intersects a circle in two points. A tangent is a line in the plane of a circle that intersects the circle in exactly one point, the point of tangency.

Can the intersection of two lines be a line segment?

Two Intersecting line segments can only have one point of intersection. No matter what the angle is, two straight lines cannot meet at more than one point. Two intersecting lines cannot be parallel to each other. The line segments that meet at more than one point do not usually have a straight path.


1 Answers

To do that first find the intersection points with the circle and then take the closest one to the line start point

So Check this code

//cx,cy is center point of the circle 
public PointF ClosestIntersection(float cx, float cy, float radius,
                                  PointF lineStart, PointF lineEnd)
{
    PointF intersection1;
    PointF intersection2;
    int intersections = FindLineCircleIntersections(cx, cy, radius, lineStart, lineEnd, out intersection1, out intersection2);

    if (intersections == 1)
        return intersection1; // one intersection

    if (intersections == 2)
    {
        double dist1 = Distance(intersection1, lineStart);
        double dist2 = Distance(intersection2, lineStart);

        if (dist1 < dist2)
            return intersection1;
        else
            return intersection2;
    }

    return PointF.Empty; // no intersections at all
}

private double Distance(PointF p1, PointF p2)
{
    return Math.Sqrt(Math.Pow(p2.X - p1.X, 2) + Math.Pow(p2.Y - p1.Y, 2));
}

// Find the points of intersection.
private int FindLineCircleIntersections(float cx, float cy, float radius,
                                        PointF point1, PointF point2, out               
                                        PointF intersection1, out PointF intersection2)
{
    float dx, dy, A, B, C, det, t;

    dx = point2.X - point1.X;
    dy = point2.Y - point1.Y;

    A = dx * dx + dy * dy;
    B = 2 * (dx * (point1.X - cx) + dy * (point1.Y - cy));
    C = (point1.X - cx) * (point1.X - cx) + (point1.Y - cy) * (point1.Y - cy) - radius * radius;

    det = B * B - 4 * A * C;
    if ((A <= 0.0000001) || (det < 0))
    {
        // No real solutions.
        intersection1 = new PointF(float.NaN, float.NaN);
        intersection2 = new PointF(float.NaN, float.NaN);
        return 0;
    }
    else if (det == 0)
    {
        // One solution.
        t = -B / (2 * A);
        intersection1 = new PointF(point1.X + t * dx, point1.Y + t * dy);
        intersection2 = new PointF(float.NaN, float.NaN);
        return 1;
    }
    else
    {
        // Two solutions.
        t = (float)((-B + Math.Sqrt(det)) / (2 * A));
        intersection1 = new PointF(point1.X + t * dx, point1.Y + t * dy);
        t = (float)((-B - Math.Sqrt(det)) / (2 * A));
        intersection2 = new PointF(point1.X + t * dx, point1.Y + t * dy);
        return 2;
    }
}

Intersection Code form here LINK

like image 121
yazan Avatar answered Oct 01 '22 07:10

yazan