Skip to content

Instantly share code, notes, and snippets.

@nonkit
Last active April 14, 2020 08:39
Show Gist options
  • Save nonkit/69803f55bc4b871665d3aa75789d6804 to your computer and use it in GitHub Desktop.
Save nonkit/69803f55bc4b871665d3aa75789d6804 to your computer and use it in GitHub Desktop.
Small Basic Pseudo Math Object
Sub Math_CartesianToPolar
' Math | convert Cartesian coodinate 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
Sub Math_Dec2Hex
' Math | convert decimal to hexadecimal
' param dec - decimal number
' returns hex - hexadecimal text
Stack.PushValue("local", dec)
hex = ""
While 0 < dec
digit = Math.Remainder(dec, 16)
dec = Math.Floor(dec / 16)
hex = Text.Append(Text.GetSubText("0123456789ABCDEF", digit + 1, 1), hex)
EndWhile
If hex = "" Then
hex = "0"
EndIf
dec = Stack.PopValue("local")
EndSub
Sub Math_Hex2Dec
' Math | convert hexadecimal to decimal
' param hex
' returns dec
dec = 0
len = Text.GetLength(hex)
For ptr = 1 To len
dec = dec * 16 + Text.GetIndexOf("123456789ABCDEF", Text.GetSubText(hex, ptr, 1))
EndFor
EndSub
@nonkit
Copy link
Author

nonkit commented Apr 1, 2020

Following code in Math_CartesianToPolar did not needed in SB 1.0 because SB 1.0 will not occur zero divided error.

  ElseIf x = 0 And y = 0 Then
    a = 0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment