Repository navigation
Expand file tree
/
Copy pathpatches.html
More file actions
789 lines (533 loc) · 40.3 KB
/
Copy pathpatches.html
File metadata and controls
789 lines (533 loc) · 40.3 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
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
<!DOCTYPE html>
<html>
<head>
<title>patches.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>patches.coffee</h1>
</div>
</li>
<li id="section-1">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-1">¶</a>
</div>
<h3 id="patches">Patches</h3>
</div>
</li>
<li id="section-2">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-2">¶</a>
</div>
<p>Class Patches is a singleton 2D matrix of Patch instances, each patch
representing a 1x1 square in patch coordinates (via 2D coord transforms).</p>
<p>From @model.world, set in Model:</p>
<ul>
<li>size: pixel h/w of each patch.</li>
<li>minX/maxX: min/max x coord in patch coords</li>
<li>minY/maxY: min/max y coord in patch coords</li>
<li>numX/numY: width/height of grid.</li>
<li>isTorus: true if coord system wraps around at edges</li>
<li>hasNeighbors: true if each patch caches its neighbors</li>
</ul>
</div>
<div class="content"><div class='highlight'><pre><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Patches</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">AgentSet</span></span>
<span class="hljs-attribute">drawWithPixels</span>: <span class="hljs-literal">true</span> <span class="hljs-comment"># Experimental class variable</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: super creates the empty AgentSet instance and installs
the agentClass (breed) variable shared by all the Patches in this set.
Patches are created from top-left to bottom-right to match data sets.</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-attribute">constructor</span>: <span class="hljs-function">-></span> <span class="hljs-comment"># model, agentClass, name, mainSet</span>
<span class="hljs-keyword">super</span> <span class="hljs-comment"># call super with all the args I was called with</span>
<span class="hljs-property">@monochrome</span> = <span class="hljs-literal">false</span> <span class="hljs-comment"># set to true to optimize patches all default color</span>
@[k] = v <span class="hljs-keyword">for</span> own k,v <span class="hljs-keyword">of</span> <span class="hljs-property">@model</span>.world <span class="hljs-comment"># add world items to patches</span>
<span class="hljs-property">@populate</span>() <span class="hljs-keyword">unless</span> <span class="hljs-property">@mainSet</span>?</pre></div></div>
</li>
<li id="section-4">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-4">¶</a>
</div>
<p>Setup patch world from world parameters.
Note that this is done as separate method so like other agentsets,
patches are started up empty and filled by “create” calls.</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-attribute">populate</span>: <span class="hljs-function">-></span> <span class="hljs-comment"># TopLeft to BottomRight, exactly as canvas imagedata</span>
<span class="hljs-keyword">for</span> y <span class="hljs-keyword">in</span> [<span class="hljs-property">@maxY</span>..<span class="hljs-property">@minY</span>] <span class="hljs-keyword">by</span> -<span class="hljs-number">1</span>
<span class="hljs-keyword">for</span> x <span class="hljs-keyword">in</span> [<span class="hljs-property">@minX</span>..<span class="hljs-property">@maxX</span>] <span class="hljs-keyword">by</span> <span class="hljs-number">1</span>
<span class="hljs-property">@add</span> <span class="hljs-keyword">new</span> <span class="hljs-property">@agentClass</span> x, y
<span class="hljs-property">@setNeighbors</span>() <span class="hljs-keyword">if</span> <span class="hljs-property">@hasNeighbors</span>
<span class="hljs-property">@setPixels</span>() <span class="hljs-keyword">if</span> <span class="hljs-property">@model</span>.div? <span class="hljs-comment"># setup off-page canvas for pixel ops</span></pre></div></div>
</li>
<li id="section-5">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-5">¶</a>
</div>
<p>Have patches cache the turtles currently on them.
Optimizes p.turtlesHere method.
Call before first turtle is created.</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-attribute">cacheTurtlesHere</span>: <span class="hljs-function">-></span> p.turtles = [] <span class="hljs-keyword">for</span> p <span class="hljs-keyword">in</span> @; <span class="hljs-literal">null</span></pre></div></div>
</li>
<li id="section-6">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-6">¶</a>
</div>
<p>Draw patches using scaled image of colors. Note anti-aliasing may occur
if browser does not support smoothing flags.</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-attribute">usePixels</span>: <span class="hljs-function"><span class="hljs-params">()</span> -></span>
u.deprecated <span class="hljs-string">"Patches.usePixels: pixels always used (color.pixel)"</span></pre></div></div>
</li>
<li id="section-7">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-7">¶</a>
</div>
<p>Optimization: Cache a single set by modeler for use by patchRect,
inCone, inRect, inRadius. Ex: flock demo model’s vision rect.</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-attribute">cacheRect</span>: <span class="hljs-function"><span class="hljs-params">(radius, meToo=<span class="hljs-literal">true</span>)</span> -></span>
<span class="hljs-keyword">for</span> p <span class="hljs-keyword">in</span> @
p.pRect = <span class="hljs-property">@patchRect</span> p, radius, radius, meToo
p.pRect.radius = radius
<span class="hljs-literal">null</span> <span class="hljs-comment"># avoid CS returning huge array!</span></pre></div></div>
</li>
<li id="section-8">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-8">¶</a>
</div>
<p>Install neighborhoods in patches</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-attribute">setNeighbors</span>: <span class="hljs-function">-></span>
<span class="hljs-keyword">for</span> p <span class="hljs-keyword">in</span> @
p.n = <span class="hljs-property">@patchRect</span> p, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-literal">false</span>
p.n4 = <span class="hljs-property">@asSet</span> (n <span class="hljs-keyword">for</span> n <span class="hljs-keyword">in</span> p.n <span class="hljs-keyword">when</span> n.x <span class="hljs-keyword">is</span> p.x <span class="hljs-keyword">or</span> n.y <span class="hljs-keyword">is</span> p.y)
<span class="hljs-literal">null</span> <span class="hljs-comment"># radius # avoid CS returning huge array!</span></pre></div></div>
</li>
<li id="section-9">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-9">¶</a>
</div>
<p>Setup pixels used for <code>drawScaledPixels</code> and <code>importColors</code></p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-attribute">setPixels</span>: <span class="hljs-function">-></span>
ctx = <span class="hljs-property">@model</span>.contexts.patches
u.setCtxSmoothing ctx, <span class="hljs-literal">false</span> <span class="hljs-comment"># crisp rendering</span>
<span class="hljs-keyword">if</span> <span class="hljs-property">@size</span> <span class="hljs-keyword">is</span> <span class="hljs-number">1</span>
<span class="hljs-keyword">then</span> <span class="hljs-property">@pixelsCtx</span> = ctx
<span class="hljs-keyword">else</span> <span class="hljs-property">@pixelsCtx</span> = u.createCtx <span class="hljs-property">@numX</span>, <span class="hljs-property">@numY</span>
<span class="hljs-property">@pixelsImageData</span> = <span class="hljs-property">@pixelsCtx</span>.getImageData(<span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-property">@numX</span>, <span class="hljs-property">@numY</span>)
<span class="hljs-property">@pixelsData</span> = <span class="hljs-property">@pixelsImageData</span>.data
<span class="hljs-property">@pixelsData32</span> = <span class="hljs-keyword">new</span> Uint32Array <span class="hljs-property">@pixelsData</span>.buffer <span class="hljs-keyword">if</span> <span class="hljs-property">@drawWithPixels</span></pre></div></div>
</li>
<li id="section-10">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-10">¶</a>
</div>
<p>Draw patches. Three cases:</p>
<ul>
<li>Pixels: use pixel manipulation rather than canvas draws</li>
<li>Monochrome: just fill canvas w/ patch default</li>
<li>Otherwise: just draw each patch individually</li>
</ul>
</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>
<span class="hljs-keyword">if</span> <span class="hljs-property">@monochrome</span>
<span class="hljs-keyword">then</span> u.fillCtx ctx, <span class="hljs-property">@agentClass</span>::color
<span class="hljs-keyword">else</span> <span class="hljs-property">@drawScaledPixels</span> ctx</pre></div></div>
</li>
<li id="section-11">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-11">¶</a>
</div>
<h4 id="patch-grid-coord-system-utilities-">Patch grid coord system utilities:</h4>
</div>
</li>
<li id="section-12">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-12">¶</a>
</div>
<p>Return the patch id/index given integer x,y in patch coords</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-attribute">patchIndex</span>: <span class="hljs-function"><span class="hljs-params">(x,y)</span> -></span> x-<span class="hljs-property">@minX</span> + <span class="hljs-property">@numX</span>*(<span class="hljs-property">@maxY</span>-y)</pre></div></div>
</li>
<li id="section-13">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-13">¶</a>
</div>
<p>Return the patch at matrix position x,y where
x & y are both valid integer patch coordinates.</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-attribute">patchXY</span>: <span class="hljs-function"><span class="hljs-params">(x,y)</span> -></span> @[<span class="hljs-property">@patchIndex</span> x,y]</pre></div></div>
</li>
<li id="section-14">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-14">¶</a>
</div>
<p>Return x,y float values to be between min/max patch coord values</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-attribute">clamp</span>: <span class="hljs-function"><span class="hljs-params">(x,y)</span> -></span> [u.clamp(x, <span class="hljs-property">@minXcor</span>, <span class="hljs-property">@maxXcor</span>), u.clamp(y, <span class="hljs-property">@minYcor</span>, <span class="hljs-property">@maxYcor</span>)]</pre></div></div>
</li>
<li id="section-15">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-15">¶</a>
</div>
<p>Return x,y float values to be modulo min/max patch coord values.</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-attribute">wrap</span>: <span class="hljs-function"><span class="hljs-params">(x,y)</span> -></span> [u.wrap(x, <span class="hljs-property">@minXcor</span>, <span class="hljs-property">@maxXcor</span>), u.wrap(y, <span class="hljs-property">@minYcor</span>, <span class="hljs-property">@maxYcor</span>)]</pre></div></div>
</li>
<li id="section-16">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-16">¶</a>
</div>
<p>Return x,y float values to be between min/max patch values
using either clamp/wrap above according to isTorus topology.</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-attribute">coord</span>: <span class="hljs-function"><span class="hljs-params">(x,y)</span> -></span> <span class="hljs-comment">#returns a valid world coord (real, not int)</span>
<span class="hljs-keyword">if</span> <span class="hljs-property">@isTorus</span> <span class="hljs-keyword">then</span> <span class="hljs-property">@wrap</span> x,y <span class="hljs-keyword">else</span> <span class="hljs-property">@clamp</span> x,y</pre></div></div>
</li>
<li id="section-17">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-17">¶</a>
</div>
<p>Return true if on world or torus, false if non-torus and off-world</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-attribute">isOnWorld</span>: <span class="hljs-function"><span class="hljs-params">(x,y)</span> -></span> <span class="hljs-property">@isTorus</span> <span class="hljs-keyword">or</span> (<span class="hljs-property">@minXcor</span><=x<=<span class="hljs-property">@maxXcor</span> <span class="hljs-keyword">and</span> <span class="hljs-property">@minYcor</span><=y<=<span class="hljs-property">@maxYcor</span>)</pre></div></div>
</li>
<li id="section-18">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-18">¶</a>
</div>
<p>Return patch at x,y float values according to topology.</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-attribute">patch</span>: <span class="hljs-function"><span class="hljs-params">(x,y)</span> -></span>
[x,y]=<span class="hljs-property">@coord</span> x,y
x = u.clamp Math.round(x), <span class="hljs-property">@minX</span>, <span class="hljs-property">@maxX</span>
y = u.clamp Math.round(y), <span class="hljs-property">@minY</span>, <span class="hljs-property">@maxY</span>
<span class="hljs-property">@patchXY</span> x, y</pre></div></div>
</li>
<li id="section-19">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-19">¶</a>
</div>
<p>Return a random valid float x,y point in patch space</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-attribute">randomPt</span>: <span class="hljs-function">-></span> [u.randomFloat2(<span class="hljs-property">@minXcor</span>,<span class="hljs-property">@maxXcor</span>), u.randomFloat2(<span class="hljs-property">@minYcor</span>,<span class="hljs-property">@maxYcor</span>)]</pre></div></div>
</li>
<li id="section-20">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-20">¶</a>
</div>
<h4 id="patch-metrics">Patch metrics</h4>
</div>
</li>
<li id="section-21">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-21">¶</a>
</div>
<p>Convert patch measure to pixels</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-attribute">toBits</span>: <span class="hljs-function"><span class="hljs-params">(p)</span> -></span> p*<span class="hljs-property">@size</span></pre></div></div>
</li>
<li id="section-22">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-22">¶</a>
</div>
<p>Convert bit measure to patches</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-attribute">fromBits</span>: <span class="hljs-function"><span class="hljs-params">(b)</span> -></span> b/<span class="hljs-property">@size</span></pre></div></div>
</li>
<li id="section-23">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-23">¶</a>
</div>
<h4 id="patch-utilities">Patch utilities</h4>
</div>
<div class="content"><div class='highlight'><pre>
<span class="hljs-attribute">patchRect</span>: <span class="hljs-function"><span class="hljs-params">(p, dx, dy=dx, meToo=<span class="hljs-literal">true</span>)</span> -></span>
<span class="hljs-keyword">return</span> p.pRect <span class="hljs-keyword">if</span> p.pRect? <span class="hljs-keyword">and</span> (p.pRect.radius <span class="hljs-keyword">is</span> dx) <span class="hljs-keyword">and</span> (dx <span class="hljs-keyword">is</span> dy)
rect = []; <span class="hljs-comment"># REMIND: optimize if no wrapping, rect inside patch boundaries</span>
<span class="hljs-keyword">for</span> y <span class="hljs-keyword">in</span> [p.y-dy..p.y+dy] <span class="hljs-keyword">by</span> <span class="hljs-number">1</span> <span class="hljs-comment"># by 1: perf: avoid bidir JS for loop</span>
<span class="hljs-keyword">for</span> x <span class="hljs-keyword">in</span> [p.x-dx..p.x+dx] <span class="hljs-keyword">by</span> <span class="hljs-number">1</span>
<span class="hljs-keyword">if</span> <span class="hljs-property">@isTorus</span> <span class="hljs-keyword">or</span> (<span class="hljs-property">@minX</span><=x<=<span class="hljs-property">@maxX</span> <span class="hljs-keyword">and</span> <span class="hljs-property">@minY</span><=y<=<span class="hljs-property">@maxY</span>)
<span class="hljs-keyword">if</span> <span class="hljs-property">@isTorus</span>
<span class="hljs-keyword">if</span> x < <span class="hljs-property">@minX</span> <span class="hljs-keyword">then</span> x += <span class="hljs-property">@numX</span> <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> x > <span class="hljs-property">@maxX</span> <span class="hljs-keyword">then</span> x -= <span class="hljs-property">@numX</span>
<span class="hljs-keyword">if</span> y < <span class="hljs-property">@minY</span> <span class="hljs-keyword">then</span> y += <span class="hljs-property">@numY</span> <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> y > <span class="hljs-property">@maxY</span> <span class="hljs-keyword">then</span> y -= <span class="hljs-property">@numY</span>
pnext = <span class="hljs-property">@patchXY</span> x, y <span class="hljs-comment"># much faster than coord()</span>
rect.push pnext <span class="hljs-keyword">if</span> (meToo <span class="hljs-keyword">or</span> p <span class="hljs-keyword">isnt</span> pnext)
<span class="hljs-property">@asSet</span> rect</pre></div></div>
</li>
<li id="section-24">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-24">¶</a>
</div>
<p>Return patches within the patch square.</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-attribute">inRect</span>: <span class="hljs-function"><span class="hljs-params">(agent, radius)</span> -></span> <span class="hljs-comment">#agentSet.inRadius @, radius</span>
<span class="hljs-property">@patchRect</span>( (agent.p ? agent), Math.ceil(radius) )</pre></div></div>
</li>
<li id="section-25">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-25">¶</a>
</div>
<p>Return patches within radius of the given agent (patch or turtle)</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-attribute">inRadius</span>: <span class="hljs-function"><span class="hljs-params">(agent, radius)</span> -></span> <span class="hljs-comment">#agentSet.inRadius @, radius</span>
pset = <span class="hljs-property">@patchRect</span>( (agent.p ? agent), Math.ceil(radius) )
pset.inRadius agent, radius
<span class="hljs-attribute">inCone</span>: <span class="hljs-function"><span class="hljs-params">(agent, radius, angle, heading)</span> -></span>
pset = <span class="hljs-property">@patchRect</span>( (agent.p ? agent), Math.ceil(radius) )
pset.inRadius agent, radius, angle, heading</pre></div></div>
</li>
<li id="section-26">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-26">¶</a>
</div>
<p>Return all the turtles contained in the patchRect.</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-attribute">turtlesOnRect</span>: <span class="hljs-function"><span class="hljs-params">(p, dx, dy=dx)</span> -></span>
<span class="hljs-property">@turtlesOnPatches</span> <span class="hljs-property">@patchRect</span>(p, dx, dy, <span class="hljs-literal">true</span>)
<span class="hljs-attribute">turtlesOnPatches</span>: <span class="hljs-function"><span class="hljs-params">(patches)</span> -></span>
array = []
<span class="hljs-keyword">if</span> patches.length <span class="hljs-keyword">isnt</span> <span class="hljs-number">0</span>
u.error <span class="hljs-string">"turtlesOnPatches: no cached turtles."</span> <span class="hljs-keyword">if</span> <span class="hljs-keyword">not</span> patches[<span class="hljs-number">0</span>].turtles?</pre></div></div>
</li>
<li id="section-27">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-27">¶</a>
</div>
<p>Use push.apply, not concat, see:
<a href="http://jsperf.com/apply-push-vs-concat-array">jsPerf</a></p>
</div>
<div class="content"><div class='highlight'><pre> Array.prototype.push.apply(array, p.turtles) <span class="hljs-keyword">for</span> p <span class="hljs-keyword">in</span> patches
<span class="hljs-property">@asSet</span> array</pre></div></div>
</li>
<li id="section-28">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-28">¶</a>
</div>
<p>Return all the unique patches the agentset or turtle is on.</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-attribute">patchesOf</span>: <span class="hljs-function"><span class="hljs-params">(aset)</span> -></span>
<span class="hljs-keyword">return</span> <span class="hljs-property">@asSet</span>([aset.p ? aset]) <span class="hljs-keyword">unless</span> aset.length?
<span class="hljs-property">@asSet</span>( ((a.p ? a) <span class="hljs-keyword">for</span> a <span class="hljs-keyword">in</span> aset) ).sortById().uniq()
<span class="hljs-attribute">turtlesOf</span>: <span class="hljs-function"><span class="hljs-params">(aset)</span> -></span> <span class="hljs-property">@turtlesOnPatches</span>(<span class="hljs-property">@patchesOf</span>(aset))</pre></div></div>
</li>
<li id="section-29">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-29">¶</a>
</div>
<p>Draws, or “imports” an image URL into the drawing layer.
The image is scaled to fit the drawing layer.</p>
<p>This is an async load, see this
[new Image()]
(<a href="http://javascript.mfields.org/2011/creating-an-image-in-javascript/">http://javascript.mfields.org/2011/creating-an-image-in-javascript/</a>)
tutorial. We draw the image into the drawing layer as
soon as the onload callback executes.</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-attribute">importDrawing</span>: <span class="hljs-function"><span class="hljs-params">(imageSrc, f)</span> -></span>
u.importImage imageSrc, <span class="hljs-function"><span class="hljs-params">(img)</span> =></span> <span class="hljs-comment"># fat arrow, this context</span>
<span class="hljs-property">@installDrawing</span> img
f() <span class="hljs-keyword">if</span> f?</pre></div></div>
</li>
<li id="section-30">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-30">¶</a>
</div>
<p>Direct install image into the given context, not async.</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-attribute">installDrawing</span>: <span class="hljs-function"><span class="hljs-params">(img, ctx=<span class="hljs-property">@model</span>.contexts.drawing)</span> -></span>
u.setIdentity ctx
ctx.drawImage img, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, ctx.canvas.width, ctx.canvas.height
ctx.restore() <span class="hljs-comment"># restore patch transform</span></pre></div></div>
</li>
<li id="section-31">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-31">¶</a>
</div>
<p>Utility function for pixel manipulation. Given a patch, returns the
native canvas index i into the pixel data.
The top-left order simplifies finding pixels in data sets</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-attribute">pixelByteIndex</span>: <span class="hljs-function"><span class="hljs-params">(p)</span> -></span> <span class="hljs-number">4</span>*p.id <span class="hljs-comment"># Uint8</span>
<span class="hljs-attribute">pixelWordIndex</span>: <span class="hljs-function"><span class="hljs-params">(p)</span> -></span> p.id <span class="hljs-comment"># Uint32</span></pre></div></div>
</li>
<li id="section-32">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-32">¶</a>
</div>
<p>Convert pixel location (top/left offset i.e. mouse) to patch coords (float)</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-attribute">pixelXYtoPatchXY</span>: <span class="hljs-function"><span class="hljs-params">(x,y)</span> -></span> [<span class="hljs-property">@minXcor</span>+(x / <span class="hljs-property">@size</span>), <span class="hljs-property">@maxYcor</span>-(y / <span class="hljs-property">@size</span>)]</pre></div></div>
</li>
<li id="section-33">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-33">¶</a>
</div>
<p>Convert patch coords (float) to pixel location (top/left offset i.e. mouse)</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-attribute">patchXYtoPixelXY</span>: <span class="hljs-function"><span class="hljs-params">(x,y)</span> -></span> [(x-<span class="hljs-property">@minXcor</span>)*<span class="hljs-property">@size</span>, (<span class="hljs-property">@maxYcor</span>-y)*<span class="hljs-property">@size</span>]</pre></div></div>
</li>
<li id="section-34">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-34">¶</a>
</div>
<p>Draws, or “imports” an image URL into the patches as their color property.
The drawing is scaled to the number of x,y patches, thus one pixel
per patch. The colors are then transferred to the patches.
Map is a color map, only for gray for now</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-attribute">importColors</span>: <span class="hljs-function"><span class="hljs-params">(imageSrc, f, map)</span> -></span>
u.importImage imageSrc, <span class="hljs-function"><span class="hljs-params">(img)</span> =></span> <span class="hljs-comment"># fat arrow, this context</span>
<span class="hljs-property">@installColors</span>(img, map)
f() <span class="hljs-keyword">if</span> f?</pre></div></div>
</li>
<li id="section-35">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-35">¶</a>
</div>
<p>Direct install image into the patch colors, not async.</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-attribute">installColors</span>: <span class="hljs-function"><span class="hljs-params">(img, map)</span> -></span>
u.setIdentity <span class="hljs-property">@pixelsCtx</span>
<span class="hljs-property">@pixelsCtx</span>.drawImage img, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-property">@numX</span>, <span class="hljs-property">@numY</span> <span class="hljs-comment"># scale if needed</span>
data = <span class="hljs-property">@pixelsCtx</span>.getImageData(<span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-property">@numX</span>, <span class="hljs-property">@numY</span>).data
<span class="hljs-keyword">for</span> p <span class="hljs-keyword">in</span> @
i = <span class="hljs-property">@pixelByteIndex</span> p</pre></div></div>
</li>
<li id="section-36">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-36">¶</a>
</div>
<p>promote initial default</p>
</div>
<div class="content"><div class='highlight'><pre> p.color = <span class="hljs-keyword">if</span> map? <span class="hljs-keyword">then</span> map[i] <span class="hljs-keyword">else</span> [data[i++],data[i++],data[i]]
<span class="hljs-property">@pixelsCtx</span>.restore() <span class="hljs-comment"># restore patch transform</span></pre></div></div>
</li>
<li id="section-37">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-37">¶</a>
</div>
<p>Draw the patches via pixel manipulation rather than 2D drawRect.
See Mozilla pixel <a href="http://goo.gl/Lxliq">manipulation article</a></p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-attribute">drawScaledPixels</span>: <span class="hljs-function"><span class="hljs-params">(ctx)</span> -></span></pre></div></div>
</li>
<li id="section-38">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-38">¶</a>
</div>
<p>u.setIdentity ctx & ctx.restore() only needed if patch size
not 1, pixel ops don’t use transform but @size>1 uses
a drawimage, using a transform</p>
</div>
<div class="content"><div class='highlight'><pre> u.setIdentity ctx <span class="hljs-keyword">if</span> <span class="hljs-property">@size</span> <span class="hljs-keyword">isnt</span> <span class="hljs-number">1</span>
data = <span class="hljs-property">@pixelsData32</span> ? <span class="hljs-property">@pixelsData</span>
<span class="hljs-keyword">if</span> <span class="hljs-property">@pixelsData32</span>?
<span class="hljs-keyword">then</span> data[p.id] = p.color.pixel <span class="hljs-keyword">for</span> p <span class="hljs-keyword">in</span> @
<span class="hljs-keyword">else</span> data.set(p.color, p.id*<span class="hljs-number">4</span>) <span class="hljs-keyword">for</span> p <span class="hljs-keyword">in</span> @
<span class="hljs-property">@pixelsCtx</span>.putImageData <span class="hljs-property">@pixelsImageData</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>
<span class="hljs-keyword">return</span> <span class="hljs-keyword">if</span> <span class="hljs-property">@size</span> <span class="hljs-keyword">is</span> <span class="hljs-number">1</span>
ctx.drawImage <span class="hljs-property">@pixelsCtx</span>.canvas, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-property">@pxWidth</span>, <span class="hljs-property">@pxHeight</span>
ctx.restore() <span class="hljs-keyword">if</span> <span class="hljs-property">@size</span> <span class="hljs-keyword">isnt</span> <span class="hljs-number">1</span></pre></div></div>
</li>
<li id="section-39">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-39">¶</a>
</div>
<p>Diffuse the value of patch variable <code>p.v</code> by distributing <code>rate</code> percent
of each patch’s value of <code>v</code> to its neighbors.
If a color map <code>cMap</code> is given, scale the patch color via variable’s value
If the patch has less than 8 neighbors, return the extra to the patch.</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-attribute">diffuse</span>: <span class="hljs-function"><span class="hljs-params">(v, rate, color)</span> -></span> <span class="hljs-comment"># variable name, diffusion rate, cMap (optional)</span></pre></div></div>
</li>
<li id="section-40">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-40">¶</a>
</div>
<p>zero temp variable if not yet set</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-keyword">unless</span> @[<span class="hljs-number">0</span>]._diffuseNext?
p._diffuseNext = <span class="hljs-number">0</span> <span class="hljs-keyword">for</span> p <span class="hljs-keyword">in</span> @</pre></div></div>
</li>
<li id="section-41">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-41">¶</a>
</div>
<p>pass 1: calculate contribution of all patches to themselves and neighbors</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-keyword">for</span> p <span class="hljs-keyword">in</span> @
dv = p[v]*rate; dv8 = dv/<span class="hljs-number">8</span>; nn = p.n.length
p._diffuseNext += p[v] - dv + (<span class="hljs-number">8</span>-nn)*dv8
n._diffuseNext += dv8 <span class="hljs-keyword">for</span> n <span class="hljs-keyword">in</span> p.n</pre></div></div>
</li>
<li id="section-42">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-42">¶</a>
</div>
<p>pass 2: set new value for all patches, zero temp, modify color if c given</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-keyword">for</span> p <span class="hljs-keyword">in</span> @
p[v] = p._diffuseNext
p._diffuseNext = <span class="hljs-number">0</span>
p.color = ColorMaps.scaleColor color, p[v] <span class="hljs-keyword">if</span> color?
<span class="hljs-literal">null</span> <span class="hljs-comment"># avoid returning copy of @</span></pre></div></div>
</li>
</ul>
</div>
</body>
</html>