Attribute VB_Name = "hlp_iTan"
Option Explicit
Option Base 0

Public Function iTan(ByVal X As Single, ByVal Y As Single, ByVal XYCenter As Single, ByRef Radius As Single) As Single
  Dim xLong1 As Long
  'Dim xLong2 As Long
  'Dim xLong3 As Long
  Dim xSingle1 As Single
  Dim xSingle2 As Single
  Dim Proportion As Single
  
'  Stop
  'Proportion = (X - XYCenter) / Sqr((Y - XYCenter) ^ 2 + (X - XYCenter))
  If X - XYCenter = 0& Then
    If Y - XYCenter = 0 Then
      iTan = 0 / 0
      Else
      If Y > XYCenter Then
        Radius = (Y - XYCenter)
        'ITan = 0 '180 / (180 / 3.1415926536)
        Else
        Radius = -(Y - XYCenter)
        iTan = 180 / (180 / 3.1415926536)
        End If
      End If
    ElseIf Y - XYCenter = 0& Then
    If X > XYCenter Then
      Radius = (X - XYCenter)
      iTan = 90 / (180 / 3.1415926536)
      Else
      Radius = -(X - XYCenter)
      iTan = 270 / (180 / 3.1415926536)
      End If
    Else
    Proportion = Abs(X - XYCenter) / Abs(Y - XYCenter)
    If Tan(1) < Proportion Then
      Select Case Proportion
        Case Is < Tan(1.1)
          iTan = 1
        Case Is < Tan(1.2)
          iTan = 1.1
        Case Is < Tan(1.3)
          iTan = 1.2
        Case Is < Tan(1.4)
          iTan = 1.3
        Case Is < Tan(1.5)
          iTan = 1.4
        Case Else
          iTan = 1.5
        End Select
      xLong1 = 2&
      Else
      xLong1 = 1&
      'Stop
      End If
    For xLong1 = xLong1 To 10& 'decimals
      xSingle1 = iTan + 6 * (0.1 ^ xLong1)
      xSingle2 = Tan(xSingle1)
      If xSingle2 < Proportion Then
        xSingle1 = iTan + 8 * (0.1 ^ xLong1)
        xSingle2 = Tan(xSingle1)
        If xSingle2 < Proportion Then
          xSingle1 = iTan + 9 * (0.1 ^ xLong1)
          xSingle2 = Tan(xSingle1)
          If xSingle2 < Proportion Then
            iTan = iTan + 9 * (0.1 ^ xLong1)
            ElseIf xSingle2 > Proportion Then
            iTan = iTan + 8 * (0.1 ^ xLong1)
            Else
            iTan = xSingle1
            Exit For
            End If
          ElseIf xSingle2 > Proportion Then
          xSingle1 = iTan + 7 * (0.1 ^ xLong1)
          xSingle2 = Tan(xSingle1)
          If xSingle2 < Proportion Then
            iTan = iTan + 7 * (0.1 ^ xLong1)
            ElseIf xSingle2 > Proportion Then
            iTan = iTan + 6 * (0.1 ^ xLong1)
            Else
            iTan = xSingle1
            Exit For
            End If
          Else
          iTan = xSingle1
          Exit For
          End If
          
        ElseIf xSingle2 > Proportion Then
        xSingle1 = iTan + 4 * (0.1 ^ xLong1)
        xSingle2 = Tan(xSingle1)
        If xSingle2 < Proportion Then
          xSingle1 = iTan + 5 * (0.1 ^ xLong1)
          xSingle2 = Tan(xSingle1)
          If xSingle2 < Proportion Then
            iTan = iTan + 5 * (0.1 ^ xLong1)
            ElseIf xSingle2 > Proportion Then
            iTan = iTan + 4 * (0.1 ^ xLong1)
            Else
            iTan = xSingle1
            Exit For
            End If
          ElseIf xSingle2 > Proportion Then
          xSingle1 = iTan + 2 * (0.1 ^ xLong1)
          xSingle2 = Tan(xSingle1)
          If xSingle2 < Proportion Then
            xSingle1 = iTan + 3 * (0.1 ^ xLong1)
            xSingle2 = Tan(xSingle1)
            If xSingle2 < Proportion Then
              iTan = iTan + 3 * (0.1 ^ xLong1)
              ElseIf xSingle2 > Proportion Then
              iTan = iTan + 2 * (0.1 ^ xLong1)
              Else
              iTan = xSingle1
              Exit For
              End If
            ElseIf xSingle2 > Proportion Then
            xSingle1 = iTan + 1 * (0.1 ^ xLong1)
            xSingle2 = Tan(xSingle1)
'            Stop
            If xSingle2 < Proportion Then
              iTan = iTan + 1 * (0.1 ^ xLong1)
              ElseIf xSingle2 > Proportion Then
              iTan = iTan + 0 * (0.1 ^ xLong1)
              Else
              iTan = xSingle1
              Exit For
              End If
            Else
            iTan = xSingle1
            Exit For
            End If
          
          
          
          
          Else
          iTan = xSingle1
          Exit For
          End If
        Else
        iTan = xSingle1
        Exit For
        End If
      Next
    'Debug.Print ITan, X, Y
    If X < XYCenter Then
      If Y < XYCenter Then
        'Debug.Print "Mod"
        iTan = 180 / (180 / 3.1415926536) + iTan
        Else
        iTan = 360 / (180 / 3.1415926536) - iTan
        End If
      ElseIf Y < XYCenter Then
      'Debug.Print , 180 / (180 / 3.1415926536) - ITan, 180 / (180 * 3.1415926536), -ITan
      iTan = 180 / (180 / 3.1415926536) - iTan
      'Debug.Print , ITan
      End If
    'Stop
    Radius = Sqr((X - XYCenter) ^ 2 + Abs(Y - XYCenter) ^ 2)
    End If
  'Debug.Print ITan, Radius
  End Function

