|Title||Find the tangent lines between a point and a circle in Visual Basic .NET|
|Description||This example shows how to find the tangent lines between a point and a circle in Visual Basic .NET.|
|Keywords||tangents, find tangents, find tangent lines, tangent lines, Visual Basic .NET, VB .NET, graphics, algorithms|
|Categories||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
' Return true if we find the tangents, false if the point is
' inside the circle.
Private Function FindTangents(ByVal center As PointF, ByVal _
radius As Single, _
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
If (D_squared < radius * radius) Then
pt1 = New PointF(-1, -1)
pt2 = New PointF(-1, -1)
' Find the distance from the external point
' to the tangent points.
Dim L As Double = Sqrt(D_squared - radius * radius)
' Find the points of intersection between
' the original circle and the circle with
' center external_point and radius dist.
center.X, center.Y, radius, _
external_point.X, external_point.Y, CSng(L), _
The code calculates the distance D squared. It uses that to calculate L and then calls FindCircleCircleIntersections to find the intersections between the two circles.