Microsoft Small Basic

Program Listing: JXB040-0
' SmallBasic Version 1.2
' Program: CartesianPolar2D
' Changelog:
' Author: Nonki Takahashi (mod Pappa Lapub)
' Website: https://social.msdn.microsoft.com/Forums/en-US/95706095-0984-40d7-897f-0ec14ed58039/small-basic-subroutine-library-project
' ImportURL: http://smallbasic.com/program/?JXB040-0
' Extension:
' Comment: https://social.technet.microsoft.com/wiki/contents/articles/37371.small-basic-sample-math-sin-math-cos-and-math-tan.aspx
'
' Variables:
' ToDo:
' ================================================================================
' Math_CartesianToPolar Test Program
' Version 0.2
' Copyright © 2016-2017 Nonki Takahashi. The MIT License.

gw = 598
gh = 428
GraphicsWindow.Width = gw
GraphicsWindow.Height = gh
GraphicsWindow.FontBold = ""
GraphicsWindow.FontSize = 10
GraphicsWindow.FontName = "Courier New"
GraphicsWindow.Title = "Cartesian-Polar 2D"

color = "LightGray"
dx = 10
dy = 10
x1 = Math.Remainder(gw/2, dx) - dx
x2 = gw - x1
y1 = Math.Remainder(gh/2, dy) - dy
y2 = gh - y1
DrawGrid()

color = "Gray"
dx = 100
dy = 100
x1 = Math.Remainder(gw/2, dx) - dx
x2 = gw - x1
y1 = Math.Remainder(gh/2, dy) - dy
y2 = gh - y1
DrawGrid()
GraphicsWindow.PenColor = "Red"

GraphicsWindow.BrushColor = "Black"
x0 = gw/2
y0 = gh/2
x0Text = Shapes.AddText("x0= 0")
Shapes.Move(x0Text, 10,10)
y0Text = Shapes.AddText("y0= 0")
Shapes.Move(y0Text, 10,20)
xText = Shapes.AddText("")
Shapes.Move(xText, 10,30)
yText = Shapes.AddText("")
Shapes.Move(yText, 10,40)
rText = Shapes.AddText("")
Shapes.Move(rText, 10,60)
aText = Shapes.AddText("")
Shapes.Move(aText, 10,70)
x2Text = Shapes.AddText("")
Shapes.Move(x2Text, 10,80)
y2Text = Shapes.AddText("")
Shapes.Move(y2Text, 10,90)

GraphicsWindow.MouseMove = OnMouseMove
GraphicsWindow.KeyDown = OnKeyDown


' ////////// EVENTs \\\\\\\\\\
Sub OnMouseMove
mx = GraphicsWindow.MouseX
my = GraphicsWindow.MouseY
x = mx - x0
y = y0 - my

Math_CartesianToPolar()
Shapes.SetText(xText, "x1= "+ x)
Shapes.SetText(yText, "y1= "+ y)
Shapes.SetText(aText, "a = "+ a +"°")
Shapes.SetText(rText, "r = "+ r)

Math_PolarToCartesian()
Shapes.SetText(x2Text, "x2= "+ x)
Shapes.SetText(y2Text, "y2= "+ y)

If mx <> mxLast Or my <> myLast Then
If line <> "" Then
Shapes.Remove(line)
EndIf
line = Shapes.AddLine(x0,y0, mx,my)
mxLast = mx
myLast = my
EndIf
EndSub

Sub OnKeyDown
If GraphicsWindow.LastKey = "Escape" Then
Program.End()
EndIf
EndSub


' ////////// SUBs \\\\\\\\\\
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 counterclockwise

'a = Math.Remainder(360 - a, 360) ' clockwise
a = 0.01 * Math.Round(100*a)
r = 0.01 * Math.Round(100*r)
EndSub

Sub Math_PolarToCartesian
' Math | convert polar coodinate 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)

x = 0.01 * Math.Round(100*x)
y = 0.01 * Math.Round(100*y)
EndSub

Sub DrawGrid
' param x1, x2 - x range
' param dx - delta x
' param y1, y2 - y range
' param dy - delta y
' param color - color for grid
GraphicsWindow.PenColor = color
For x = x1 To x2 Step dx
GraphicsWindow.DrawLine(x,y1, x,y2)
EndFor
For y = y1 To y2 Step dy
GraphicsWindow.DrawLine(x1,y, x2,y)
EndFor
EndSub