Repository navigation
Expand file tree
/
Copy pathpatch.html
More file actions
315 lines (215 loc) · 15 KB
/
Copy pathpatch.html
File metadata and controls
315 lines (215 loc) · 15 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
<!DOCTYPE html>
<html>
<head>
<title>patch.coffee</title>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<meta name="viewport" content="width=device-width, target-densitydpi=160dpi, initial-scale=1.0; maximum-scale=1.0; user-scalable=0;">
<link rel="stylesheet" media="all" href="docco.css" />
</head>
<body>
<div id="container">
<div id="background"></div>
<ul class="sections">
<li id="title">
<div class="annotation">
<h1>patch.coffee</h1>
</div>
</li>
<li id="section-1">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-1">¶</a>
</div>
<h3 id="patch">Patch</h3>
</div>
</li>
<li id="section-2">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-2">¶</a>
</div>
<p>Class Patch instances represent a rectangle on a grid. They hold variables
that are in the patches the turtles live on. The set of all patches (@model.patches)
is the world on which the turtles live and the model runs.</p>
</div>
<div class="content"><div class='highlight'><pre><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Patch</span></span></pre></div></div>
</li>
<li id="section-3">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-3">¶</a>
</div>
<p>Constructor & Class Variables:</p>
<ul>
<li>id: unique id, promoted by agentset create() factory method</li>
<li>breed: the agentset this agent belongs to</li>
<li>x,y: position on the patch grid, in patch coordinates</li>
<li>color: the color of the patch as an RGBA array, A optional.</li>
<li>hidden: whether or not to draw this patch</li>
<li>label: text for the patch</li>
<li>labelColor: the color of my label text</li>
<li>labelOffset:the x,y offset of my label from my x,y location</li>
<li>n,n4: adjacent neighbors: n: 8 patches, n4: N,E,S,W patches.</li>
<li>pRect: cached rect for performance</li>
</ul>
<p>Patches may not need their neighbors, thus we use a default
of none. n and n4 are promoted by the Patches agentset
if world.neighbors is true, the default.</p>
</div>
<div class="content"><div class='highlight'><pre>
<span class="hljs-attribute">id</span>: <span class="hljs-literal">null</span> <span class="hljs-comment"># unique id, promoted by agentset create factory method</span>
<span class="hljs-attribute">breed</span>: <span class="hljs-literal">null</span> <span class="hljs-comment"># set by the agentSet owning this patch</span>
<span class="hljs-attribute">x</span>:<span class="hljs-literal">null</span>; <span class="hljs-attribute">y</span>:<span class="hljs-literal">null</span> <span class="hljs-comment"># The patch position in the patch grid</span>
<span class="hljs-attribute">n</span>:<span class="hljs-literal">null</span>; <span class="hljs-attribute">n4</span>:<span class="hljs-literal">null</span> <span class="hljs-comment"># The neighbors, n: 8, n4: 4. null OK if model doesn't need them.</span></pre></div></div>
</li>
<li id="section-4">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-4">¶</a>
</div>
<p>color: [0,0,0] # The patch color</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-attribute">hidden</span>: <span class="hljs-literal">false</span> <span class="hljs-comment"># draw me?</span>
<span class="hljs-attribute">label</span>: <span class="hljs-literal">null</span> <span class="hljs-comment"># text for the patch</span></pre></div></div>
</li>
<li id="section-5">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-5">¶</a>
</div>
<p>labelColor: [0,0,0] # text color</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-attribute">labelOffset</span>: [<span class="hljs-number">0</span>,<span class="hljs-number">0</span>] <span class="hljs-comment"># text offset from the patch center</span>
<span class="hljs-attribute">pRect</span>: <span class="hljs-literal">null</span> <span class="hljs-comment"># Performance: cached rect of neighborhood larger than n.</span></pre></div></div>
</li>
<li id="section-6">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-6">¶</a>
</div>
<p>New Patch: Just set x,y. Neighbors set by Patches constructor if needed.</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-attribute">constructor</span>: <span class="hljs-function"><span class="hljs-params">(<span class="hljs-property">@x</span>, <span class="hljs-property">@y</span>)</span> -></span></pre></div></div>
</li>
<li id="section-7">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-7">¶</a>
</div>
<p>Return a string representation of the patch.</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-attribute">toString</span>: <span class="hljs-function">-></span> <span class="hljs-string">"{id:<span class="hljs-subst">#{<span class="hljs-property">@id</span>}</span> xy:<span class="hljs-subst">#{[<span class="hljs-property">@x</span>,<span class="hljs-property">@y</span>]}</span> c:<span class="hljs-subst">#{<span class="hljs-property">@color</span>}</span>}"</span></pre></div></div>
</li>
<li id="section-8">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-8">¶</a>
</div>
<p>Set patch color to <code>c</code> scaled by <code>s</code>. Usage:</p>
<pre><code>p.scaleColor p.color, <span class="hljs-number">.8</span> <span class="hljs-comment"># reduce patch color by .8</span>
p.scaleColor <span class="hljs-property">@foodColor</span>, p.foodPheromone <span class="hljs-comment"># ants model</span>
</code></pre><p>Promotes color if currently using the default.</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-attribute">scaleColor</span>: <span class="hljs-function"><span class="hljs-params">(c, s)</span> -></span></pre></div></div>
</li>
<li id="section-9">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-9">¶</a>
</div>
<p>@color = u.clone @color unless @hasOwnProperty(“color”)
u.scaleColor c, s, @color</p>
</div>
<div class="content"><div class='highlight'><pre> u.deprecated <span class="hljs-string">"Patch.scaleColor: use ColorMaps ramps or closestColor"</span>
<span class="hljs-property">@color</span> = ColorMaps.scaleColor(c, s)
<span class="hljs-attribute">scaleOpacity</span>: <span class="hljs-function"><span class="hljs-params">(c, s)</span> -></span>
u.deprecated <span class="hljs-string">"Patch.scaleOpacity: use ColorMaps ramps"</span>
<span class="hljs-property">@color</span> = u.scaleOpacity c, s, <span class="hljs-property">@color</span></pre></div></div>
</li>
<li id="section-10">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-10">¶</a>
</div>
<p>@color = u.clone @color unless @.hasOwnProperty(“color”)
u.scaleOpacity c, s, @color</p>
</div>
</li>
<li id="section-11">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-11">¶</a>
</div>
<p>Draw the patch and its text label if there is one.</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-attribute">draw</span>: <span class="hljs-function"><span class="hljs-params">(ctx)</span> -></span>
u.deprecated <span class="hljs-string">"Patch.draw not used, Patches.draw uses pixels"</span>
ctx.fillStyle = <span class="hljs-property">@color</span>.css <span class="hljs-comment"># u.colorStr @color</span>
ctx.fillRect <span class="hljs-property">@x</span>-<span class="hljs-number">.5</span>, <span class="hljs-property">@y</span>-<span class="hljs-number">.5</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>
<span class="hljs-keyword">if</span> <span class="hljs-property">@label</span>? <span class="hljs-comment"># REMIND: should be 2nd pass.</span>
[x,y] = <span class="hljs-property">@breed</span>.patchXYtoPixelXY <span class="hljs-property">@x</span>, <span class="hljs-property">@y</span>
u.ctxDrawText ctx, <span class="hljs-property">@label</span>,
x+<span class="hljs-property">@labelOffset</span>[<span class="hljs-number">0</span>], y+<span class="hljs-property">@labelOffset</span>[<span class="hljs-number">1</span>], <span class="hljs-property">@labelColor</span>.css</pre></div></div>
</li>
<li id="section-12">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-12">¶</a>
</div>
<p>Return the members of the agentset within radius of me</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-attribute">inRadius</span>: <span class="hljs-function"><span class="hljs-params">(agentSet, radius)</span> -></span> agentSet.inRadius @, radius</pre></div></div>
</li>
<li id="section-13">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-13">¶</a>
</div>
<p>Return an array of the turtles/breeds on this patch.
If patches.cacheTurtlesHere has created an @turtles instance
variable for the patches, turtles will add/remove themselves
as they move from patch to patch.</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-attribute">turtlesHere</span>: <span class="hljs-function">-></span>
u.deprecated <span class="hljs-string">"Patch.turtlesHere: make caching default"</span> <span class="hljs-keyword">unless</span> <span class="hljs-property">@turtles</span>
<span class="hljs-property">@turtles</span> ? (a <span class="hljs-keyword">for</span> a <span class="hljs-keyword">in</span> <span class="hljs-property">@model</span>.turtles <span class="hljs-keyword">when</span> a.p <span class="hljs-keyword">is</span> @)
<span class="hljs-attribute">breedsHere</span>: <span class="hljs-function"><span class="hljs-params">(breed)</span> -></span>
(a <span class="hljs-keyword">for</span> a <span class="hljs-keyword">in</span> <span class="hljs-property">@turtlesHere</span>() <span class="hljs-keyword">when</span> a.breed <span class="hljs-keyword">is</span> breed)</pre></div></div>
</li>
<li id="section-14">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-14">¶</a>
</div>
<p>Returns true if this patch is on the edge of the grid.</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-attribute">isOnEdge</span>: <span class="hljs-function">-></span>
<span class="hljs-property">@x</span> <span class="hljs-keyword">is</span> <span class="hljs-property">@breed</span>.minX <span class="hljs-keyword">or</span> <span class="hljs-property">@x</span> <span class="hljs-keyword">is</span> <span class="hljs-property">@breed</span>.maxX <span class="hljs-keyword">or</span> \
<span class="hljs-property">@y</span> <span class="hljs-keyword">is</span> <span class="hljs-property">@breed</span>.minY <span class="hljs-keyword">or</span> <span class="hljs-property">@y</span> <span class="hljs-keyword">is</span> <span class="hljs-property">@breed</span>.maxY</pre></div></div>
</li>
<li id="section-15">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-15">¶</a>
</div>
<p>Factory: Create num new turtles on this patch. The optional init
function is called on the new turtle after inserting in its agentset.</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-attribute">sprout</span>: <span class="hljs-function"><span class="hljs-params">(num = <span class="hljs-number">1</span>, breed = <span class="hljs-property">@model</span>.turtles, init = ->)</span> -></span>
breed.create num, <span class="hljs-function"><span class="hljs-params">(a)</span> =></span> <span class="hljs-comment"># fat arrow so that @ = this patch</span>
a.setXY <span class="hljs-property">@x</span>, <span class="hljs-property">@y</span>; init(a); a</pre></div></div>
</li>
<li id="section-16">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-16">¶</a>
</div>
<p>use colorMixin to setup colors</p>
</div>
<div class="content"><div class='highlight'><pre>colorMixin(Patch,<span class="hljs-string">"color"</span>,<span class="hljs-string">"black"</span>)
colorMixin(Patch,<span class="hljs-string">"labelColor"</span>,<span class="hljs-string">"black"</span>)</pre></div></div>
</li>
</ul>
</div>
</body>
</html>