Home Search   What's New Index Books Links Q & A Newsletter Banners   Feedback Tip Jar MSDN Visual Basic Community

Title Find the tangent lines between a point and a circle in Visual Basic .NET This example shows how to find the tangent lines between a point and a circle in Visual Basic .NET. tangents, find tangents, find tangent lines, tangent lines, Visual Basic .NET, VB .NET, graphics, algorithms Graphics, Algorithms, VB.NET

This isn't too hard if you're familiar with the example Determine where two circles intersect in Visual Basic .NET.

Consider the figure. R is the radius of the circle. You can easily calculate the distance D between the external point and the circle's radius by using the Pythagorean theorem. If the point P is (Px, Py) and the circle's center C is (Cx, Cy), then .

The tangent meets the circle's radius at a 90 degree angle so you can use the Pythagorean theorem again to find .

Believe it or not, you're now done because the tangent points P0 and P1 are the the points of intersection between the original circle and the circle with center P and radius L. Simply use the code from the example Determine where two circles intersect in Visual Basic .NET to find those points.

The following code shows how the FindTangents method used by the example program finds the tangent points.

```' Find the tangent points for this circle and external
' point.
' Return true if we find the tangents, false if the point is
' inside the circle.
Private Function FindTangents(ByVal center As PointF, ByVal _
ByVal external_point As PointF, ByRef pt1 As PointF, _
ByRef pt2 As PointF) As Boolean

' Find the distance squared from the
' external point to the circle's center.
Dim dx As Double = center.X - external_point.X
Dim dy As Double = center.Y - external_point.Y
Dim D_squared As Double = dx * dx + dy * dy
pt1 = New PointF(-1, -1)
pt2 = New PointF(-1, -1)
Return False
End If

' Find the distance from the external point
' to the tangent points.

' Find the points of intersection between
' the original circle and the circle with
' center external_point and radius dist.
FindCircleCircleIntersections( _