Microsoft Small Basic

Program Listing: CNM748-3
' Rotate Triangle 0.5
' Copyright (c) 2014 Nonki Takahashi. The MIT License.
' Program ID CNM748-3
'
GraphicsWindow.Title = "Rotate Triangle 0.5"
Not = "False=True;True=False;"
If Text.GetLength(GraphicsWindow.GetPixel(0, 0)) = 9 Then
silverlight = "True"
Else
silverlight = "False"
EndIf
gw = 598
gh = 428
GraphicsWindow.Width = gw
GraphicsWindow.Height = gh
ox = gw / 3
oy = gh / 3
param = "color=#88FFFF;step=10;"
DrawGrid()
param = "color=#77EEEE;step=50;"
DrawGrid()
param = "color=#66CCCC;step=100;number=True;"
DrawGrid()
While "True"
For i = 1 To 3
GetVertex()
ShowVertex()
EndFor
CalcCenter()
If tri <> "" Then
Shapes.Remove(tri)
EndIf
GraphicsWindow.PenWidth = 0
GraphicsWindow.BrushColor = "Orange"
tri = Shapes.AddTriangle(x[1], y[1], x[2], y[2], x[3], y[3])
Shapes.Move(tri, ox, oy)
Shapes.SetOpacity(tri, 70)
For angle = 0 To 360 Step 5
Shapes.Rotate(tri, angle)
If silverlight Then
a = Math.GetRadians(angle)
_cx = cx * Math.Cos(a) - cy * Math.Sin(a)
_cy = cx * Math.Sin(a) + cy * Math.Cos(a)
_ox = ox - (_cx - cx)
_oy = oy - (_cy - cy)
Shapes.Move(tri, _ox, _oy)
EndIf
Program.Delay(100)
EndFor
EndWhile
Sub CalcCenter
xmin = 0
ymin = 0
xmax = x[1]
ymax = y[1]
For i = 2 To 3
If x[i] < xmin Then
xmin = x[i]
EndIf
If xmax < x[i] Then
xmax = x[i]
EndIf
If y[i] < ymin Then
ymin = y[i]
EndIf
If ymax < y[i] Then
ymax = y[i]
EndIf
EndFor
If xmin < 0 Then
xmin = 0
EndIF
If ymin < 0 Then
ymin = 0
EndIf
If xmax < 0 Then
xmax = 0
EndIF
If ymax < 0 Then
ymax = 0
EndIf
cx = Math.Round((xmin + xmax) / 2)
cy = Math.Round((ymin + ymax) / 2)
GraphicsWindow.BrushColor = "Red"
GraphicsWindow.PenColor = "Red"
If center = "" Then
center = Shapes.AddText("")
Shapes.Move(center, 10, 20 * 4)
EndIf
Shapes.SetText(center, "cx=" + cx + ", cy=" + cy)
If center1 = "" Then
center1 = Shapes.AddLine(-4, -4, 4, 4)
center2 = Shapes.AddLine(-4, 4, 4, -4)
EndIf
Shapes.Move(center1, cx + ox, cy + oy)
Shapes.Move(center2, cx + ox, cy + oy)
EndSub
Sub DrawGrid
GraphicsWindow.PenColor = param["color"]
GraphicsWindow.BrushColor = param["color"]
x1 = -Math.Floor(ox / param["step"]) * param["step"]
x2 = Math.Floor((gw - ox) / param["step"]) * param["step"]
For x = x1 To x2 Step param["step"]
dx = x + ox
GraphicsWindow.DrawLine(dx, 0, dx, gh)
If param["number"] Then
GraphicsWindow.DrawText(dx + 2, oy, x)
EndIf
EndFor
y1 = -Math.Floor(oy / param["step"]) * param["step"]
y2 = Math.Floor((gh - oy) / param["step"]) * param["step"]
For y = y1 To y2 Step param["step"]
dy = y + oy
GraphicsWindow.DrawLine(0, dy, gw, dy)
If param["number"] Then
GraphicsWindow.DrawText(ox + 2, dy, y)
EndIf
EndFor
EndSub
Sub GetVertex
' param i
mouseDown = "False"
GraphicsWindow.MouseDown = OnMouseDown
While Not[mouseDown]
Program.Delay(200)
EndWhile
x[i] = Math.Round((dx - ox) / 10) * 10
y[i] = Math.Round((dy - oy) / 10) * 10
EndSub
Sub OnMouseDown
dx = GraphicsWindow.MouseX
dy = GraphicsWindow.MouseY
mouseDown = "True"
EndSub
Sub ShowVertex
' param i
GraphicsWindow.BrushColor = "Black"
GraphicsWindow.PenColor = "Black"
If vertex[i] = "" Then
vertex[i] = Shapes.AddText("")
Shapes.Move(vertex[i], 10, 20 * i)
EndIf
Shapes.SetText(vertex[i], "x" + i + "=" + x[i] + ", y" + i + "=" + y[i])
If cross1[i] = "" Then
cross1[i] = Shapes.AddLine(-4, -4, 4, 4)
cross2[i] = Shapes.AddLine(-4, 4, 4, -4)
EndIf
Shapes.Move(cross1[i], x[i] + ox, y[i] + oy)
Shapes.Move(cross2[i], x[i] + ox, y[i] + oy)
EndSub