Microsoft Small Basic

Program Listing: RSQ369
' Trapezoid
' Version 0.1
' Copyright © 2019 Nonki Takahashi. The MIT License.
' Last update 2019-01-03

GraphicsWindow.Title = "Trapezoid | Toggle Shapes opacity with mouse"
Init()
pw = 0
pc = "Black"
txt = "" ' dummy
While "True"
i = 0
bc = "LightGray"
width = Math.GetRandomNumber(400) + 100
height = Math.GetRandomNumber(200) + 100
x = Math.GetRandomNumber(gw - width - 20) + 10
y = Math.GetRandomNumber(gh - height - 20) + 10
angle = 0
AddRectangle()
ratio = Math.GetRandomNumber(width / 2) / height
_x = x
_y = y
x = _x + height * ratio - 4
y = _y - 8
angle = 180
x1 = 4
y1 = 0
x2 = 0
y2 = 8
x3 = 8
y3 = 8
AddTriangle()
x = _x
y = _y
angle = 0
bc = "SteelBlue"
AddTrapezoid()
Delay()
RemoveShapes()
EndWhile

Sub AddTrapezoid
' param x, y - the left top coordinate of the trapezoid
' param width, height - the size of the trapezoid
' param ratio - the delta x per height
GraphicsWindow.PenColor = pc
GraphicsWindow.PenWidth = pw
_x = x
_y = y
_width = width
_height = height
r2 = ratio * ratio
l2 = Math.SquareRoot(1 + r2)
x1 = Math.Round(ratio * height)
y1 = 0
x2 = 0
y2 = height
x3 = x1 * 2
y3 = height
x = _x
y = _y
angle = 0
AddTriangle()
x = x + width - 2 * x1
angle = 0
AddTriangle()
x = _x + x1
width = width - 2 * x1
AddRectangle()
x = _x
y = _y
width = _width
height = _height
EndSub

Sub AddRectangle
i = i + 1
GraphicsWindow.PenColor = pc
GraphicsWindow.PenWidth = pw
GraphicsWindow.BrushColor = bc
shp[i] = Shapes.AddRectangle(width, height)
Shapes.Move(shp[i], x, y)
Shapes.Rotate(shp[i], angle)
Shapes.SetOpacity(shp[i], op)
EndSub

Sub AddText
i = i + 1
GraphicsWindow.BrushColor = bc
GraphicsWindow.FontSize = fs
GraphicsWindow.FontBold = fb
GraphicsWindow.FontItalic = fi
GraphicsWindow.FontName = fn
shp[i] = Shapes.AddText(txt)
Shapes.Move(shp[i], x, y)
Shapes.Rotate(shp[i], angle)
Shapes.SetOpacity(shp[i], op)
EndSub

Sub AddTriangle
i = i + 1
GraphicsWindow.PenColor = pc
GraphicsWindow.PenWidth = pw
GraphicsWindow.BrushColor = bc
shp[i] = Shapes.AddTriangle(x1, y1, x2, y2, x3, y3)
Shapes.Move(shp[i], x, y)
Shapes.Rotate(shp[i], angle)
Shapes.SetOpacity(shp[i], op)
EndSub

Sub Delay
t = Clock.ElapsedMilliseconds
mouseDown = "False"
While Clock.ElapsedMilliseconds < (t + 3000)
If mouseDown Then
If op = 100 Then
op = 70
Else
op = 100
EndIF
For i = 1 To Array.GetItemCount(shp)
Shapes.SetOpacity(shp[i], op)
EndFor
mouseDown = "False"
EndIf
EndWhile
EndSub

Sub DumpParallelogram
TextWindow.WriteLine("x=" + x)
TextWindow.WriteLine("y=" + y)
TextWindow.WriteLine("width=" + width)
TextWindow.WriteLine("height=" + height)
TextWindow.WriteLine("angle=" + angle)
TextWindow.WriteLine("ratio=" + ratio)
EndSub

Sub DumpTriangle
TextWindow.WriteLine("x=" + x)
TextWindow.WriteLine("y=" + y)
TextWindow.WriteLine("x1=" + x1)
TextWindow.WriteLine("y1=" + y1)
TextWindow.WriteLine("x2=" + x2)
TextWindow.WriteLine("y2=" + y2)
TextWindow.WriteLine("x3=" + x3)
TextWindow.WriteLine("y3=" + y3)
TextWindow.WriteLine("angle=" + angle)
TextWindow.Pause()
EndSub

Sub Init
Not = "True=False;False=True;"
gw = 598
gh = 428
GraphicsWindow.Width = gw
GraphicsWindow.Height = gh
GraphicsWindow.PenWidth = 0
op = 100
fn = "Trebuchet MS"
fs = 20
fb = "True"
fi = "False"
GraphicsWindow.MouseDown = OnMouseDown
EndSub

Sub OnMouseDown
mouseDown = "True"
EndSub

Sub RemoveShapes
n = Array.GetItemCount(shp)
For i = 1 To n
Shapes.Remove(shp[i])
shp[i] = ""
EndFor
EndSub

Sub Math_CartesianToPolar
' Math | convert cartesian coodinate To polar coordinate
' param x, y - cartesian coordinate
' return r, a - polar coordinate
r = Math.SquareRoot(x * x + y * y)
If x = 0 And y > 0 Then
a = 90 ' [degree]
ElseIf x = 0 And y < 0 Then
a = -90
ElseIf x = 0 Then
a = 0
Else
a = Math.ArcTan(y / x) * 180 / Math.Pi
EndIf
If x < 0 Then
a = a + 180
ElseIf x >= 0 And y < 0 Then
a = a + 360
EndIf
EndSub