36. Circle-line tangents

The following diagram shows a circle and point P with their two tangent lines:

You can calculate the distance between the point P and the circle's center C by using the distance formula. If the point P is at (Px, Py) and the circle's center C is at (Cx, Cy), then the following formula gives the distance between the two points:

A tangent line meets the circle's radius at a 90°. We know D and R, so we can use the Pythagorean theorem to find L:

At this point, you're basically done. The points p0 and p1 are the distance L away from the point P, so they lie on a circle of radius L centered at point P. They also lie on the original circle. Now, you can use the techniques described in Solution 35. Circle-circle intersection, to find the points where those two circles intersect, and that gives you the points p0 and p1.

The following FindTangentPoints method finds the tangent points between a point and a circle:

// Find the tangent points for this circle and external point.
private List<PointF> FindLineCircleTangents(PointF center,
float radius, PointF point)
{
// Find the distance between center and point.
float D = Distance(center, point);
if (D <= radius) return new List<PointF>();

// Find the distance from point to the tangent points.
float L = (float)Math.Sqrt(D * D - radius * radius);

// Find the points of intersection between the original circle
// and the circle with center point and radius L.
return FindCircleCircleIntersections(center, radius, point, L);
}

This method calculates the distances D and L shown in the preceding diagram. It then calls the FindCircleCircleIntersections method to find the intersections of the two circles described earlier.

Note that this version of FindCircleCircleIntersections has been modified to take the circles' centers and radii as parameters. Download the CircleLineTangents example solution to see the new version of the method and other details.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset