Resources For IT Professionals

# Small Basic: How to Use Trigonometric Functions

Let's imagine to draw arc.
Assume center of the arc as x = 200, y = 300, start angle a1 = -30 and end angle = -60.
In mathematics, y axis goes up.  But in Small Basic GraphicsWindow, y axis goes down.  So sign of angle becomes opposite.

If you don't use trigonometric function, so use only 0, 30, 45, 60, 90, ...[degree].  Between these angles you can calculate height s from width c of the triangle while three edges of triangle r, c, s such as picture below have relationship r2 = s2 + c2 .

But for any angle, use trigonometric function (sin, cos).

Be careful that trigonometric function needs [radian] but [degree] for Small Basic language.

Following sample code shows how not to use or how to use trigonometric functions (sin, cos) for drawing arc.

`gw ````= ````GraphicsWindow``.``Width`
`gh ````= ````GraphicsWindow``.``Height`
`DrawGrid``(``)`
`x ````= ````200`
`y ````= ````300`
`r ````= ````200`
`DrawArcwoTrigo``(``)`
`Program``.``Delay``(``3000``)`
`GraphicsWindow``.``Clear``(``)`
`DrawGrid``(``)`
`a1 ````= ````-``30`
`a2 ````= ````-``60`
`DrawArcwTrigo``(``)`
```Sub ````DrawArcwoTrigo  ``' draw arc without trigonometric functions`
`  ``GraphicsWindow``.````Title ````= ``"Arc without Trigonometric Functions"`
`  ````c1 ````= ``r ` `* ``Math``.``SquareRoot``(``3````) ````/ ``2   ``' a1 = -30`
`  ````c2 ````= ``r ` `/ ``2                        ``' a2 = -60`
`  ````r2 ````= ``Math``.``Power``(``r````, ````2``)`
`  ````For ````c ``= ` `c1 ````To ````c2 ````Step ````-``1`
`    ````s ````= ``Math``.``SquareRoot``(````r2 ````- ``Math``.``Power``(``c````, ````2``)``)`
`    ````x2 ````= ``x ` `+ ``c`
`    ````y2 ````= ``y ` `- ``s`
`    ````If ````c ``= ` `c1 ``Then`
`      ``GraphicsWindow``.````PenColor ````= ``"Gray"`
`      ``GraphicsWindow``.``DrawLine``(``x````, ````y``, ``x2````, ````y2``)`
`    ``Else`
`      ``GraphicsWindow``.````PenColor ````= ``"Black"`
`      ``GraphicsWindow``.``DrawLine``(``x1````, ````y1``, ` `x2``, ``y2``)`
`    ``EndIf`
`    ````If ````c ``- ` `1 ``< ````c2 ````Then`
`      ``GraphicsWindow``.````PenColor ````= ``"Gray"`
`      ``GraphicsWindow``.``DrawLine``(``x````, ````y``, ``x2````, ````y2``)`
`    ``EndIf`
`    ````x1 ````= ``x2`
`    ````y1 ````= ``y2`
`  ``EndFor`
`EndSub`
```Sub ````DrawArcwTrigo  ``' draw arc with trigonometric functions`
`  ``GraphicsWindow``.````Title ````= ``"Arc with Trigonometric Functions"`
`  ````For ````a ``= ` `a1 ````To ````a2 ````Step ````-``0.3`
`    ````x2 ````= ``x ` `+ ``r ````* ````Math``.``Cos``(``Math``.``GetRadians``(``a``)``)`
`    ````y2 ````= ``y ` `+ ``r ````* ````Math``.``Sin``(``Math``.``GetRadians``(``a``)``)`
`    ````If ````a ``= ` `a1 ``Then`
`      ``GraphicsWindow``.````PenColor ````= ``"Gray"`
`      ``GraphicsWindow``.``DrawLine``(``x````, ````y``, ``x2````, ````y2``)`
`    ``Else`
`      ``GraphicsWindow``.````PenColor ````= ``"Black"`
`      ``GraphicsWindow``.``DrawLine``(``x1````, ````y1``, ` `x2``, ``y2``)`
`    ``EndIf`
`    ````If ````a ``- ` `0.3 ``< ````a2 ````Then`
`      ``GraphicsWindow``.````PenColor ````= ``"Gray"`
`      ``GraphicsWindow``.``DrawLine``(``x````, ````y``, ``x2````, ````y2``)`
`    ``EndIf`
`    ````x1 ````= ``x2`
`    ````y1 ````= ``y2`
`  ``EndFor`
`EndSub`
```Sub ````DrawGrid`
`  ``GraphicsWindow``.````PenColor ````= ``"MediumSeaGreen"`
`  ``GraphicsWindow``.````BrushColor ````= ``"MediumSeaGreen"`
`  ````For ````_x ``= ` `0 ````To ````gw ````Step ````50`
`    ``GraphicsWindow``.``DrawLine``(``_x````, ````0``, ``_x````, ````gh``)`
`    ````If ````gw ``- ` `50 ``< ````_x ````Then`
`      ``GraphicsWindow``.``DrawText``(````_x ````+ ``4````, ````4``, ``"x"``)`
`    ``Else`
`      ``GraphicsWindow``.``DrawText``(````_x ````+ ``4````, ````4``, ``_x``)`
`    ``EndIf`
`  ``EndFor`
`  ````For ````_y ``= ` `0 ````To ````gh ````Step ````50`
`    ``GraphicsWindow``.``DrawLine``(``0````, ````_y``, ` `gw``, ``_y``)`
`    ````If ````gh ``- ` `50 ``< ````_y ````Then`
`      ``GraphicsWindow``.``DrawText``(``4````, ````_y ``+ ` `4``, ``"y"``)`
`    ``Else`
`      ``GraphicsWindow``.``DrawText``(``4````, ````_y ``+ ` `4``, ``_y``)`
`    ``EndIf`
`  ``EndFor`
`EndSub`
Sort by: Published Date | Most Recent | Most Useful