Microsoft Small Basic

Program Listing: TBS366
' Round Triangle and Round Hat
' Version 0.1
' Copyright © 2018 Nonki Takahahsi. The MIT License.
' Last update 2018-12-12

GraphicsWindow.Title = "Round Triangle and Round Hat | Toggle Shapes opacity with mouse"
gw = 598
gh = 428
GraphicsWindow.Width = gw
GraphicsWindow.Height = gh
Not = "False=True;True=False;"
π = Math.Pi
op = 100
bc = ""
pc = "Black"
pw = 0
r = 20
width = 250
height = 300
x = (gw - width) / 2
y = (gh - height) / 2
GraphicsWindow.MouseDown = OnMouseDown
While "True"
If bc <> "Red" Then
bc = "Red"
RoundTriangle()
Else
bc = "Blue"
RoundHat()
EndIf
Delay()
RemoveShapes()
EndWhile

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 OnMouseDown
mouseDown = "True"
EndSub

Sub RoundHat
' param x, y - top left corner
' param width, height - size
' param r - cornar radius
' param angle - rotate angle
hat = "True"
RoundHatOrTriangle()
EndSub

Sub RoundTriangle
' param x, y - top left corner
' param width, height - size
' param r - cornar radius
hat = "False"
RoundHatOrTriangle()
EndSub

Sub RoundHatOrTriangle
' param x, y - top left corner
' param width, height - size
' param r - cornar radius
' param hat - "True" If hat
_x = x
_y = y
_width = width
_height = height
cx = _x + _width / 2
cy = _y + _height / 2
_x1 = cx
_y1 = _y + r
x = _x1 - r
y = _y1 - r
width = 2 * r
height = 2 * r
i = 0
AddEllipse()
_x2 = _x + r
_y2 = _y + _height - r
x = _x2 - r
y = _y2 - r
AddEllipse()
_x3 = _x + _width - r
_y3 = _y2
x = _x3 - r
AddEllipse()
_pc = pc
_pw = pw
If Not[hat] Then
pw = 0
x1 = _width / 2 - r
y1 = 0
x2 = 0
y2 = _height - 2 * r
x3 = _width - 2 * r
y3 = _height - 2 * r
x = cx - _width / 2 + r
y = cy - _height / 2 + r
AddTriangle()
EndIf
pc = bc
pw = 2 * r
_angle = angle
angle = 0
x = 0
y = 0
x1 = _x1
y1 = _y1
x2 = _x2
y2 = _y2
AddLine()
If Not[hat] Then
x1 = _x2
y1 = _y2
x2 = _x3
y2 = _y3
AddLine()
EndIf
x1 = _x3
y1 = _y3
x2 = _x1
y2 = _y1
AddLine()
pc = _pc
pw = _pw
x = _x1 - _x2
y = _y1 - _y2
_r = r
Math_CartesianToPolar()
r = _r
_a = Math.GetRadians(a)
x = 0
y = 0
x1 = _x1 + (r - pw / 2) * Math.Cos(_a - π / 2)
y1 = _y1 + (r - pw / 2) * Math.Sin(_a - π / 2)
x2 = _x2 + (r - pw / 2) * Math.Cos(_a - π / 2)
y2 = _y2 + (r - pw / 2) * Math.Sin(_a - π / 2)
xL = _x2 + (r - pw / 2) * Math.Cos(_a + π / 2)
yL = _y2 + (r - pw / 2) * Math.Sin(_a + π / 2)
AddLine()
x = _x1 - _x3
y = _y1 - _y3
_r = r
Math_CartesianToPolar()
r = _r
_a = Math.GetRadians(a)
x = 0
y = 0
x1 = _x1 + (r - pw / 2) * Math.Cos(_a + π / 2)
y1 = _y1 + (r - pw / 2) * Math.Sin(_a + π / 2)
x2 = _x3 + (r - pw / 2) * Math.Cos(_a + π / 2)
y2 = _y3 + (r - pw / 2) * Math.Sin(_a + π / 2)
xR = _x3 + (r - pw / 2) * Math.Cos(_a - π / 2)
yR = _y3 + (r - pw / 2) * Math.Sin(_a - π / 2)
AddLine()
If hat Then
_w = (xR - xL) / 2
_h = _w * (_y2 - _y1) / (_x1 - _x2)
x1 = (xL + xR) / 2
y1 = yL - _h
x2 = xL
y2 = yL
AddLine()
x2 = xR
y2 = yR
AddLine()
pw = 0
_bc = bc
bc = pc
width = _pw
height = _pw
x = x1 - _pw / 2
y = y1 - _pw / 2
AddEllipse()
pw = _pw
bc = _bc
Else
x1 = _x + r
y1 = _y + _height - pw / 2
x2 = _x + _width - r
y2 = _y + _height - pw / 2
AddLine()
EndIf
x = _x
y = _y
width = _width
height = _height
EndSub

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

Sub AddLine
i = i + 1
GraphicsWindow.PenColor = pc
GraphicsWindow.PenWidth = pw
shp[i] = Shapes.AddLine(x1, y1, x2, y2)
Shapes.Move(shp[i], x, y)
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.SetOpacity(shp[i], op)
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