Microsoft Small Basic

Program Listing: STH586-0
' Molecular Structure of Methane
' Version 0.2
' Copyright © 2019 Nonki Takahashi. The MIT License.
' Last update 2019-11-18
' Program ID STH586-0

GraphicsWindow.Title = "Molecular Structure of Methane"
bg = "LightGray"
pc = "DimGray"
GraphicsWindow.BackgroundColor = bg
atom["C"] = "color=Black;size=150;"
atom["O"] = "color=#EE0000;size=100;"
atom["H"] = "color=White;size=50;"
root2 = Math.SquareRoot(2)
root3 = Math.SquareRoot(3)
mol = "CH_4"
DrawMolecularName()
gap = 20
gw = GraphicsWindow.Width
gh = GraphicsWindow.Height
_size = atom["H"]["size"]
size = atom["C"]["size"]
distance = size / 2 + _size / 2 + gap
d1 = size / 2
d2 = size / 2 + gap
opt = ""
cx = gw / 2
cy = gh / 2
c = 2 / 3 * root2
s = 1 / 3
' draw H (1)
bc = atom["H"]["color"]
size = _size
ox = cx
oy = cy - distance
FillCircle()
' draw line (1)
x1 = cx
y1 = cy - d1
x2 = cx
y2 = cy - d2
n = 1
DrawLine()
' draw H (2)
bc = atom["H"]["color"]
size = _size
ox = cx - distance * root3 / 2 * c
oy = cy + distance * s
FillCircle()
' draw line (2)
x1 = cx - d1 * root3 / 2 * c
y1 = cy + d1 * s
x2 = cx - d2 * root3 / 2 * c
y2 = cy + d2 * s
n = 1
DrawLine()
' draw H (3)
ox = cx + distance * root3 / 2 * c
FillCircle()
' draw line (2)
x1 = cx + d1 * root3 / 2 * c
y1 = cy + d1 * s
x2 = cx + d2 * root3 / 2 * c
y2 = cy + d2 * s
n = 1
DrawLine()
' draw C
size = atom["C"]["size"]
bc = atom["C"]["color"]
ox = cx
oy = cy
FillCircle()
' draw H (4)
bc = atom["H"]["color"]
size = _size
ox = cx
oy = cy + distance * s
FillCircle()

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, opt)
EndIf
EndSub