Microsoft Small Basic

Program Listing: BDT938
' SmallBasic Version 1.2
' Program: ConicSectionsTurtle
' Changelog:
' Author: Pappa Lapub
' Website: https://social.msdn.microsoft.com/Forums/en-US/eaba725c-24a8-4300-8fa6-8231763398cb/challenge-of-the-month-may-2018
' ImportURL: http://smallbasic.com/program/?
' Extension: ---
' Comment: Maths Challenge (Conics)
' Get the Turtle (or another way) to draw the main conic sections. These are:
' Circle, Ellipse, Parabola, Hyperbola
' https://en.wikipedia.org/wiki/Conic_section
' Variables:
' ToDo:
'===============================================================================
gw = 600
gh = 600
r = gw/4

GraphicsWindow.Top = 0
GraphicsWindow.Width = gw
GraphicsWindow.Height = gh

Turtle.Y = gh/2 ' 2*r
Turtle.Turn(90)
'Turtle.Speed = 9

Circle()
Ellipse()
Parabola()
Hyperbola()

Turtle.Hide()
GraphicsWindow.Title = "Conic Sections (Turtle)"


' //////////////////// SUBs ////////////////////
Sub Circle
GraphicsWindow.Title = "Circle y = √(r² -x²)"
GraphicsWindow.PenColor = GraphicsWindow.GetRandomColor()
Turtle.X = r
For x = -r To r
y = Math.SquareRoot(r*r - x*x)
GraphicsWindow.PenWidth = 2*y
Turtle.Move(1)
EndFor
EndSub

Sub Ellipse ' with a = 2*b = r
GraphicsWindow.Title = "Ellipse y = b/a * √(a² -x²)"
GraphicsWindow.PenColor = GraphicsWindow.GetRandomColor()
Turtle.X = r
For x = -r To r
y = Math.SquareRoot(r*r - x*x)
GraphicsWindow.PenWidth = y
Turtle.Move(1)
EndFor
EndSub

Sub Parabola
GraphicsWindow.Title = "Parabola y = p * √x | y = q * x²"
GraphicsWindow.PenColor = GraphicsWindow.GetRandomColor()
Turtle.X = r
p = 12 ' = r/sqrt(r) = 150/sqrt(150) = 12.24745
For x = -r To r
y = p * Math.SquareRoot(Math.Abs(x)) ' horz. OR ..
'y = x * x / r ' vert. (r = 150)
GraphicsWindow.PenWidth = 2*y
Turtle.Move(1)
EndFor
EndSub

Sub Hyperbola ' with a = 2*b = r
GraphicsWindow.Title = "Hyperbola y = b/a * √(x² - a²)"
GraphicsWindow.PenColor = GraphicsWindow.GetRandomColor()
Turtle.X = 0
For x = -2*r To 2*r
y = Math.SquareRoot(x*x - r*r)
GraphicsWindow.PenWidth = 2*y
' Walk over undefined area
If y = 0 Then ' If x = -r Then
Turtle.Move(2*r-1)
x = r
EndIf
Turtle.Move(1)
EndFor
EndSub