Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Let's imagine how we'd draw an arc in Small Basic.
Assume the center of the arc is at x = 200, y = 300, and the start angle a1 = -30 and the end angle = -60.
In mathematics, the y axis goes up. But in the Small Basic GraphicsWindow, the y axis goes down. So the sine of the angle becomes the opposite.
If you don't need to use trigonometric functions, you can use only 0, 30, 45, 60, 90, ...[degrees]. Between these angles you can calculate height s from width c of the triangle while three edges of triangle r, c, s (like in picture below) have this relationship: r2 = s2 + c2.
But for any angle, use trigonometric functions (sin, cos):
Be careful; the trigonometric functions need [radian] but [degree] for Small Basic language.
You can use Math.GetRadians(degree) to convert from [degree] to [radian].
The 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