Repository navigation
Expand file tree
/
Copy pathlink.html
More file actions
217 lines (158 loc) · 11.2 KB
/
Copy pathlink.html
File metadata and controls
217 lines (158 loc) · 11.2 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
<!DOCTYPE html>
<html>
<head>
<title>link.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>link.coffee</h1>
</div>
</li>
<li id="section-1">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-1">¶</a>
</div>
<h3 id="link">Link</h3>
</div>
</li>
<li id="section-2">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-2">¶</a>
</div>
<p>Class Link connects two agent endpoints for graph modeling.</p>
</div>
<div class="content"><div class='highlight'><pre><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Link</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 initializes instance variables:</p>
<ul>
<li>id: unique identifier, promoted by agentset create() factory method</li>
<li>breed: the agentset this agent belongs to</li>
<li>end1, end2: two agents being connected</li>
<li>color: defaults to light gray</li>
<li>thickness: thickness in pixels of the link, default 2</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>hidden: whether or not to draw this link</li>
</ul>
</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">end1</span>:<span class="hljs-literal">null</span>; <span class="hljs-attribute">end2</span>:<span class="hljs-literal">null</span><span class="hljs-comment"># My two endpoints, using agents. Promoted by ctor</span>
<span class="hljs-attribute">color</span>: [<span class="hljs-number">130</span>,<span class="hljs-number">130</span>,<span class="hljs-number">130</span>]<span class="hljs-comment"># my color</span>
<span class="hljs-attribute">thickness</span>: <span class="hljs-number">2</span> <span class="hljs-comment"># my thickness in pixels, default to 2</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 midpoint</span>
<span class="hljs-attribute">constructor</span>: <span class="hljs-function"><span class="hljs-params">(<span class="hljs-property">@end1</span>, <span class="hljs-property">@end2</span>)</span> -></span>
<span class="hljs-keyword">if</span> <span class="hljs-property">@end1</span>.links?
<span class="hljs-property">@end1</span>.links.push @
<span class="hljs-property">@end2</span>.links.push @</pre></div></div>
</li>
<li id="section-4">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-4">¶</a>
</div>
<p>Draw a line between the two endpoints. Draws “around” the
torus if appropriate using two lines. As with Turtle.draw,
is called with patch coordinate transform installed.</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>
ctx.save()
ctx.strokeStyle = <span class="hljs-property">@color</span>.css <span class="hljs-comment"># u.colorStr @color</span>
ctx.lineWidth = <span class="hljs-property">@model</span>.patches.fromBits <span class="hljs-property">@thickness</span>
ctx.beginPath()
<span class="hljs-keyword">if</span> !<span class="hljs-property">@model</span>.patches.isTorus
ctx.moveTo <span class="hljs-property">@end1</span>.x, <span class="hljs-property">@end1</span>.y
ctx.lineTo <span class="hljs-property">@end2</span>.x, <span class="hljs-property">@end2</span>.y
<span class="hljs-keyword">else</span>
pt = <span class="hljs-property">@end1</span>.torusPt <span class="hljs-property">@end2</span>
ctx.moveTo <span class="hljs-property">@end1</span>.x, <span class="hljs-property">@end1</span>.y
ctx.lineTo pt...
<span class="hljs-keyword">if</span> pt[<span class="hljs-number">0</span>] <span class="hljs-keyword">isnt</span> <span class="hljs-property">@end2</span>.x <span class="hljs-keyword">or</span> pt[<span class="hljs-number">1</span>] <span class="hljs-keyword">isnt</span> <span class="hljs-property">@end2</span>.y
pt = <span class="hljs-property">@end2</span>.torusPt <span class="hljs-property">@end1</span>
ctx.moveTo <span class="hljs-property">@end2</span>.x, <span class="hljs-property">@end2</span>.y
ctx.lineTo pt...
ctx.closePath()
ctx.stroke()
ctx.restore()
<span class="hljs-keyword">if</span> <span class="hljs-property">@label</span>?
[x0, y0] = u.lerp2 <span class="hljs-property">@end1</span>.x, <span class="hljs-property">@end1</span>.y, <span class="hljs-property">@end2</span>.x, <span class="hljs-property">@end2</span>.y, <span class="hljs-number">.5</span>
[x,y] = <span class="hljs-property">@model</span>.patches.patchXYtoPixelXY x0, y0
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-5">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-5">¶</a>
</div>
<p>Remove this link from its agentset</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 @
u.removeItem <span class="hljs-property">@end1</span>.links, @ <span class="hljs-keyword">if</span> <span class="hljs-property">@end1</span>.links?
u.removeItem <span class="hljs-property">@end2</span>.links, @ <span class="hljs-keyword">if</span> <span class="hljs-property">@end2</span>.links?
<span class="hljs-literal">null</span>
<span class="hljs-attribute">hitTest</span>: <span class="hljs-function"><span class="hljs-params">(x, y)</span> -></span>
distance = u.aSum (a.distanceXY x, y <span class="hljs-keyword">for</span> a <span class="hljs-keyword">in</span> <span class="hljs-property">@bothEnds</span>())
distance - <span class="hljs-property">@length</span>() < <span class="hljs-number">0.05</span> / <span class="hljs-property">@model</span>.patches.size</pre></div></div>
</li>
<li id="section-6">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-6">¶</a>
</div>
<p>Return the two endpoints of this link</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-attribute">bothEnds</span>: <span class="hljs-function">-></span> [<span class="hljs-property">@end1</span>, <span class="hljs-property">@end2</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 the distance between the endpoints with the current topology.</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-attribute">length</span>: <span class="hljs-function">-></span> <span class="hljs-property">@end1</span>.distance <span class="hljs-property">@end2</span></pre></div></div>
</li>
<li id="section-8">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-8">¶</a>
</div>
<p>Return the other end of the link, given an endpoint agent.
Assumes the given input <em>is</em> one of the link endpoint pairs!</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-attribute">otherEnd</span>: <span class="hljs-function"><span class="hljs-params">(a)</span> -></span> <span class="hljs-keyword">if</span> <span class="hljs-property">@end1</span> <span class="hljs-keyword">is</span> a <span class="hljs-keyword">then</span> <span class="hljs-property">@end2</span> <span class="hljs-keyword">else</span> <span class="hljs-property">@end1</span></pre></div></div>
</li>
<li id="section-9">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-9">¶</a>
</div>
<p>use colorMixin to setup colors</p>
</div>
<div class="content"><div class='highlight'><pre>colorMixin(Link, <span class="hljs-string">"color"</span>, [<span class="hljs-number">130</span>,<span class="hljs-number">130</span>,<span class="hljs-number">130</span>])
colorMixin(Link, <span class="hljs-string">"labelColor"</span>, <span class="hljs-string">"black"</span>)</pre></div></div>
</li>
</ul>
</div>
</body>
</html>