Repository navigation
Expand file tree
/
Copy pathlinks.html
More file actions
182 lines (127 loc) · 7.98 KB
/
Copy pathlinks.html
File metadata and controls
182 lines (127 loc) · 7.98 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
<!DOCTYPE html>
<html>
<head>
<title>links.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>links.coffee</h1>
</div>
</li>
<li id="section-1">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-1">¶</a>
</div>
<h3 id="links">Links</h3>
</div>
</li>
<li id="section-2">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-2">¶</a>
</div>
<p>Class Links is a subclass of AgentSet which stores instances of Link
or subclasses of Link</p>
</div>
<div class="content"><div class='highlight'><pre><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Links</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">AgentSet</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: super creates the empty AgentSet instance and installs
the agentClass (breed) variable shared by all the Links in this set.</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></pre></div></div>
</li>
<li id="section-4">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-4">¶</a>
</div>
<p>Factory: Add 1 or more links from the from agent to the to agent(s) which
can be a single agent or an array of agents. The optional init
proc is called on the new link after inserting in the agentSet.</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-attribute">create</span>: <span class="hljs-function"><span class="hljs-params">(from, to, init = ->)</span> -></span> <span class="hljs-comment"># returns array of new links too</span>
to = [to] <span class="hljs-keyword">unless</span> to.length?
(<span class="hljs-function"><span class="hljs-params">(o)</span> -></span> init(o); o) <span class="hljs-property">@add</span> <span class="hljs-keyword">new</span> <span class="hljs-property">@agentClass</span> from, a <span class="hljs-keyword">for</span> a <span class="hljs-keyword">in</span> to <span class="hljs-comment"># too tricky?</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 all links from set via link.die()
Note call in reverse order to optimize list restructuring.</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-attribute">clear</span>: <span class="hljs-function">-></span> <span class="hljs-property">@last</span>().die() <span class="hljs-keyword">while</span> <span class="hljs-property">@any</span>(); <span class="hljs-literal">null</span> <span class="hljs-comment"># tricky, each die modifies list</span></pre></div></div>
</li>
<li id="section-6">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-6">¶</a>
</div>
<p>Return all the nodes in this agentset, with duplicates
included. If 4 links have the same endpoint, it will
appear 4 times.</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-attribute">allEnds</span>: <span class="hljs-function">-></span> <span class="hljs-comment"># all link ends, w/ dups</span>
n = <span class="hljs-property">@asSet</span> []
n.push l.end1, l.end2 <span class="hljs-keyword">for</span> l <span class="hljs-keyword">in</span> @
n</pre></div></div>
</li>
<li id="section-7">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-7">¶</a>
</div>
<p>Returns all the nodes in this agentset sorted by ID and with
duplicates removed.</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-attribute">nodes</span>: <span class="hljs-function">-></span> <span class="hljs-comment"># allEnds without dups</span>
<span class="hljs-property">@allEnds</span>().sortById().uniq()</pre></div></div>
</li>
<li id="section-8">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-8">¶</a>
</div>
<p>Circle Layout: position the turtles in the list in an equally
spaced circle of the given radius, with the initial turtle
at the given start angle (default to pi/2 or “up”) and in the
+1 or -1 direction (counter clockwise or clockwise)
defaulting to -1 (clockwise).</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-attribute">layoutCircle</span>: <span class="hljs-function"><span class="hljs-params">(list, radius, startAngle = Math.PI/<span class="hljs-number">2</span>, direction = -<span class="hljs-number">1</span>)</span> -></span>
dTheta = <span class="hljs-number">2</span>*Math.PI/list.length
<span class="hljs-keyword">for</span> a, i <span class="hljs-keyword">in</span> list
a.setXY <span class="hljs-number">0</span>, <span class="hljs-number">0</span>
a.heading = startAngle + direction*dTheta*i
a.forward radius
<span class="hljs-literal">null</span>
<span class="hljs-attribute">setDraggable</span>: <span class="hljs-function"><span class="hljs-params">()</span> -></span>
<span class="hljs-property">@on</span> <span class="hljs-string">'dragstart'</span>, <span class="hljs-function"><span class="hljs-params">(mouseEvent)</span> =></span>
mouseEvent.target.dragging = <span class="hljs-literal">true</span>
<span class="hljs-property">@on</span> <span class="hljs-string">'dragend'</span>, <span class="hljs-function"><span class="hljs-params">(mouseEvent)</span> =></span>
mouseEvent.target.dragging = <span class="hljs-literal">false</span>
<span class="hljs-property">@on</span> <span class="hljs-string">'drag'</span>, <span class="hljs-function"><span class="hljs-params">(mouseEvent)</span> =></span>
end1 = mouseEvent.target.end1
end2 = mouseEvent.target.end2
end1.setXY(end1.x - mouseEvent.dx, end1.y - mouseEvent.dy)
end2.setXY(end2.x - mouseEvent.dx, end2.y - mouseEvent.dy)</pre></div></div>
</li>
</ul>
</div>
</body>
</html>