Attribute VB_Name = "Globe"
Option Explicit

Public Function DrawTangent(ByRef MyDwg As AutoCAD.AcadDocument, CCenter() As Double, EPoint() As Double, Radi As Double, Id As Integer)

Dim Cx As Double, Cy As Double, Px As Double, Py As Double
Dim X As Double, Dist As Double, Theta As Double
Dim tmpCir As AcadCircle, tmpLn As AcadLine

'Split the Indvidual Coordinates
Cx = CCenter(0)
Cy = CCenter(1)
Px = EPoint(0)
Py = EPoint(1)

'Calculate Angle At Which the Tangency Pt Lies (Relative to External Point)
Dist = Sqr(((Px - Cx) ^ 2) + ((Py - Cy) ^ 2))
X = Sqr((Dist ^ 2) - (Radi ^ 2))
Theta = Atn(Radi / X)

'Draw The Given Circle
'Draw A Line Joining The Given Point With the Circle Center
'Rotate the Line At a Relative Angle Theta
'Use the IntersectWith Function To Acheive Triming of the Tangent by Setting its EndPoint
'Note that There Only One Point is Returned  by the Function as it is a Tangent

Set tmpCir = MyDwg.ModelSpace.AddCircle(CCenter, Radi)
'Id=0 means Anticlockwise With Respect to Center
'Id=1 means Clockwise With Respect to Center

If Id = 0 Then
    Set tmpLn = MyDwg.ModelSpace.AddLine(EPoint, CCenter)
    tmpLn.Rotate EPoint, Theta
    tmpLn.EndPoint = tmpLn.IntersectWith(tmpCir, acExtendThisEntity)
ElseIf Id = 1 Then
    Set tmpLn = MyDwg.ModelSpace.AddLine(EPoint, CCenter)
    tmpLn.Rotate EPoint, -Theta
    tmpLn.EndPoint = tmpLn.IntersectWith(tmpCir, acExtendThisEntity)
End If

End Function
