|
|
Title | Find the tangent lines between a point and a circle in Visual Basic 6 |
Description | This example shows how to find the tangent lines between a point and a circle in Visual Basic 6. |
Keywords | tangents, find tangents, find tangent lines, tangent lines, Visual Basic 6, VB 6, graphics, algorithms |
Categories | Graphics, Algorithms |
|
|
This isn't too hard if you're familiar with the example Determine where two circles intersect in Visual Basic 6.
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 6 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 cx As Single, ByVal cy _
As Single, _
ByVal radius As Single, ByVal px As Single, ByVal py As _
Single, _
ByRef px1 As Single, ByRef py1 As Single, _
ByRef px2 As Single, ByRef py2 As Single) As Boolean
Dim dx As Double
Dim dy As Double
Dim D_squared As Double
Dim L As Double
' Find the distance squared from the
' external point to the circle's center.
dx = cx - px
dy = cy - py
D_squared = dx * dx + dy * dy
If (D_squared < radius * radius) Then
px1 = -1
FindTangents = False
Exit Function
End If
' Find the distance from the external point
' to the tangent points.
L = Sqr(D_squared - radius * radius)
' Find the points of intersection between
' the original circle and the circle with
' center external_point and radius dist.
FindCircleCircleIntersections _
cx, cy, radius, _
px, py, CSng(L), _
px1, py1, px2, py2
FindTangents = True
End Function
|
|
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.
|
|
|
|
|
|