forked from concord-consortium/agentscript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgridpath.html
112 lines (102 loc) · 3.54 KB
/
gridpath.html
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
<html>
<head>
<title>Grid Path Model</title>
<script src="../lib/agentscript.js"></script>
<script src="../tools/coffee-script.js"></script>
<script type="text/coffeescript">
u = ABM.Util # ABM.Util alias
class MyModel extends ABM.Model
setup: ->
# This model is unusual in that all its drawing is done
# by the single turtle stamping its image and label
# and drawing a path via its pen being down.
@refreshPatches = false # for static patches
@refreshTurtles = false # for static turtles, use drawing layer
@refreshLinks = false # not need, no links .. emphasize drawing-only
# Create a no-op shape, only used for label:
ABM.Shapes.add "empty", false, (c) ->
# Set font, use default textAlign, textBaseline (centered)
u.ctxTextParams @contexts.drawing, "12px sans-serif"
# globals
@prob = 1
# defaults
@turtles.setDefault "labelColor", [255,255,255]
@turtles.setDefault "shape", "circle"
@turtles.setDefault "color", ([210,0,0])
@turtles.setDefault "size", .5
@turtles.setDefault "hidden", true # We're using drawing ctx & stamp()
#@turtles.setDefault "heading", 0 # override promotion to random angle
@anim.setRate 10
for p in @patches
p.color = u.randomGray 160, 190
p.occupied = false
@drawGrid [0,0,0]
[@walker, @printer] = @turtles.create(2)
@printer.shape = "empty"
p0 = @patches[0]
@walker.moveTo p0
@walker.penDown = true
@walker.penSize = 2.5
@printer.moveTo p0
@walker.p.occupied = true
step: ->
@floodFill()
p = @okNeighbors() # @asSet (n for n in @walker.p.n4 when n.ok)
n = p.length
@prob /= n
@walker.stamp()
@walker.moveTo p.oneOf()
@walker.p.occupied = true
@printer.label = n #""+ n
@printer.stamp()
@printer.moveTo @walker# unless @done()
if @done()
@walker.stamp()
@printer.label = @okNeighbors().length;
@printer.stamp()
console.log "prob: #{@prob}"
@stop()
okNeighbors: -> @asSet (n for n in @walker.p.n4 when n.ok)
done: -> @patches.last().occupied
drawGrid: (c) ->
a = @turtles.create(1)[0]
a.color = c
a.penSize *= .4
a.heading = Math.PI/2
maxX = @patches.maxX; maxY = @patches.maxY
for x in [0..maxX]
a.setXY(x,0); a.penDown = true; a.forward maxY; a.penDown = false
a.heading = 0
for y in [0..maxY]
a.setXY(0,y); a.penDown = true; a.forward maxX; a.penDown = false
a.die()
floodFill: ->
p.ok = false for p in @patches
pset = [@patches.last()]
while pset.length > 0
p.ok = true for p in pset
pnext = @asSet u.flatten (p.n4 for p in pset)
pnext.sortById().uniq()
pset = (n for n in pnext when not n.ok and not n.occupied)
null
# div, patchSize, minX, maxX, minY, maxY, isTorus, hasNeighbors
# Defaults: 13, -16, 16, -16, 16, false, true
# model = new MyModel "layers", 30, 0,10, 0,10
# model.setRootVars() # Debug: Put Model vars in global name space
# model.start() # Run model immediately after startup initialization
new MyModel({
div: "layers",
size: 50,
minX: 0,
maxX: 9,
minY: 0,
maxY: 9
})
.start()
.debug()
</script>
</head>
<body>
<div id="layers"></div>
</body>
</html>