-
Notifications
You must be signed in to change notification settings - Fork 2
/
swirlograph.bmx
215 lines (188 loc) · 3.88 KB
/
swirlograph.bmx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
Global things:tlist=New tlist
Type thing
Field x# , y# , vx# , vy#
Field red,green,blue
Field oldpos#[20]
Method New()
things.addlast Self
colour = Rand(1 , 7)
red = 50
green = 50
blue = 50
If colour & 1 red=150
If colour & 2 green=150
If colour & 4 blue=150
End Method
Function create:thing(x# , y# , vx# , vy#)
t:thing = New thing
t.x = x
t.y = y
t.vx = vx
t.vy = vy
For i = 0 To 9
t.oldpos[i * 2] = t.x
t.oldpos[i * 2 + 1] = t.y
Next
Return t
End Function
Method update()
x:+ vx
y:+ vy
For i = 0 To 17
oldpos[i] = oldpos[i + 2]
Next
oldpos[18] = x
oldpos[19] = y
End Method
Method draw()
SetColor red,green,blue
v# = Sqr(vx * vx + vy * vy)
'DrawText v,x,y
SetLineWidth 3
For i = 0 To 8
DrawLine oldpos[i * 2] , oldpos[i * 2 + 1] , oldpos[i * 2 + 2] , oldpos[i * 2 + 3]
Next
'DrawOval x-4,y-4,8,8
End Method
Method die()
things.remove Self
End Method
End Type
Global attractors:tlist=New tlist
Type attractor
Field x# , y# , mass#
Method New()
attractors.addlast Self
End Method
Function create:attractor(x# , y# , mass#)
a:attractor = New attractor
a.x = x
a.y = y
a.mass = mass
Return a
End Function
Method update()
For t:thing = EachIn things
dx# = x - t.x
dy# = y - t.y
d# = Sqr(dx * dx + dy * dy)
dx:/ d
dy:/ d
iv# = t.vx * dx + t.vy * dy
'DrawText v,t.x,t.y
f# = - Sgn(iv) * mass * 100 / d
v# = Sqr(t.vx * t.vx + t.vy * t.vy)
'If Abs(f)>v f=0
t.vx:+ f * dx
t.vy:+ f * dy
nv#=Sqr(t.vx*t.vx+t.vy*t.vy)
t.vx:* v / nv
t.vy:* v / nv
If d < 15
't.die()
'an# = Rand(360)
't.create(400+Cos(an)*600,400+Sin(an)*600,Cos(-an)*9,Sin(-an)*9)
EndIf
Next
End Method
Method draw()
SetColor 100,100,100
DrawOval x - 10*mass , y - 10*mass , 20*mass , 20*mass
End Method
End Type
SeedRnd MilliSecs()
Function setup()
things=New tlist
For c = 1 To 30
t:thing = thing.create(Rand(800) , Rand(800) , Rnd( - 3 , 3) , Rnd( - 3 , 3))
Next
attractors=New tlist
n=Rand(3 , 5)
For c = 1 To n
attractor.create(Rand(100 , 700) , Rand(100 , 700) , Rand(.2 , 2) )
Next
End Function
AppTitle="Where did you learn physics?"
Graphics 800 , 800 , 0
SetBlend ALPHABLEND
SetClsColor 255 , 255 , 255
Cls
setup()
selected:attractor = Null
ox# = 0
oy# = 0
colour = Rand(1 , 7)
If colour & 1 cred = 255 Else cred = 150
If colour & 2 cgreen = 255 Else cgreen = 150
If colour & 4 cblue = 255 Else cblue = 150
filltime = 999
filldir = -1
While Not KeyHit(KEY_ESCAPE)
mx = MouseX()
my = MouseY()
For t:thing = EachIn things
t.update()
t.draw()
Next
For a:attractor = EachIn attractors
a.update()
a.draw()
Next
Rem
If selected
If Not MouseDown(1)
selected = Null
EndIf
selected.x = mx + ox
selected.y = my + oy
Else
If MouseDown(1)
mindist#=-1
For a:attractor=EachIn attractors
dx# = a.x - mx
dy# = a.y - my
d# = dx * dx + dy * dy
'DrawText d,x,y+10
If d < mindist Or mindist = - 1
mindist=d
selected = a
ox# = dx
oy# = dy
EndIf
Next
EndIf
EndIf
EndRem
If MouseHit(2) Or MouseHit(1)
filldir = - 1
filltime = 0
EndIf
If Rand(500)=1 And filltime>0
colour = Rand(1 , 7)
If colour & 1 cred = 255 Else cred = 150
If colour & 2 cgreen = 255 Else cgreen = 150
If colour & 4 cblue = 255 Else cblue = 150
EndIf
SetBlend ALPHABLEND
filltime:+filldir
If filltime>0
SetAlpha .01
Else
SetAlpha -filltime*.01
EndIf
If filltime = - 100
filldir = 1
setup()
EndIf
If filltime=1000
filldir=-1
EndIf
SetColor cred,cgreen,cblue
DrawRect 0 , 0 , 800 , 800
SetAlpha 1
'DrawRect 0,0,10,10
SetBlend ALPHABLEND
SetColor 255,255,255
Flip
'Cls
Wend