Microsoft Small Basic

Program Listing: DQG281
' Snell's Law
' Copyright © 2017 Nonki Takahashi. The MIT License.
' Last update 2017-09-04

GraphicsWindow.Title = "Snell's Law"
Init()
θA = 40
nA = 1 ' refractive index of air
nB = 2 ' refractive index of glass
_θA = Math.GetRadians(θA)
_θB = Math.ArcSin(nA / nB * Math.Sin(_θA))
θB = Math.GetDegrees(_θB)
xo = gw / 2
yo = gh / 2
x = xo - Math.Tan(_θA) * (yo - 10)
y = 10
GraphicsWindow.DrawLine(x, y, xo, yo)
x = xo + Math.Tan(_θB) * (yo - 10)
y = gh - 10
GraphicsWindow.DrawLine(x, y, xo, yo)
param = "r=50;x=" + xo + ";y=" + yo + ";a1=-90;a2=" + (-90-θA) + ";"
DrawArc()
a = -90 - θA / 2
_a = Math.GetRadians(a)
r = 70
GraphicsWindow.FontSize = 12
x = xo + r * Math.Cos(_a) - 4
y = yo + r * Math.Sin(_a) - 4
GraphicsWindow.DrawText(x, y, "θ")
GraphicsWindow.FontSize = 9
GraphicsWindow.DrawText(x + 8, y + 6, "A")
param = "r=50;x=" + xo + ";y=" + yo + ";a1=90;a2=" + (90-θB) + ";"
DrawArc()
a = 90 - θB / 2
_a = Math.GetRadians(a)
r = 70
GraphicsWindow.FontSize = 12
x = xo + r * Math.Cos(_a) - 4
y = yo + r * Math.Sin(_a) - 4
GraphicsWindow.DrawText(x, y, "θ")
GraphicsWindow.FontSize = 9
GraphicsWindow.DrawText(x + 8, y + 6, "B")

Sub Init
Not = "False=True;True=False;"
gw = 598
gh = 428
GraphicsWindow.Width = 598
GraphicsWindow.Height = 428
GraphicsWindow.BrushColor = "Black"
GraphicsWindow.PenWidth = 1
GraphicsWindow.FontSize = 12
GraphicsWindow.DrawText(20, 10, "sin θ")
GraphicsWindow.DrawText(20, 30, "sin θ")
GraphicsWindow.DrawLine(16, 28, 60, 28)
GraphicsWindow.DrawText(64, 20, "=")
GraphicsWindow.DrawText(80, 10, "λ")
GraphicsWindow.DrawText(80, 30, "λ")
GraphicsWindow.DrawLine(76, 28, 96, 28)
GraphicsWindow.DrawText(98, 20, "=")
GraphicsWindow.DrawText(114, 10, "n")
GraphicsWindow.DrawText(114, 30, "n")
GraphicsWindow.DrawLine(110, 28, 132, 28)
GraphicsWindow.FontSize = 9
GraphicsWindow.DrawText(50, 14, "A")
GraphicsWindow.DrawText(50, 34, "B")
GraphicsWindow.DrawText(87, 16, "A")
GraphicsWindow.DrawText(87, 36, "B")
GraphicsWindow.DrawText(122, 16, "B")
GraphicsWindow.DrawText(122, 36, "A")
GraphicsWindow.BrushColor = "PaleTurquoise"
GraphicsWindow.FillRectangle(10, gh / 2, gw - 20, gh / 2 - 10)
GraphicsWindow.BrushColor = "Black"
GraphicsWindow.DrawLine(10, gh / 2, gw - 10, gh / 2)
param = "x1=" + (gw / 2) + ";y1=10;x2=" + (gw / 2) + ";y2=" + (gh - 10) + ";"
DrawDottedLine()
GraphicsWindow.DrawLine(gw / 2, gh / 2 - 10, gw / 2 + 10, gh / 2 - 10)
GraphicsWindow.DrawLine(gw / 2 + 10, gh / 2 - 10, gw / 2 + 10, gh / 2)
GraphicsWindow.DrawLine(gw / 2 - 10, gh / 2, gw / 2 - 10, gh / 2 + 10)
GraphicsWindow.DrawLine(gw / 2 - 10, gh / 2 + 10, gw / 2, gh / 2 + 10)
EndSub

Sub DrawArc
xo = param["x"]
yo = param["y"]
r = param["r"]
a1 = param["a1"]
a2 = param["a2"]
aMin = Math.Min(a1, a2)
aMax = Math.Max(a1, a2)
last = "False"
For a = aMin To aMax Step 5
_a = Math.GetRadians(a)
x = xo + r * Math.Cos(_a)
y = yo + r * Math.Sin(_a)
If aMin < a Then
GraphicsWindow.DrawLine(x, y, xLast, yLast)
EndIf
If Not[last] And (aMax < a + 5) Then
a = aMax - 5
last = "True"
EndIf
xLast = x
yLast = y
EndFor
EndSub

Sub DrawDottedLine
x1 = param["x1"]
y1 = param["y1"]
x2 = param["x2"]
y2 = param["y2"]
l = Math.SquareRoot(Math.Power(x2 - x1, 2) + Math.Power(y2 - y1, 2))
dl = 3
r = 1
dr = dl / l * r
i = 0
For r = 0 To 1 Step dr
x = x1 + (x2 - x1) * r
y = y1 + (y2 - y1) * r
If Math.Remainder(i, 2) = 1 Then
GraphicsWindow.DrawLine(x, y, xLast, yLast)
EndIf
xLast = x
yLast = y
i = i + 1
EndFor
EndSub