Repository navigation
Expand file tree
/
Copy pathturtle.html
More file actions
714 lines (489 loc) · 37.9 KB
/
Copy pathturtle.html
File metadata and controls
714 lines (489 loc) · 37.9 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
<!DOCTYPE html>
<html>
<head>
<title>turtle.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>turtle.coffee</h1>
</div>
</li>
<li id="section-1">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-1">¶</a>
</div>
</div>
</li>
<li id="section-2">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-2">¶</a>
</div>
<p>Class Turtle instances represent the dynamic, behavioral element of modeling.
Each turtle knows the patch it is on, and interacts with that and other
patches, as well as other turtles.</p>
</div>
<div class="content"><div class='highlight'><pre><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Turtle</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 identifier, promoted by agentset create() factory method</li>
<li>breed: the agentset this turtle belongs to</li>
<li>x,y: position on the patch grid, in patch coordinates, default: 0,0</li>
<li>size: size of turtle, in patch coords, default: 1</li>
<li>color: the color of the turtle, default: randomColor</li>
<li>shape: the shape name of the turtle, default: “default”</li>
<li>label: a text label drawn on my instances</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>heading: direction of the turtle, in radians, from x-axis</li>
<li>hidden: whether or not to draw this turtle</li>
<li>p: patch at current x,y location</li>
<li>penDown: true if turtle pen is drawing</li>
<li>penSize: size in pixels of the pen, default: 1 pixel</li>
<li>sprite: an image of the turtle if non null</li>
<li>cacheLinks: if true, keep array of links in/out of me</li>
<li>links: array of links in/out of me. Only used if @cacheLinks is true</li>
</ul>
<p>These class variables are “defaults” and many are “promoted” to instance variables.
To have these be set to a constant for all instances, use breed.setDefault.
This can be a huge savings in memory.</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"># my agentSet, set by the agentSet owning me</span>
<span class="hljs-attribute">x</span>: <span class="hljs-number">0</span>; <span class="hljs-attribute">y</span>:<span class="hljs-number">0</span>; <span class="hljs-attribute">p</span>: <span class="hljs-literal">null</span> <span class="hljs-comment"># my location and the patch I'm on</span>
<span class="hljs-attribute">size</span>: <span class="hljs-number">1</span> <span class="hljs-comment"># my size in patch coords</span>
<span class="hljs-attribute">color</span>: <span class="hljs-literal">null</span> <span class="hljs-comment"># default color, overrides random color if set</span>
<span class="hljs-attribute">strokeColor</span>: <span class="hljs-literal">null</span> <span class="hljs-comment"># color of the border of an turtle</span>
<span class="hljs-attribute">shape</span>: <span class="hljs-string">"default"</span> <span class="hljs-comment"># my shape</span>
<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"># my text</span>
<span class="hljs-attribute">labelColor</span>: [<span class="hljs-number">0</span>,<span class="hljs-number">0</span>,<span class="hljs-number">0</span>] <span class="hljs-comment"># its color</span>
<span class="hljs-attribute">labelOffset</span>: [<span class="hljs-number">0</span>,<span class="hljs-number">0</span>] <span class="hljs-comment"># its offset from my x,y</span>
<span class="hljs-attribute">penDown</span>: <span class="hljs-literal">false</span> <span class="hljs-comment"># if my pen is down, I draw my path between changes in x,y</span>
<span class="hljs-attribute">penSize</span>: <span class="hljs-number">1</span> <span class="hljs-comment"># the pen thickness in pixels</span>
<span class="hljs-attribute">heading</span>: <span class="hljs-literal">null</span> <span class="hljs-comment"># the direction I'm pointed in, in radians</span>
<span class="hljs-attribute">sprite</span>: <span class="hljs-literal">null</span> <span class="hljs-comment"># an image of me for optimized drawing</span>
<span class="hljs-attribute">useSprites</span>: <span class="hljs-literal">false</span> <span class="hljs-comment"># should I use sprites?</span>
<span class="hljs-attribute">cacheLinks</span>: <span class="hljs-literal">false</span> <span class="hljs-comment"># should I keep links to/from me in links array?.</span>
<span class="hljs-attribute">links</span>: <span class="hljs-literal">null</span> <span class="hljs-comment"># array of links to/from me as an endpoint; init by ctor</span>
<span class="hljs-attribute">constructor</span>: <span class="hljs-function">-></span> <span class="hljs-comment"># called by agentSets create factory, not user</span>
<span class="hljs-property">@x</span> = <span class="hljs-property">@y</span> = <span class="hljs-number">0</span>
<span class="hljs-property">@p</span> = <span class="hljs-property">@model</span>.patches.patch <span class="hljs-property">@x</span>, <span class="hljs-property">@y</span>
<span class="hljs-property">@color</span> = u.randomColor() <span class="hljs-keyword">unless</span> <span class="hljs-property">@color</span>? <span class="hljs-comment"># or @useSprites or @hidden</span>
<span class="hljs-property">@heading</span> = u.randomFloat(Math.PI*<span class="hljs-number">2</span>) <span class="hljs-keyword">unless</span> <span class="hljs-property">@heading</span>?
<span class="hljs-property">@p</span>.turtles.push @ <span class="hljs-keyword">if</span> <span class="hljs-property">@p</span>.turtles? <span class="hljs-comment"># @model.patches.cacheTurtlesHere</span>
<span class="hljs-property">@links</span> = [] <span class="hljs-keyword">if</span> <span class="hljs-property">@cacheLinks</span></pre></div></div>
</li>
<li id="section-4">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-4">¶</a>
</div>
<p>Set turtle color to <code>c</code> scaled by <code>s</code>. Usage: see patch.scaleColor</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>
u.deprecated <span class="hljs-string">"Turtle.scaleColor: use ColorMaps ramps or closestColor"</span>
<span class="hljs-property">@color</span> = ColorMaps.scaleColor(c, s)</pre></div></div>
</li>
<li id="section-5">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-5">¶</a>
</div>
<p>@color = u.clone @color unless @hasOwnProperty “color” # promote color to inst var
u.scaleColor c, s, @color</p>
</div>
<div class="content"><div class='highlight'><pre>
<span class="hljs-attribute">scaleOpacity</span>: <span class="hljs-function"><span class="hljs-params">(c, s)</span> -></span>
u.deprecated <span class="hljs-string">"Turtle.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-6">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-6">¶</a>
</div>
<p>@color = u.clone @color unless @hasOwnProperty “color”
u.scaleOpacity c, s, @color</p>
</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 turtle.</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">#{u.aToFixed [<span class="hljs-property">@x</span>,<span class="hljs-property">@y</span>]}</span> c:<span class="hljs-subst">#{<span class="hljs-property">@color</span>.css}</span> h: <span class="hljs-subst">#{h=<span class="hljs-property">@heading</span>.toFixed <span class="hljs-number">2</span>}</span>/<span class="hljs-subst">#{Math.round(u.radToDeg(h))}</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>Place the turtle at the given x,y (floats) in patch coords
using patch topology (isTorus)</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-attribute">setXY</span>: <span class="hljs-function"><span class="hljs-params">(x, y)</span> -></span> <span class="hljs-comment"># REMIND GC problem, 2 arrays</span>
[x0, y0] = [<span class="hljs-property">@x</span>, <span class="hljs-property">@y</span>] <span class="hljs-keyword">if</span> <span class="hljs-property">@penDown</span>
[<span class="hljs-property">@x</span>, <span class="hljs-property">@y</span>] = <span class="hljs-property">@model</span>.patches.coord x, y
p = <span class="hljs-property">@p</span>
<span class="hljs-property">@p</span> = <span class="hljs-property">@model</span>.patches.patch <span class="hljs-property">@x</span>, <span class="hljs-property">@y</span>
<span class="hljs-keyword">if</span> p.turtles? <span class="hljs-keyword">and</span> p <span class="hljs-keyword">isnt</span> <span class="hljs-property">@p</span> <span class="hljs-comment"># @model.patches.cacheTurtlesHere</span>
u.removeItem p.turtles, @
<span class="hljs-property">@p</span>.turtles.push @
<span class="hljs-keyword">if</span> <span class="hljs-property">@penDown</span>
drawing = <span class="hljs-property">@model</span>.drawing
drawing.strokeStyle = <span class="hljs-property">@color</span>.css <span class="hljs-comment"># u.colorStr @color</span>
drawing.lineWidth = <span class="hljs-property">@model</span>.patches.fromBits <span class="hljs-property">@penSize</span>
drawing.beginPath()
drawing.moveTo x0, y0; drawing.lineTo x, y <span class="hljs-comment"># REMIND: euclidean</span>
drawing.stroke()</pre></div></div>
</li>
<li id="section-9">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-9">¶</a>
</div>
<p>Place the turtle at the given patch/turtle location</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-attribute">moveTo</span>: <span class="hljs-function"><span class="hljs-params">(a)</span> -></span> <span class="hljs-property">@setXY</span> a.x, a.y</pre></div></div>
</li>
<li id="section-10">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-10">¶</a>
</div>
<p>Move forward (along heading) d units (patch coords),
using patch topology (isTorus)</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-attribute">forward</span>: <span class="hljs-function"><span class="hljs-params">(d)</span> -></span>
<span class="hljs-property">@setXY</span> <span class="hljs-property">@x</span> + d*Math.cos(<span class="hljs-property">@heading</span>), <span class="hljs-property">@y</span> + d*Math.sin(<span class="hljs-property">@heading</span>)</pre></div></div>
</li>
<li id="section-11">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-11">¶</a>
</div>
<p>Change current heading by rad radians which can be + (left) or - (right).
Returns new heading.</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-attribute">rotate</span>: <span class="hljs-function"><span class="hljs-params">(rad)</span> -></span> <span class="hljs-property">@heading</span> = u.wrap <span class="hljs-property">@heading</span> + rad, <span class="hljs-number">0</span>, Math.PI*<span class="hljs-number">2</span>
<span class="hljs-attribute">right</span>: <span class="hljs-function"><span class="hljs-params">(rad)</span> -></span> <span class="hljs-property">@rotate</span> -rad
<span class="hljs-attribute">left</span>: <span class="hljs-function"><span class="hljs-params">(rad)</span> -></span> <span class="hljs-property">@rotate</span> rad</pre></div></div>
</li>
<li id="section-12">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-12">¶</a>
</div>
<p>Draw the turtle, instanciating a sprite if required</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>
shape = Shapes[<span class="hljs-property">@shape</span>]
rad = <span class="hljs-keyword">if</span> shape.rotate <span class="hljs-keyword">then</span> <span class="hljs-property">@heading</span> <span class="hljs-keyword">else</span> <span class="hljs-number">0</span> <span class="hljs-comment"># radians</span>
<span class="hljs-keyword">if</span> <span class="hljs-property">@sprite</span>? <span class="hljs-keyword">or</span> <span class="hljs-property">@useSprites</span> <span class="hljs-comment"># @breed.useSprites</span>
<span class="hljs-property">@setSprite</span>() <span class="hljs-keyword">unless</span> <span class="hljs-property">@sprite</span>? <span class="hljs-comment"># lazy evaluation of useSprites</span>
Shapes.drawSprite ctx, <span class="hljs-property">@sprite</span>, <span class="hljs-property">@x</span>, <span class="hljs-property">@y</span>, <span class="hljs-property">@size</span>, rad
<span class="hljs-keyword">else</span>
Shapes.draw ctx, shape, <span class="hljs-property">@x</span>, <span class="hljs-property">@y</span>, <span class="hljs-property">@size</span>, rad, <span class="hljs-property">@color</span>, <span class="hljs-property">@strokeColor</span>
<span class="hljs-keyword">if</span> <span class="hljs-property">@label</span>?
[x,y] = <span class="hljs-property">@model</span>.patches.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></pre></div></div>
</li>
<li id="section-13">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-13">¶</a>
</div>
<p>Set an individual turtle’s sprite, synching its color, shape, size</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-attribute">setSprite</span>: <span class="hljs-function"><span class="hljs-params">(sprite)</span>-></span>
<span class="hljs-keyword">if</span> (s=sprite)?
<span class="hljs-property">@sprite</span> = s; <span class="hljs-property">@color</span> = s.color; <span class="hljs-property">@strokeColor</span> = s.strokeColor
<span class="hljs-property">@shape</span> = s.shape; <span class="hljs-property">@size</span> = <span class="hljs-property">@model</span>.patches.fromBits(s.size)
<span class="hljs-keyword">else</span></pre></div></div>
</li>
<li id="section-14">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-14">¶</a>
</div>
<p>@color = u.randomColor() unless @color? # not needed if lazy evaluation</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-property">@sprite</span> = Shapes.shapeToSprite <span class="hljs-property">@shape</span>, <span class="hljs-property">@color</span>,
<span class="hljs-property">@model</span>.patches.toBits(<span class="hljs-property">@size</span>), <span class="hljs-property">@strokeColor</span></pre></div></div>
</li>
<li id="section-15">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-15">¶</a>
</div>
<p>Draw the turtle on the drawing layer, leaving permanent image.</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-attribute">stamp</span>: <span class="hljs-function">-></span> <span class="hljs-property">@draw</span> <span class="hljs-property">@model</span>.drawing</pre></div></div>
</li>
<li id="section-16">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-16">¶</a>
</div>
<p>Return distance in patch coords from me to x,y
using patch topology (isTorus)</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-attribute">distanceXY</span>: <span class="hljs-function"><span class="hljs-params">(x,y)</span> -></span>
<span class="hljs-keyword">if</span> <span class="hljs-property">@model</span>.patches.isTorus
<span class="hljs-keyword">then</span> u.torusDistance <span class="hljs-property">@x</span>, <span class="hljs-property">@y</span>, x, y, <span class="hljs-property">@model</span>.patches.numX, <span class="hljs-property">@model</span>.patches.numY
<span class="hljs-keyword">else</span> u.distance <span class="hljs-property">@x</span>, <span class="hljs-property">@y</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 distance in patch coords from me to given turtle/patch using patch topology.</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-attribute">distance</span>: <span class="hljs-function"><span class="hljs-params">(o)</span> -></span> <span class="hljs-comment"># o any object w/ x,y, patch or turtle</span>
<span class="hljs-property">@distanceXY</span> o.x, o.y</pre></div></div>
</li>
<li id="section-18">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-18">¶</a>
</div>
<p>Return the closest torus topology point of given x,y relative to myself.
Used internally to determine how to draw links between two turtles.
See Util.torusPt.</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-attribute">torusPtXY</span>: <span class="hljs-function"><span class="hljs-params">(x, y)</span> -></span>
u.torusPt <span class="hljs-property">@x</span>, <span class="hljs-property">@y</span>, x, y, <span class="hljs-property">@model</span>.patches.numX, <span class="hljs-property">@model</span>.patches.numY</pre></div></div>
</li>
<li id="section-19">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-19">¶</a>
</div>
<p>Return the closest torus topology point of given turtle/patch
relative to myself. See Util.torusPt.</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-attribute">torusPt</span>: <span class="hljs-function"><span class="hljs-params">(o)</span> -></span>
<span class="hljs-property">@torusPtXY</span> o.x, o.y</pre></div></div>
</li>
<li id="section-20">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-20">¶</a>
</div>
<p>Set my heading towards given turtle/patch using patch topology.</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-attribute">face</span>: <span class="hljs-function"><span class="hljs-params">(o)</span> -></span> <span class="hljs-property">@heading</span> = <span class="hljs-property">@towards</span> o</pre></div></div>
</li>
<li id="section-21">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-21">¶</a>
</div>
<p>Return heading towards x,y using patch topology.</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-attribute">towardsXY</span>: <span class="hljs-function"><span class="hljs-params">(x, y)</span> -></span>
<span class="hljs-keyword">if</span> (ps=<span class="hljs-property">@model</span>.patches).isTorus
<span class="hljs-keyword">then</span> u.torusRadsToward <span class="hljs-property">@x</span>, <span class="hljs-property">@y</span>, x, y, ps.numX, ps.numY
<span class="hljs-keyword">else</span> u.radsToward <span class="hljs-property">@x</span>, <span class="hljs-property">@y</span>, x, y</pre></div></div>
</li>
<li id="section-22">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-22">¶</a>
</div>
<p>Return heading towards given turtle/patch using patch topology.</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-attribute">towards</span>: <span class="hljs-function"><span class="hljs-params">(o)</span> -></span> <span class="hljs-property">@towardsXY</span> o.x, o.y</pre></div></div>
</li>
<li id="section-23">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-23">¶</a>
</div>
<p>Return patch ahead of me by given distance and heading.
Returns null if non-torus and off patch world.
Heading is + for left, - for right.</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-attribute">patchAtHeadingAndDistance</span>: <span class="hljs-function"><span class="hljs-params">(h,d)</span> -></span>
[dx,dy] = u.polarToXY d, h + <span class="hljs-property">@heading</span>
<span class="hljs-property">@patchAt</span> dx,dy
<span class="hljs-attribute">patchLeftAndAhead</span>: <span class="hljs-function"><span class="hljs-params">(dh, d)</span> -></span> <span class="hljs-property">@patchAtHeadingAndDistance</span> dh, d
<span class="hljs-attribute">patchRightAndAhead</span>: <span class="hljs-function"><span class="hljs-params">(dh, d)</span> -></span> <span class="hljs-property">@patchAtHeadingAndDistance</span> -dh, d
<span class="hljs-attribute">patchAhead</span>: <span class="hljs-function"><span class="hljs-params">(d)</span> -></span> <span class="hljs-property">@patchAtHeadingAndDistance</span> <span class="hljs-number">0</span>, d
<span class="hljs-attribute">canMove</span>: <span class="hljs-function"><span class="hljs-params">(d)</span> -></span> <span class="hljs-property">@patchAhead</span>(d)?
<span class="hljs-attribute">patchAt</span>: <span class="hljs-function"><span class="hljs-params">(dx,dy)</span> -></span>
x=<span class="hljs-property">@x</span>+dx; y=<span class="hljs-property">@y</span>+dy
<span class="hljs-keyword">if</span> (ps=<span class="hljs-property">@model</span>.patches).isOnWorld x,y <span class="hljs-keyword">then</span> ps.patch x,y <span class="hljs-keyword">else</span> <span class="hljs-literal">null</span></pre></div></div>
</li>
<li id="section-24">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-24">¶</a>
</div>
<p>Remove myself from the model. Includes removing myself from my
agentset(s) and removing any links I may have.</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-attribute">die</span>: <span class="hljs-function">-></span>
<span class="hljs-property">@breed</span>.remove @
l.die() <span class="hljs-keyword">for</span> l <span class="hljs-keyword">in</span> <span class="hljs-property">@myLinks</span>() <span class="hljs-keyword">by</span> -<span class="hljs-number">1</span>
u.removeItem <span class="hljs-property">@p</span>.turtles, @ <span class="hljs-keyword">if</span> <span class="hljs-property">@p</span>.turtles?
<span class="hljs-literal">null</span></pre></div></div>
</li>
<li id="section-25">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-25">¶</a>
</div>
<p>Factory: create num new turtles at this turtle’s location. The optional init
proc is called on the new turtle after inserting in its agentSet.</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-attribute">hatch</span>: <span class="hljs-function"><span class="hljs-params">(num = <span class="hljs-number">1</span>, breed = <span class="hljs-property">@breed</span>, 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 turtle</span>
a.setXY <span class="hljs-property">@x</span>, <span class="hljs-property">@y</span> <span class="hljs-comment"># for side effects like patches.turtlesHere</span>
a.color = <span class="hljs-property">@color</span>
a[k] = @[k] <span class="hljs-keyword">for</span> k <span class="hljs-keyword">in</span> breed.ownVariables <span class="hljs-keyword">when</span> a[k] <span class="hljs-keyword">is</span> <span class="hljs-literal">null</span> <span class="hljs-comment"># hatched turtle inherits parents' breed properties</span></pre></div></div>
</li>
<li id="section-26">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-26">¶</a>
</div>
<p>possible alternative: a[k] = @[k] for k, v in a when v is null</p>
</div>
<div class="content"><div class='highlight'><pre> init(a); a <span class="hljs-comment"># Important: init called after object inserted in agentset</span></pre></div></div>
</li>
<li id="section-27">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-27">¶</a>
</div>
<p>Return the members of the given agentset that are within radius distance
from me, using patch topology</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-attribute">inRadius</span>: <span class="hljs-function"><span class="hljs-params">(aset, radius)</span> -></span>
aset.inRadius(@, radius)</pre></div></div>
</li>
<li id="section-28">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-28">¶</a>
</div>
<p>Return the members of the given agentset that are within distance
from me, and within angle radians of my heading using patch topology</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-attribute">inCone</span>: <span class="hljs-function"><span class="hljs-params">(aset, distance, angle)</span> -></span>
aset.inCone @, distance, angle, <span class="hljs-property">@heading</span></pre></div></div>
</li>
<li id="section-29">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-29">¶</a>
</div>
<p>Return true if world coordinate falls on turtle sprite</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-attribute">hitTest</span>: <span class="hljs-function"><span class="hljs-params">(x, y)</span> -></span>
<span class="hljs-property">@distanceXY</span>(x, y) < <span class="hljs-property">@size</span></pre></div></div>
</li>
<li id="section-30">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-30">¶</a>
</div>
<p>Return other end of link from me</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-attribute">otherEnd</span>: <span class="hljs-function"><span class="hljs-params">(l)</span> -></span> <span class="hljs-keyword">if</span> l.end1 <span class="hljs-keyword">is</span> @ <span class="hljs-keyword">then</span> l.end2 <span class="hljs-keyword">else</span> l.end1</pre></div></div>
</li>
<li id="section-31">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-31">¶</a>
</div>
<p>Return all links linked to me</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-attribute">myLinks</span>: <span class="hljs-function">-></span>
<span class="hljs-property">@links</span> ? (l <span class="hljs-keyword">for</span> l <span class="hljs-keyword">in</span> <span class="hljs-property">@model</span>.links <span class="hljs-keyword">when</span> (l.end1 <span class="hljs-keyword">is</span> @) <span class="hljs-keyword">or</span> (l.end2 <span class="hljs-keyword">is</span> @))</pre></div></div>
</li>
<li id="section-32">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-32">¶</a>
</div>
<p>Return all agents linked to me</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-attribute">linkNeighbors</span>: <span class="hljs-function">-></span>
<span class="hljs-property">@otherEnd</span> l <span class="hljs-keyword">for</span> l <span class="hljs-keyword">in</span> <span class="hljs-property">@myLinks</span>()</pre></div></div>
</li>
<li id="section-33">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-33">¶</a>
</div>
<p>Return links where I am the “to” agent in links.create</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-attribute">myInLinks</span>: <span class="hljs-function">-></span>
l <span class="hljs-keyword">for</span> l <span class="hljs-keyword">in</span> <span class="hljs-property">@myLinks</span>() <span class="hljs-keyword">when</span> l.end2 <span class="hljs-keyword">is</span> @</pre></div></div>
</li>
<li id="section-34">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-34">¶</a>
</div>
<p>Return other end of myInLinks</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-attribute">inLinkNeighbors</span>: <span class="hljs-function">-></span>
l.end1 <span class="hljs-keyword">for</span> l <span class="hljs-keyword">in</span> <span class="hljs-property">@myLinks</span>() <span class="hljs-keyword">when</span> l.end2 <span class="hljs-keyword">is</span> @</pre></div></div>
</li>
<li id="section-35">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-35">¶</a>
</div>
<p>Return links where I am the “from” agent in links.create</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-attribute">myOutLinks</span>: <span class="hljs-function">-></span>
l <span class="hljs-keyword">for</span> l <span class="hljs-keyword">in</span> <span class="hljs-property">@myLinks</span>() <span class="hljs-keyword">when</span> l.end1 <span class="hljs-keyword">is</span> @</pre></div></div>
</li>
<li id="section-36">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-36">¶</a>
</div>
<p>Return other end of myOutinks</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-attribute">outLinkNeighbors</span>: <span class="hljs-function">-></span>
l.end2 <span class="hljs-keyword">for</span> l <span class="hljs-keyword">in</span> <span class="hljs-property">@myLinks</span>() <span class="hljs-keyword">when</span> l.end1 <span class="hljs-keyword">is</span> @</pre></div></div>
</li>
<li id="section-37">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-37">¶</a>
</div>
<p>use colorMixin to setup colors</p>
</div>
<div class="content"><div class='highlight'><pre>colorMixin(Turtle, <span class="hljs-string">"color"</span>, <span class="hljs-literal">null</span>)
colorMixin(Turtle, <span class="hljs-string">"strokeColor"</span>, <span class="hljs-literal">null</span>)
colorMixin(Turtle, <span class="hljs-string">"labelColor"</span>, <span class="hljs-string">"black"</span>)</pre></div></div>
</li>
</ul>
</div>
</body>
</html>