Microsoft Small Basic

Program Listing: CVF012
' Cubic Bézier Curve
' Version 0.1.0
' Copyright © 2020 Nonki Takahshi. The MIT License.
' Last update 2020-09-10

scale = 150
DrawGrid()

size = 10 ' size of a point [pixel]

a = 1
b = 0
c = -1
d = 0
DrawCubic()
Expression()
GraphicsWindow.DrawText(40, 10, expr)

x = -1
y = 0
DrawPoint()
x1 = x
y1 = y

x = -1/3
y = 4/3
DrawPoint()
x2 = x
y2 = y

x = 1/3
y = -4/3
DrawPoint()
x3 = x
y3 = y

x = 1
y = 0
DrawPoint()
x4 = x
y4 = y

AddPoints()
GraphicsWIndow.PenWidth = 2
GraphicsWindow.PenColor = "DarkRed"
While "True"
shL = ""
nL = 0
For k = 0 To 1 Step 0.05
MovePoints()
If 0 < k Then
nL = nL + 1
shL[nL] = Shapes.AddLine(_gx, _gy, gx, gy)
EndIf
_gx = gx
_gy = gy
Program.Delay(100)
EndFor
For i = 1 To nL
Shapes.Remove(shL[i])
EndFor
EndWhile

Sub AddPoints
shT = Shapes.AddText("")
Shapes.Move(shT, 40, 40)
GraphicsWindow.PenWidth = 0
GraphicsWindow.BrushColor = "DarkGreen"
shP[1] = Shapes.AddEllipse(size, size)
shP[2] = Shapes.AddEllipse(size, size)
shP[3] = Shapes.AddEllipse(size, size)
GraphicsWindow.BrushColor = "DarkOrange"
shP[4] = Shapes.AddEllipse(size, size)
shP[5] = Shapes.AddEllipse(size, size)
GraphicsWindow.BrushColor = "DarkRed"
shP[6] = Shapes.AddEllipse(size, size)
EndSub

Sub Cubic
y = a * x * x * x + b * x * x + c * x + d
EndSub

Sub DrawCubic
' param gxo, gyo - center position in the graphics window
' param a, b - major and minor semi axis
' param n
' param scale
GraphicsWIndow.PenColor = "#00CCCC"
For x = x1 To x2 Step dx
Cubic()
Map()
gx2 = gx
gy2 = gy
If x <> x1 Then
GraphicsWindow.DrawLine(gx1, gy1, gx2, gy2)
EndIf
gx1 = gx2
gy1 = gy2
EndFor
EndSub

Sub DrawGrid
gw = GraphicsWindow.Width
gh = GraphicsWindow.Height
gxo = gw / 2
gyo = gh / 2
fn = GraphicsWindow.FontName
If (fn = "Tahoma") Or (fn = "Segoe UI") Then
c10 = "#33009999"
c100 = "#66009999"
bc = "#00CCCC"
Else ' for SBO
c10 = "#00999933"
c100 = "#00999966"
bc = "#00CCCC"
EndIf
GraphicsWindow.FontName = "Courier New"
GraphicsWindow.FontSize = 14
GraphicsWindow.BrushColor = bc
dx = 0.1
dy = -0.1
gx = Math.Remainder(gw / 2, dx * scale) - dx * scale
gy = Math.Remainder(gh / 2, dy * scale)
MapInv()
x1 = x
y1 = y
gx = gw - Math.Remainder(gw / 2, dx * scale) + dx * scale
gy = gh - Math.Remainder(gh / 2, dy * scale)
MapInv()
x2 = x
y2 = y
For x = x1 To x2 Step dx
Map()
rem = Math.Remainder(x, 1)
If rem = 0.0 Then
GraphicsWindow.PenColor = c100
GraphicsWindow.DrawText(gx + 2, gh / 2, x)
Else
GraphicsWindow.PenColor = c10
EndIf
GraphicsWindow.DrawLine(gx, 0, gx, gh)
EndFor
For y = y1 To y2 Step dy
Map()
If Math.Remainder(y, 1) = 0.0 Then
GraphicsWindow.PenColor = c100
If x <> 0 Then
GraphicsWindow.DrawText(gw / 2 + 2, gy, y)
EndIf
Else
GraphicsWindow.PenColor = c10
EndIf
GraphicsWindow.DrawLine(0, gy, gw, gy)
EndFor
EndSub

Sub DrawPoint
GraphicsWindow.BrushColor = "Black"
Map()
GraphicsWindow.FillEllipse(gx - size / 2, gy - size / 2, size, size)
_x = Math.Floor(x * 1000) / 1000
_y = Math.Floor(y * 1000) / 1000
GraphicsWindow.DrawText(gx, gy, "(" + _x + ", " + _y + ")")
EndSub

Sub Expression
expr = "y ="
plus = " "
If a = 1 Then
expr = expr + " x^3"
plus = " + "
ElseIf a = -1 Then
expr = expr + " - x^3"
plus = " + "
ElseIf a <> 0 Then
expr = expr + " " + a + " * x^3"
plus = " + "
EndIf
If b = 1 Then
expr = expr + plus + "x^2"
plus = " + "
ElseIf b = -1 Then
expr = expr + " - x^2"
plus = " + "
ElseIf 0 < b Then
expr = expr + plus + b + " * x^2"
plus = " + "
ElseIf b < 0 Then
expr = expr + " - " + Math.Abs(b) + " * x^2"
plus = " + "
EndIf
If c = 1 Then
expr = expr + plus + "x"
plus = " + "
ElseIf c = -1 Then
expr = expr + " - x"
plus = " + "
ElseIf 0 < c Then
expr = expr + plus + c + " * x"
plus = " + "
ElseIf c < 0 Then
expr = expr + " - " + Math.Abs(c) + " * x"
plus = " + "
EndIf
If 0 < d Then
expr = expr + plus + d
ElseIf d < 0 Then
expr = expr + " - " + Math.Abs(d)
EndIf
If (a = 0) And (b = 0) And (c = 0) And (d = 0) Then
expr = expr + " 0"
EndIf
EndSub

Sub Map
gx = gxo + scale * x
gy = gyo - scale * y
EndSub

Sub MapInv
x = (gx - gxo) / scale
y = -(gy - gyo) / scale
EndSub

Sub MovePoints
Shapes.SetText(shT, "k = " + k)
x12 = (1 - k) * x1 + k * x2
y12 = (1 - k) * y1 + k * y2
x = x12
y = y12
Map()
Shapes.Move(shP[1], gx - size / 2, gy - size / 2)
x23 = (1 - k) * x2 + k * x3
y23 = (1 - k) * y2 + k * y3
x = x23
y = y23
Map()
Shapes.Move(shP[2], gx - size / 2, gy - size / 2)
x34 = (1 - k) * x3 + k * x4
y34 = (1 - k) * y3 + k * y4
x = x34
y = y34
Map()
Shapes.Move(shP[3], gx - size / 2, gy - size / 2)
x13 = (1 - k) * x12 + k * x23
y13 = (1 - k) * y12 + k * y23
x = x13
y = y13
Map()
Shapes.Move(shP[4], gx - size / 2, gy - size / 2)
x24 = (1 - k) * x23 + k * x34
y24 = (1 - k) * y23 + k * y34
x = x24
y = y24
Map()
Shapes.Move(shP[5], gx - size / 2, gy - size / 2)
x = (1 - k) * x13 + k * x24
y = (1 - k) * y13 + k * y24
Map()
Shapes.Move(shP[6], gx - size / 2, gy - size / 2)
EndSub