Microsoft Small Basic

Program Listing: HZB416
' Crystal Structure of Salt
' Version 0.4
' Copyright © 2019 Nonki Takahashi. The MIT License.
' Last update 2019-11-20

GraphicsWindow.Title = "Crystal Structure of Salt"
scale = 0.47
Init()
mol = "NaCl"
DrawMolecularName()
distance = (atom["Na"]["+"] + atom["Cl"]["-"]) * scale
cols = 5
rows = 5
For row = 1 To rows
For col = 1 To cols
If Math.Remainder(row - col, 2) = 0 Then
' draw Na
ion = "+"
a = "Na"
Else
' draw Cl
ion = "-"
a = "Cl"
EndIf
ox = cx - (col - (cols + 1) / 2) * distance
oy = cy - (row - (rows + 1) / 2) * distance
bc = "#11000000"
opt = ""
size = atom[a][ion] * 2 * scale
FillCircle()
opt = ion
bc = atom[a]["color"]
size = atom[a]["size"] * scale
FillCircle()
EndFor
EndFor

Sub DrawLine
GraphicsWindow.PenColor = pc
If n = 2 Then
GraphicsWindow.PenWidth = 12
GraphicsWindow.DrawLine(x1, y1, x2, y2)
GraphicsWindow.PenColor = bg
EndIf
GraphicsWindow.PenWidth = 4
GraphicsWindow.DrawLine(x1, y1, x2, y2)
EndSub

Sub DrawMolecularName
GraphicsWindow.FontName = "Trebuchet MS"
GraphicsWindow.BrushColor = "Black"
x = 30
For p = 1 To Text.GetLength(mol)
c = Text.GetSubText(mol, p, 1)
If c = "_" Then
y = 35
p = p + 1
fs = 20
c = Text.GetSubText(mol, p, 1)
Else
y = 20
fs = 30
EndIf
GraphicsWindow.FontSize = fs
GraphicsWindow.DrawText(x, y, c)
x = x + fs * 0.7
EndFor
EndSub

Sub FillCircle
GraphicsWindow.BrushColor = bc
GraphicsWindow.FillEllipse(ox - size / 2, oy - size / 2, size, size)
If opt <> "" Then
GraphicsWindow.FontSize = 35
GraphicsWindow.BrushColor = pc
GraphicsWindow.DrawText(ox + size / 2, oy - size / 2 - 35 * 0.5, opt)
EndIf
EndSub

Sub Init
bg = "LightGray"
pc = "DimGray"
GraphicsWindow.BackgroundColor = bg
' size: Van der Waals radius / 2 [pm]
' 1: single-bond covalent radius [pm]
' 2: double-bond covalent radius [pm]
' 3: triple-bond covalent radius [pm]
' +: cation (effective) ionic radius [pm]
' -: anion (effective) ionic radius [pm]
atom["H"] = "color=White;size=55;1=32;"
atom["C"] = "color=Black;size=85;1=75;2=67;3=60;"
atom["O"] = "color=#EE0000;size=76;1=63;2=57;3=53;"
atom["F"] = "color=YellowGreen;size=73.5;1=64;2=59;3=53;"
atom["Na"] = "color=MediumOrchid;size=113.5;1=155;2=160;+=102;"
atom["Cl"] = "color=LimeGreen;size=87.5;1=99;2=95;3=93;-=181;"
root2 = Math.SquareRoot(2)
root3 = Math.SquareRoot(3)
gw = GraphicsWindow.Width
gh = GraphicsWindow.Height
cx = gw / 2
cy = gh / 2
n = 1
x1 = 0
y1 = 0
x2 = 0
y2 = 0
EndSub