-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgussie.coffee
More file actions
701 lines (590 loc) · 20 KB
/
Copy pathgussie.coffee
File metadata and controls
701 lines (590 loc) · 20 KB
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
##....working on changing turtleset to make a copy on .with()$$
# all turtlesets should be copies, and turtles point back to their
# turtlesets so they can delete themselves when they die.
#
#gussie.coffee by jmvidal@gmail.com
# no bugs here, just lots of newts
#
#Coordinates:
# Turtle.xcor and ycor are real-valued and map directly into the underlying canvas.
# Turtle.who is an int, starting at 0
# Patch.pxcor and pycor are ints and count the patches, starting at 0,0 in the top-left
# and incrementing by 1. The
#
patchCanvas = 0
patchContext = 0
turtleCanvas = 0
turtleContext = 0
#The size of the patches, in canvas pixels. Patches must be square
#
patches_size = 10
patches_radius = patches_size / 2
#How many patches there will be in each direction.
max_pxcor = 40
max_pycor = 40
#The canvas width and height
canvas_width = patches_size * max_pxcor
canvas_height = patches_size * max_pycor
#Global counter for the next who number
who = 0
color =
black: "#555555"
white: "#FEFEFE"
red: "#FF0000"
green: "#00FF00"
blue: "#0000FF"
yellow: "#FFFF00"
magenta: "#FF00FF"
cyan: "#00FFFF"
window.color = color
#Add methods to the built-in array
Array::min = ->
Math.min.apply null,this
Array::max = ->
Math.max.apply null,this
Array::one_of = ->
@[Math.floor(Math.random() * @length)]
# Array Remove - By John Resig (MIT Licensed)
# from,to are indeces
Array::remove = (from, to) ->
rest = @slice((to || from) + 1 || @length)
@length = if (from < 0) then (@length + from) else from
@push.apply(@, rest)
# Remove element if it exists.
Array::eliminate = (element) ->
i = @indexOf(element)
if i >= 0
@remove(i)
return @
if (typeof Object.create != 'function')
Object.create = (o) ->
F = ->
F.prototype = o
return new F()
# a Turtle keeps track of all the sets it is in with @_sets
# when it dies it removes itself from all those sets.
#The Turtle
class Turtle
constructor: ->
@_xcor = 100 # @_ means private instance variable: OBEY
@_ycor = 100
@heading = 0
@who = who++
@color = color.red
@myLinks = new Turtleset
@size = 1
@_sets = [] #array of all the Turtlesets that I belong to
@shape = 'default'
turtles.add(@) #all turtles are in the turtles set
# Add me Turtleset tset
addTo: (tset) ->
@_sets.push(tset)
removeFrom: (tset) ->
@_sets.eliminate(tset)
xcor: (x) ->
@_xcor = x if x?
return @_xcor
ycor: (y) ->
@_ycor = y if y?
return @_ycor
key: ->
@who
pxcor: (px) ->
if px?
@xcor(px * patches_size + patches_radius)
return Math.floor (@xcor() / patches_size)
pycor: (py) ->
if py?
@ycor(py * patches_size + patches_radius)
return Math.floor (@ycor() / patches_size)
setxy: (x,y) ->
@xcor(x)
@ycor(y)
setpxy: (x,y) ->
@pxcor(x)
@pycor(y)
myLinks: -> @_myLinks
createLinkWith: (other) ->
if other == @
return @
link = new Link(@,other)
@myLinks.add(link)
other.myLinks.add(link)
return @
linkNeighbors: ->
myself = @
new Turtleset @myLinks.values ->
if @a == myself then @b else @a
do: (f) ->
f.apply(@)
draw: ->
@drawShape()
return @ if not wraparound
# Calculate wraparound coordinates: NOTE: this only works for
# shapes that wrap around ONCE, and no more. It does not handle bigger shapes.
radius = @size * patches_radius
ox = @_xcor
oy = @_ycor
nx = @_xcor
nx = @_xcor - canvas_width if @_xcor + radius > canvas_width
nx = canvas_width + @_xcor if @_xcor - radius < 0
ny = @_ycor
ny = @_ycor - canvas_width if @_ycor + radius > canvas_height
ny = canvas_height + @_ycor if @_ycor - radius < 0
if nx != ox
@xcor(nx)
@drawShape()
@xcor(ox)
if ny != oy
@ycor(ny)
@drawShape()
@ycor(oy)
if nx != ox and ny != oy
@xcor(nx)
@ycor(ny)
@drawShape()
@xcor(ox)
@ycor(oy)
return @
drawShape: ->
turtleContext.save()
turtleContext.fillStyle = @color
turtleContext.translate(Math.round(@xcor()),Math.round(@ycor()))
turtleContext.scale(@size,@size)
turtleContext.rotate(@heading)
if @shape == 'circle'
turtleContext.beginPath()
turtleContext.arc(0,0,patches_radius,0,2*Math.PI)
turtleContext.fill()
else
turtleContext.beginPath()
turtleContext.moveTo(0,0)
turtleContext.lineTo(-patches_radius,-patches_radius)
turtleContext.lineTo(patches_radius,0)
turtleContext.lineTo(-patches_radius,patches_radius)
turtleContext.lineTo(0,0)
turtleContext.fill()
turtleContext.restore()
return @
setHeading: (@heading) ->
return @
dx: ->
return Math.cos @heading
dy: ->
return Math.sin @heading
forward: (distance) ->
distance = distance * patches_size
dx = Math.cos(this.heading) * distance
dy = Math.sin(this.heading) * distance
@xcor(@xcor() + dx)
@ycor(@ycor() + dy)
[x, y] = wrap(@xcor(),@ycor()) if wraparound
@xcor(x)
@ycor(y)
return @
# Return the angle that would make this turtle point to otherx,othery
# We look for the min
towardsxy: (otherx,othery) ->
dx = otherx - @xcor()
dy = othery - @ycor()
if wraparound
#Wraparound fix
if 2 * Math.abs(dx) > canvas_width #it is closer to go around
if dx > 0 #he is to my right
otherx = @xcor() - canvas_width + dx
if dx < 0 #he is to my left
otherx = @xcor() + canvas_width + dx
dx = otherx - @xcor()
if 2 * Math.abs(dy) > canvas_height #it is closer to go around
if dy > 0
othery = @ycor() - canvas_height + dy
if dy < 0
othery = @ycor() + canvas_height + dy
dy = othery - @ycor()
angle = Math.atan (dy / dx)
angle = angle + Math.PI if dx < 0
return angle
towards: (other) ->
return @towardsxy(other.xcor(), other.ycor())
distancexy: (otherx,othery) ->
dx = Math.abs (otherx - @xcor())
dy = Math.abs (othery - @ycor())
return Math.sqrt(Math.pow(Math.min(dx, canvas_width - dx), 2) + Math.pow(Math.min(dy, canvas_height - dy),2) ) / (2 * patches_radius)
distance: (other) ->
@distancexy other.xcor(),other.ycor()
face: (other) ->
@heading = @towards other
return @
#A turtle dies by removing itself from all its @_sets (which includes 'turtles')
die: ->
for tset in @_sets
tset.remove(@)
#remove this turtle from tset turtleset and return that
other: (tset) ->
return tset.minus(@)
window.Turtle = Turtle
class Link extends Turtle
#a,b are start and end Turtle
constructor: (@a,@b)->
@heading = 0
@who = who++
@color = color.black
@directed = false
@size = 1
@_sets = [] #array of all the Turtlesets that I belong to
links.add(@)
fixCoords: ->
@_xcor = @a.xcor()
@_ycor = @a.ycor()
@face(@b) #set my direction
length = @distance @b
@size = length
@forward(@size / 2)
return @
drawShape: ->
turtleContext.save()
turtleContext.fillStyle = @color
turtleContext.strokeStyle = @color
turtleContext.lineWidth = 2 #if this is 1 then horizontal line disappears.
turtleContext.translate(Math.round(@xcor()),Math.round(@ycor()))
turtleContext.rotate(@heading)
turtleContext.beginPath()
startx = -patches_radius * @size #+ patches_radius #hit the end of the circle around b
endx = patches_radius * @size #- patches_radius
turtleContext.moveTo(startx,0)
turtleContext.lineTo(endx,0)
if @directed
tip = patches_radius / 2
turtleContext.lineTo(endx - tip, -tip)
turtleContext.moveTo(endx,0)
turtleContext.lineTo(endx - tip, tip)
turtleContext.stroke()
turtleContext.restore()
return @
#Turtleset stores the turtles in @_turtles as an object
# with turtle.key as the key
#It is a set (no duplicates) based on the key.
#
# @with and other commands return a COPY of this turtleset, but the @_turtles themselves
# are not copied (that would not make sense, we only want one copy of each turtle).
#
class Turtleset
constructor: (array) -> #TODO: check that array is an Array
@_turtles = {}
@size = 0
if array
for turtle in array
@add turtle
add: (turtle) ->
if not @_turtles.hasOwnProperty turtle.key or not @_turtles[turtle.key()]
@size++
turtle.addTo(@)
@_turtles[turtle.key()] = turtle
return @
#Return a new turtleset with all the same turtles except 'turtle'
#The returned turtleset inherits from this one, but with turtle delete (set to undefined)
minus: (turtle) ->
c = new Turtleset
for key,t of @_turtles when t != turtle
c.add t
return c
# Return a new Turtleset which is a copy of this one
copy: ->
c = new Turtleset
for key,turtle of @_turtles
c.add turtle
return c
get: (key) ->
@_turtles[key]
#Returns an array with the values of the given property for all, like 'of'
values: (property) ->
if property instanceof Function
return (property.apply(turtle) for key,turtle of @_turtles)
return (turtle[property] for key,turtle of @_turtles)
count: ->
return @size
one_of : ->
keys = (key for key,turtle of @_turtles) #TODO: @keys optimization
chosenKey = keys[Math.floor(Math.random() * keys.length)]
return @_turtles[chosenKey]
#Returns a turtleset containing all the turtles that have a minimal value for prop
min_of : (prop) ->
vals = @values prop
minVal = vals.min()
return @withPV(prop,minVal)
#Same as min_of
with_min: (prop) ->
return @min_of prop
min_n_of: (prop, n) ->
#Returns one of the turtles with a min value for prop.
# prop can be a function or a string.
min_one_of: (prop) ->
return @min_of(prop).one_of()
max_of: (prop) ->
vals = @values prop
maxVal = vals.max()
return @withPV(prop,maxVal)
with_max: (prop) ->
return @max_of prop
max_one_of: (prop) ->
return @max_of(prop).one_of()
#Return turtles with f, that is, for which f evaluates to true.
with: (f) ->
result = new Turtleset
for key,turtle of @_turtles when turtle?
if f.apply(turtle)
result.add turtle
return result
#Return turtles t for which property has value,
# or, if property is a function, for which property evaluates to true
withPV: (property, value) ->
result = new Turtleset
for key,turtle of @_turtles
if property instanceof Function
if property.apply(turtle) == value
result.add turtle
else if turtle[property] == value
result.add turtle
return result
do: (f) ->
for key,turtle of @_turtles
f.apply(turtle)
# Remove turtle from this turtleset, nothing more.
remove: (turtle) ->
@size--
delete @_turtles[turtle.key()]
draw: ->
turtle.draw() for key,turtle of @_turtles
window.Turtleset = Turtleset
window.turtles = new Turtleset
turtles = window.turtles
turtle = (w) ->
turtles.get(w)
window.links = new Turtleset
links = window.links
#global var where we store a Turtleset of patches
window.patches = 0
patches = window.patches
patch = (x,y) ->
patches.get(x + "-" + y)
class Patch extends Turtle
constructor: (@pxcor, @pycor)->
@xcor(0) # the center point of the patch
@ycor(0)
@pxcor = 0 # OVERRIDE Turtle.pxcor
@pycor = 0 # the patch's position (in patch coordinates)
@pcxcor = 0 #the top-left point of the patch, in pixels, used for drawing
@pcycor = 0
@pcolor = "#AA5555"
@drawnColor = null
@neighbors = null #a turtleset with my neighbors
@who = @key()
@_sets = [patches] #array of all the Turtlesets that I belong to
draw: ->
if not (@drawnColor == @pcolor)
@drawnColor = @pcolor
patchContext.fillStyle = @pcolor
patchContext.fillRect(@pcxcor, @pcycor, patches_size, patches_size)
key: ->
@pxcor + "-" + @pycor
setColor: (@pcolor) ->
neighbors: () ->
return @neighbors
# Returns coordinates that are within the canvas, by wrapping around
wrap = (x,y) ->
x = x % canvas_width
x = canvas_width + x if x < 0
y = y % canvas_height
y = canvas_height + y if y < 0
return [x,y]
# Create all the patches, set window.patches variable
create_patches = () ->
$('#world').attr('width',canvas_width).width(canvas_width)
$('#world').attr('height',canvas_height).height(canvas_height)
$('#patchCanvas').attr('width',canvas_width)
$('#patchCanvas').attr('height',canvas_height)
$('#turtleCanvas').attr('width',canvas_width) #setting it in CSS (.width()) does not work!
$('#turtleCanvas').attr('height',canvas_height)
#create all the patches
window.patches = new Turtleset
patches = window.patches
console.log('making patches')
for x in [0...max_pxcor]
for y in [0...max_pycor]
p = new Patch(x,y)
p.pxcor = x
p.pycor = y
p.pcxcor = x * patches_size
p.pcycor = y * patches_size
p.xcor = p.pcxcor + (patches_size / 2)
p.ycor = p.pcycor + (patches_size / 2)
patches.add p
#set each patch's neighbors
console.log('setting neighbors')
patches = window.patches
patches.do ->
myPxcor = @pxcor
myPycor = @pycor
myPxcorM1 = myPxcor - 1
myPxcorM1 =(max_pxcor - 1) if myPxcorM1 < 0 #assumes torus world
myPxcorP1 = (myPxcor + 1) % max_pxcor
myPycorM1 = myPycor - 1
myPycorM1 = (max_pycor - 1) if myPycorM1 < 0
myPycorP1 = (myPycor + 1) % max_pycor
@neighbors = new Turtleset([
patch(myPxcorM1,myPycor),
patch(myPxcorP1,myPycor),
patch(myPxcor,myPycorM1),
patch(myPxcor,myPycorP1),
patch(myPxcorM1,myPycorM1),
patch(myPxcorM1,myPycorP1),
patch(myPxcorP1,myPycorM1),
patch(myPxcorP1,myPycorP1) ] )
window.Patch = Patch
class Vector
constructor: (@dx,@dy) ->
add: (v) ->
new Vector(@dx+v.dx, @dy+v.dy)
scale: (c) ->
new Vector(@dx*c, @dy*c)
#Layout all the turtles given all the links.
# turtles repel each other 1/d^2
# linked turtles are attracted/repelled to the link's springLength
layout_magspring = (springLength )->
remainingTurtles = turtles.copy()
turtles.do ->
@forces = []
turtles.do ->
from = @
remainingTurtles = remainingTurtles.minus(@)
remainingTurtles.do ->
#apply repulsive force between 'from' and '@'
oldHeading = @heading
@face(from)
hisForce = new Vector(@dx(), @dy())
d = @distance(from)
hisForce = hisForce.scale(1 / (d * d))
from.forces.push(hisForce)
myForce = hisForce.scale(-1)
@forces.push(myForce)
@heading = oldHeading
links.do ->
a = @a
b = @b
@a.do ->
oldHeading = @heading
@face(b)
distanceToOther = @distance(b)
scale = Math.abs(distanceToOther - springLength) / springLength
scale = scale * scale
if distanceToOther > springLength #then, attraction
aForce = new Vector(@dx(),@dy())
aForce = aForce.scale(scale)
@forces.push(aForce)
bForce = aForce.scale(-1)
b.forces.push(bForce)
else #repulsion
bForce = new Vector(@dx(),@dy())
bForce = bForce.scale(scale)
b.forces.push(bForce)
aForce = bForce.scale(-1)
@forces.push(aForce)
@heading = oldHeading
turtles.do ->
@totalForce = @forces.reduce (a,b) ->
a.add(b)
@xcor(@xcor() + @totalForce.dx)
@ycor(@ycor() + @totalForce.dy)
window.layout_magspring = layout_magspring
create_turtles = (num) ->
for i in [0...num]
new Turtle
clear_all = ->
turtles.do( -> @die())
links.do( -> @die())
patches = {}
who = 0
create_patches()
redraw()
window.clear_all = clear_all
animate = true
#Redraw everything in the canvas.
redraw = ->
patches.draw()
turtleContext.clearRect(0,0,turtleCanvas.width(), turtleCanvas.height())
links.do ->
@fixCoords()
links.draw()
turtles.draw()
window.redraw = redraw
#Setup the canvas global variables
$ ->
console.log('I am ready')
#Create the patches, according to the user's need
# pxmax,pymax are the number of patches in each dimension.
# size: is the size of the patches in pixels. Patches are square.
window.make_patches = (p) ->
max_pxcor = p.pxmax
max_pycor = p.pymax
patches_size = p.size
patches_radius = patches_size / 2
canvas_width = patches_size * max_pxcor
canvas_height = patches_size * max_pycor
create_patches()
redraw()
wraparound = true
window.make_world = (p) ->
$world = $('<div class="widget" id="world"><canvas id="patchCanvas"></canvas><canvas id="turtleCanvas"></canvas></div>')
wraparound = p.wraparound if p.wraparound?
$world.css('top', p.top) if p.top?
$world.css('left', p.left) if p.left?
$world.width p.width if p.width?
$world.height p.height if p.height?
$('#frame').append $world
patchCanvas = $('#patchCanvas')
patchContext = patchCanvas[0].getContext('2d')
turtleCanvas = $('#turtleCanvas')
turtleContext = turtleCanvas[0].getContext('2d')
_forever_call = (f, id) ->
fun = ->
if $('#' + id).prop('checked')
f()
setTimeout fun,0
return fun
window.make_button = (p) ->
id = p.id
if p.toggle?
$button = $("<input class=\"widget\" type=\"checkbox\" id=\"#{id}\"/>
<label class=\"widget\" for=\"#{p.id}\">#{p.label}</label>")
else
$button = $("<button class=\"widget\" id=\"#{p.id}\">#{p.label}</button>)")
$('#frame').append $button
if p.toggle?
$button.on('change',_forever_call(p.click,p.id))
else
$button.on('click',p.click)
$button.css('top', p.top) if p.top?
$button.css('left', p.left) if p.left?
$('#'+ p.id).button()
window.make_slider = (p) ->
displayID = p.id + 'Display'
$slider = $("<div class=\"ui-widget widget sliderContainer\"><div id=\"#{p.id}\"></div><p>#{p.label}<span id=\"#{displayID}\"></span></p></div>")
$slider.css('top', p.top) if p.top?
$slider.css('left', p.left) if p.left?
$('#frame').append $slider
$slider.width p.width if p.width?
$slider.height p.height if p.height?
$('#' + p.id).slider
min: p.min
max: p.max
value: p.value
slide: (event,ui) ->
$('#' + displayID).html(ui.value)
create: (event,ui) ->
$('#' + displayID).html(16)
#TODO: Sample programs
# grah coloring: link and layout primitives
# active edges:
# graph-oriented programming: Following Sussman's paper where a node
# holds variables and edges represnt evidence for the vars' values.
# combinatorial auction