Share via


Small Basic Sample: Math.Sin, Math.Cos and Math.ArcTan

This article contains a sample program for Math.Sin(), Math.Cos() and Math.ArcTan() operations.

Subroutine Using Math.Sin() and Math.Cos()

Subroutine Math_PolarToCartesian() uses Math.Sin() and Math.Cos() operations to calculate Cartesian coordinate from polar coordinate.

Sub Math_PolarToCartesian
  ' Math | convert polar coordinate to Cartesian coordinate
  ' param r, a - polar Coordinate
  ' return x, y - Cartesian coordinate
  _a = Math.GetRadians(a)
  x = r  * Math.Cos(_a)
  y = r  * Math.Sin(_a)
EndSub

Subroutine Using Math.ArcTan()

Subroutine Math_CartesianToPolar() uses Math.ArcTan() operation to calculate polar coordinate from Cartesian coordinate.

Sub Math_CartesianToPolar
  ' Math | convert Cartesian coordinate to polar coordinate
  ' param x, y - Cartesian coordinate
  ' return r, a - polar Coordinate (0<=a<360)
  r = Math.SquareRoot(x * x  + y * y)
  If x =  0 And y > 0  Then
    a = 90  ' [degree]
  ElseIf x =  0 And y < 0  Then
    a = -90
  ElseIf x =  0 And y = 0  Then
    a = 0
  Else
    a = Math.ArcTan(y / x) * 180  / Math.Pi
  EndIf
  ' at this point -90<=a<=90
  If x <  0 Then
    a = a  + 180
  ElseIf x >= 0 And  y < 0 Then
    a = a  + 360
  EndIf
  ' at this point 0<=a<360
EndSub

Test Program

Program JXB040 is a test code for these subroutines above.  The usage is just moving mouse on the program window.  The top x, y are got by Mouse.X and Mouse.Y .  The second r and a are calculated with Math_CartesianToPolar().  The last x and y are calculated with Math_PolarToCartesian().

See Also