Microsoft Small Basic

Program Listing: KJG712-0
' Curve Demo
' Version 0.2
' Copyright © 2015 Nonki Takahashi. The MIT License.
' Last update 2015-06-06
' Program ID KJG712-0
'
title = "Curve Demo"
ver = "0.2"
Not = "False=True;True=False;"
nth = "1=first;2=second;3=third;4=fourth;"
While "True"
Quadratic()
Cubic()
EndWhile
Sub Quadratic
Form()
GraphicsWindow.Title = title + " " + ver + " - Quadratic Bezier Curve"
n = 3
GetPoints()
msg = ""
ShowInstruction()
GraphicsWindow.PenColor = "Gray"
GraphicsWindow.DrawLine(px[1], py[1], px[2], py[2])
GraphicsWindow.DrawLine(px[2], py[2], px[3], py[3])
DrawQuadraticBezier()
Program.Delay(2000)
msg = "Click to clear the screen."
ShowInstruction()
GetPoint()
GraphicsWindow.Clear()
EndSub
Sub Cubic
Form()
GraphicsWindow.Title = title + " " + ver + " - Cubic Bezier Curve"
n = 4
GetPoints()
msg = ""
ShowInstruction()
GraphicsWindow.PenColor = "Gray"
GraphicsWindow.DrawLine(px[1], py[1], px[2], py[2])
GraphicsWindow.DrawLine(px[2], py[2], px[3], py[3])
GraphicsWindow.DrawLine(px[3], py[3], px[4], py[4])
DrawCubicBezier()
Program.Delay(2000)
msg = "Click to clear the screen."
ShowInstruction()
GetPoint()
GraphicsWindow.Clear()
EndSub
Sub Form
gw = 598
gh = 428
GraphicsWindow.Width = gw
GraphicsWindow.Height = gh
GraphicsWindow.FontName = "Trebuchet MS"
GraphicsWindow.BrushColor = "Black"
GraphicsWindow.FontSize = 20
instruction = Shapes.AddText("")
Shapes.Move(instruction, 10, 10)
GraphicsWindow.PenColor = "#99FFFF"
incr = 10
DrawGrid()
GraphicsWindow.PenColor = "#00FFFF"
incr = 100
DrawGrid()
GraphicsWindow.MouseDown = OnMouseDown
EndSub
Sub DrawGrid
For x = 0 To gw Step incr
GraphicsWindow.DrawLine(x, 0, x, gh)
EndFor
For y = 0 To gh Step incr
GraphicsWindow.DrawLine(0, y, gw, y)
EndFor
EndSub
Sub DrawQuadraticBezier
' param px, py - array of three control points for quadratic Bezier curve
For k = 0 To 1 Step 0.05
x1 = px[1] + k * (px[2] - px[1])
y1 = py[1] + k * (py[2] - py[1])
x2 = px[2] + k * (px[3] - px[2])
y2 = py[2] + k * (py[3] - py[2])
GraphicsWindow.PenColor = "Gray"
line = Shapes.AddLine(x1, y1, x2, y2)
x = x1 + k * (x2 - x1)
y = y1 + k * (y2 - y1)
If 0 < k Then
GraphicsWindow.PenColor = "Black"
GraphicsWindow.DrawLine(_x, _y, x, y)
EndIf
_x = x ' last x
_y = y ' last y
msg = "k = " + k
ShowInstruction()
Program.Delay(500)
Shapes.Remove(line)
EndFor
EndSub
Sub DrawCubicBezier
' param px, py - array of three control points for qubic Bezier curve
For k = 0 To 1 Step 0.05
x1 = px[1] + k * (px[2] - px[1])
y1 = py[1] + k * (py[2] - py[1])
x2 = px[2] + k * (px[3] - px[2])
y2 = py[2] + k * (py[3] - py[2])
x3 = px[3] + k * (px[4] - px[3])
y3 = py[3] + k * (py[4] - py[3])
GraphicsWindow.PenColor = "Gray"
line1 = Shapes.AddLine(x1, y1, x2, y2)
line2 = Shapes.AddLine(x2, y2, x3, y3)
x12 = x1 + k * (x2 - x1)
y12 = y1 + k * (y2 - y1)
x23 = x2 + k * (x3 - x2)
y23 = y2 + k * (y3 - y2)
line3 = Shapes.AddLine(x12, y12, x23, y23)
x = x12 + k * (x23 - x12)
y = y12 + k * (y23 - y12)
If 0 < k Then
GraphicsWindow.PenColor = "Black"
GraphicsWindow.DrawLine(_x, _y, x, y)
EndIf
_x = x ' last x
_y = y ' last y
msg = "k = " + k
ShowInstruction()
Program.Delay(500)
Shapes.Remove(line1)
Shapes.Remove(line2)
Shapes.Remove(line3)
EndFor
EndSub
Sub GetPoint
' return x, y - clicked point
mouseDown = "False"
While Not[mouseDown]
Program.Delay(300)
EndWhile
x = GraphicsWindow.MouseX
y = GraphicsWindow.MouseY
EndSub
Sub GetPoints
' param n - number of points to get
For i = 1 To n
msg = "Click "+ nth[i] + " point on the screen."
ShowInstruction()
GetPoint()
DrawMarker()
px[i] = x
py[i] = y
EndFor
EndSub
Sub OnMouseDown
mouseDown = "True"
EndSub
Sub ShowInstruction
' param msg - instruction message to show
Shapes.SetText(instruction, msg)
EndSub
Sub DrawMarker
' param x, y - point to draw
GraphicsWindow.PenColor = "Black"
GraphicsWindow.DrawLine(x - 4, y - 4, x + 4, y + 4)
GraphicsWindow.DrawLine(x - 4, y + 4, x + 4, y - 4)
EndSub