Skip to content

Commit 73050b9

Browse files
authored
Create Example - Circle Morphing.monkey2
1 parent bc6f1f0 commit 73050b9

File tree

1 file changed

+108
-0
lines changed

1 file changed

+108
-0
lines changed

Example - Circle Morphing.monkey2

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
#Import "<std>"
2+
#Import "<mojo>"
3+
4+
Using std..
5+
Using mojo..
6+
7+
'
8+
Class circlemorph
9+
Field numpoints:Int
10+
Field circlearray:Int[,]
11+
Field trianglearray:Int[,]
12+
Field drawarray:Int[,]
13+
' perc is the percentage between morph (0 to 1)
14+
' float is how fast we will morph
15+
Field perc:Float,ms:Float=.01
16+
Method New(numpoints:Int)
17+
Self.numpoints = numpoints
18+
circlearray = New Int[numpoints,2]
19+
trianglearray = New Int[numpoints,2]
20+
drawarray = New Int[numpoints,2]
21+
makearrays()
22+
End Method
23+
' Move the draw points between the circle and triangle
24+
Method update()
25+
perc+=ms
26+
If perc>=1 Or perc<=0 Then ms=-ms
27+
For Local i:Int=0 Until numpoints
28+
drawarray[i,0] = lerp(perc,circlearray[i,0],trianglearray[i,0])
29+
drawarray[i,1] = lerp(perc,circlearray[i,1],trianglearray[i,1])
30+
Next
31+
End Method
32+
' Create the triangle and the circle points
33+
Method makearrays()
34+
' create the circle points
35+
Local stp:Float=TwoPi/numpoints
36+
Local angle:Float=0
37+
For Local i:Int=0 Until numpoints
38+
circlearray[i,0] = Cos(angle)*50
39+
circlearray[i,1] = Sin(angle)*50
40+
angle+=stp
41+
Next
42+
'create the triangle points
43+
Local x1:Int=(Cos(0)*150)
44+
Local y1:Int=(Sin(0)*150)
45+
Local x2:Int=Cos(Pi/2)*150
46+
Local y2:Int=Sin(Pi/2)*150
47+
Local x3:Int=Cos(TwoPi)*150
48+
Local y3:Int=Sin(TwoPi)*150
49+
stp = 1/(numpoints/3)
50+
For Local i:Int=0 Until numpoints/3
51+
trianglearray[i,0] = lerp(stp*i,x1,x2)
52+
trianglearray[i,1] = lerp(stp*i,y1,y2)
53+
Next
54+
For Local i:Int=0 Until numpoints/3
55+
trianglearray[i+(numpoints/3),0] = lerp(stp*i,x2,x3)
56+
trianglearray[i+(numpoints/3),1] = lerp(stp*i,y2,y3)
57+
Next
58+
For Local i:Int=0 Until numpoints/3
59+
trianglearray[i+(numpoints-(numpoints/3)),0] = lerp(stp*i,x3,x1)
60+
trianglearray[i+(numpoints-(numpoints/3)),1] = lerp(stp*i,y3,y1)
61+
Next
62+
63+
End Method
64+
' Draw lines between the points
65+
Method draw(canvas:Canvas)
66+
canvas.Color = Color.White
67+
' For Local i:=1 Until numpoints
68+
' canvas.DrawLine(circlearray[i-1,0],circlearray[i-1,1],circlearray[i,0],circlearray[i,1])
69+
' Next
70+
' canvas.DrawLine(circlearray[0,0],circlearray[0,1],circlearray[numpoints-1,0],circlearray[numpoints-1,1])
71+
' For Local i:=1 Until numpoints
72+
' canvas.DrawLine(trianglearray[i-1,0],trianglearray[i-1,1],trianglearray[i,0],trianglearray[i,1])
73+
' Next
74+
' canvas.DrawLine(trianglearray[0,0],trianglearray[0,1],trianglearray[numpoints-1,0],trianglearray[numpoints-1,1])
75+
For Local i:=1 Until numpoints
76+
canvas.DrawLine(drawarray[i-1,0],drawarray[i-1,1],drawarray[i,0],drawarray[i,1])
77+
Next
78+
canvas.DrawLine(drawarray[0,0],drawarray[0,1],drawarray[numpoints-1,0],drawarray[numpoints-1,1])
79+
80+
End Method
81+
' Percentage 0 to 1 gives number between a and b
82+
Function lerp:double(t:double , a:double, b:double)
83+
return a + t * (b - a)
84+
End Function
85+
End Class
86+
87+
Class MyWindow Extends Window
88+
Field cm:circlemorph
89+
Method New()
90+
cm = New circlemorph(100)
91+
End method
92+
93+
Method OnRender( canvas:Canvas ) Override
94+
App.RequestRender() ' Activate this method
95+
canvas.Clear(Color.Black)
96+
canvas.Translate(Width/2,Height/2)
97+
cm.update()
98+
cm.draw(canvas)
99+
' if key escape then quit
100+
If Keyboard.KeyReleased(Key.Escape) Then App.Terminate()
101+
End Method
102+
End Class
103+
104+
Function Main()
105+
New AppInstance
106+
New MyWindow
107+
App.Run()
108+
End Function

0 commit comments

Comments
 (0)