# Cartesian and Polar Coordinates in Small Basic

I introduced a Small Basic sample code about 2-dimensional vector in my last blog. A 2-D vector has two elements. A point in the plane has *x* coordinate and *y* coordinate. This kind of coordinate is called Cartesian coordinates.

Today, I'd like to introduce another coordinate system - polar coordinates. In polar coordinates, a point is represented with *r* (the length from the origin) and *θ* (the angle between the vector and the x axis). A sample program TJB764 shows both coordinates.

# Conversion from polar coordinates to Cartesian coordinates

This is easy.

Small Basic code is as follows. A variable *a* means *θ* above.

` x `` = `` r `` * ``Math``.``Cos``(``a``)`

` y `` = `` r `` * ``Math``.``Sin``(``a``)`

*Conversion from Cartesian coordinates to polar coodinates*

This is basically easy.

Small Basic code is:

` 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 `` Then ``' this condition is needed for SB 1.2`

` `` a `` = ``0`

` ``Else`

` `` a `` = ``Math``.``ArcTan``(`` y `` / ``x`` ) `` * `` 180 `` / ``Math``.``Pi`

` ``EndIf`

` `` If `` x `` < `` 0 ``Then`

` `` a `` = `` a `` + ``180`

` `` ElseIf `` x `` > `` 0 `` And `` y `` < `` 0 ``Then`

` `` a `` = `` a `` + ``360`

` ``EndIf`

`EndSub`

I already wrote about this subroutine in this blog titled Small Basic Game Programming - Game Math. But the last one causes divide by zero error in Small Basic 1.2.

Math function tan^{-1} is Math.ArcTan() operation in Small Basic. The graph of this operation is: