Microsoft Small Basic

Program Listing: LKL810-0
' Challenge of the Month - by NaochanON LKL810-0 added spinning simulation
' October 2016 Make an animation of a top rotating and moving in the GraphicsWindow
make_top()
CenterPosition()

While "True"
Top_move()
Program.Delay(25)
endwhile

Sub Top_move
Shapes.Move(el0,Shapes.GetLeft(el0)+pmX*ddx,Shapes.Gettop(el0)+pmY*ddY)
XYC="X="+(Shapes.GetLeft(el0)+4)+";Y="+ (Shapes.Gettop(el0)+4) ' base point
For i=1 To Array.GetItemCount(shp)
If dat[i]["fn"] = "rc" then
shx=XYC["X"]+DR[i]*Math.cos( math.GetRadians(DS0[i]+pmx*5))-dat[i]["w"]*s/2
shy=XYC["Y"]+DR[i]*Math.sin( math.GetRadians(DS0[i]+pmx*5))-dat[i]["h"]*s/2
ElseIf dat[i]["fn"] = "tr" then
shx=XYC["X"]+DR[i]*Math.cos( math.GetRadians(DS0[i]+pmx*5))-cntr[i]["X"]
shy=XYC["Y"]+DR[i]*Math.sin( math.GetRadians(DS0[i]+pmx*5))-cntr[i]["Y"]
EndIf
Shapes.Move(shp[i],shx,shy)
Shapes.Rotate(shp[i],pmX*5) ' in this case it rotates only +5 or -5 degrees
EndFor
'----------------------------------------------------------------------------------------------
NN=NN+1
shx=XYC["X"]+DR[12]*Math.cos( math.GetRadians(DS0[12]+pmx*5))-dat[12]["w"]*s/2+NN*rot*math.Cos(math.GetRadians(pmx*5))
shy=XYC["Y"]+DR[12]*Math.sin( math.GetRadians(DS0[12]+pmx*5))-dat[12]["h"]*s/2+pmx*NN*rot*math.sin(math.GetRadians(math.Abs(pmx*5)))
Shapes.Move(shp[12],shx,shy) ' spinning simulation
If NN> dat[2]["w"]*s/rot-2 Then
NN=0
EndIf
'----------------------------------------------------------------------------------------------
If Shapes.GetLeft(el0)<0 or (Shapes.GetLeft(shp[2])+dat[2]["w"]*s)>1200 Then ' X edge
pmX=-pmX
EndIf
If Shapes.Gettop(el0)<0 or (Shapes.Gettop(shp[11])+dat[11]["y3"]*s)>650 Then ' Y edge
pmY=-pmY
EndIf
EndSub

Sub CenterPosition
For i= 1 To array.GetItemCount(SHP)
If dat[i]["fn"] = "rc" then
X[i]=Shapes.GetLeft(SHP[i])+dat[i]["w"]*s/2 ' center of the shape ... X
Y[i]=Shapes.GetTop(SHP[i])+dat[i]["h"]*s/2 ' center of the shape ... Y
ElseIf dat[i]["fn"] = "tr" then
X[i]=Shapes.GetLeft(SHP[i])+cntr[i]["X"] ' center of the shape ... X
Y[i]=Shapes.GetTop(SHP[i])+ cntr[i]["Y"] ' center of the shape ... Y
EndIf
DS0[i]=math.GetDegrees(Math.ArcTan((Y[i]-XYC["Y"])/(X[i]-XYC["X"]))) ' degree // angle of the shape_Center from XYC position
DR[i]=Math.SquareRoot((X[i]-XYC["X"])*(X[i]-XYC["X"])+(Y[i]-XYC["Y"])*(Y[i]-XYC["Y"])) ' radius
endfor
endsub

Sub make_top
GraphicsWindow.BackgroundColor="Lightcyan"
GraphicsWindow.Width=1200
GraphicsWindow.Height=650
dat[1] = "fn=rc;x=156;y=0;w=34;h=70;bc=Brown;pc=Brown;pw=1;" '
dat[2] = "fn=rc;x=0;y=70;w=346;h=20;bc=Green;pc=Green;pw=1;" '
dat[3] = "fn=rc;x=0;y=90;w=346;h=20;bc=Red;pc=Red;pw=1;" '
dat[4] = "fn=rc;x=0;y=110;w=346;h=20;bc=Yellow;pc=Yellow;pw=1;" '
dat[5] = "fn=tr;x=0;y=130;x1=0;y1=0;x2=48;y2=0;x3=48;y3=20;bc=Pink;pc=Pink;pw=1;" '
dat[6] = "fn=rc;x=48;y=130;w=250;h=20;bc=Pink;pc=Pink;pw=1;" '
dat[7] = "fn=tr;x=298;y=130;x1=0;y1=0;x2=48;y2=0;x3=0;y3=20;bc=Pink;pc=Pink;pw=1;" '
dat[8] = "fn=tr;x=48;y=150;x1=0;y1=0;x2=48;y2=0;x3=48;y3=20;bc=Lightgray;pc=Lightgray;pw=1;" '
dat[9] = "fn=rc;x=96;y=150;w=154;h=20;bc=Lightgray;pc=Lightgray;pw=1;" '
dat[10] = "fn=tr;x=250;y=150;x1=0;y1=0;x2=48;y2=0;x3=0;y3=20;bc=Lightgray;pc=Lightgray;pw=1;" '
dat[11] = "fn=tr;x=96;y=170;x1=0;y1=0;x2=154;y2=0;x3=77;y3=60;bc=cyan;pc=cyan;pw=1;" '
dat[12] = "fn=rc;x=0;y=70;w=16;h=60;bc=Black;pc=Black;pw=1;" ' spinning mark

s=0.8
X0=100
Y0=100
For i = 1 To Array.GetItemCount(dat)
GraphicsWindow.Penwidth = dat[i]["pw"]*s
GraphicsWindow.PenColor = dat[i]["pc"]
GraphicsWindow.BrushColor = dat[i]["bc"]
If dat[i]["fn"] = "rc" Then
SHP[i] = shapes.Addrectangle(dat[i]["w"]*s, dat[i]["h"]*s)
ElseIf dat[i]["fn"] = "tr" Then
SHP[i]= Shapes.Addtriangle(dat[i]["x1"]*s, dat[i]["y1"]*s, dat[i]["x2"]*s, dat[i]["y2"]*s, dat[i]["x3"]*s, dat[i]["y3"]*s)
cntr[i]="X="+((math.Max(dat[i]["x2"],dat[i]["x3"])-dat[i]["x1"])*s/2)+";Y="+ ((math.Max(dat[i]["y2"],dat[i]["y3"])-dat[i]["y1"])*s/2)
EndIf
shapes.Move(SHP[i], X0+dat[i]["x"]*s, Y0+dat[i]["y"]*s)
EndFor
'----------------------------------------------------------------------------------------------
GraphicsWindow.BrushColor="Transparent"
GraphicsWindow.PenColor="Lightcyan"
el0= Shapes.AddEllipse(8,8)
Shapes.Move(el0,Shapes.GetLeft(shp[1])-170*s,Shapes.Gettop(shp[1])-10)
XYC="X="+(Shapes.GetLeft(el0)+4)+";Y="+ (Shapes.Gettop(el0)+4) ' base point
'----------------------------------------------------------------------------------------------
ddx=-5
ddy=-1
pmX=1
pmY=1
rot=12
EndSub