-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathclass.testing.coffee
211 lines (153 loc) · 4.73 KB
/
class.testing.coffee
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
class Testing
@detect: false
constructor: () ->
@graphs = []
@graph = new Graph()
update_graphs: (i) ->
if typeof(this.graphs) == "undefined"
return
g[0].apply(g[1], [i]) for g in this.graphs
algorithm: (@algo) ->
@graph.layout.select(@algo)
renderer: (@rendermode) ->
if @graph.render.projector?
$(@graph.render.projector.canvas_dom).empty()
@graph.render.select(@rendermode)
@toggle_slow(false)
@toggle_pause(false)
detect_resize: ->
@detect = true
document.body.onresize = () => @graph.trigger("resize")
toggle_slow: (swap = true) ->
if not @graph.slowed?
@graph.slowed = false
if not swap
@graph.slowed = not @graph.slowed
if @graph.slowed
@graph.slowed = false
$(".toggle_slow").removeClass("active")
else
@graph.slowed = true
$(".toggle_slow").addClass("active")
toggle_pause: (swap = true) ->
@graph.forced_pause ?= false
if not swap
@graph.forced_pause = not @graph.forced_pause
if @graph.forced_pause
@graph.forced_pause = false
$(".toggle_pause").removeClass("active")
else
@graph.forced_pause = true
$(".toggle_pause").addClass("active")
@graph.paused = @graph.forced_pause
toggle_2d: ->
$(".toggle_2d").addClass("active")
$(".toggle_3d").removeClass("active")
@renderer('2d')
toggle_3d: ->
$(".toggle_3d").addClass("active")
$(".toggle_2d").removeClass("active")
@renderer('3d')
toggle_spring: ->
$(".toggle_spring").addClass("active")
$(".toggle_kamada").removeClass("active")
@algorithm('spring')
toggle_kamada: ->
$(".toggle_spring").removeClass("active")
$(".toggle_kamada").addClass("active")
@algorithm('kamada')
toggle_mixed: ->
@algorithm('mixed')
clear_graph: ->
@graph.clear()
cliqueify: ->
for i, n1 of @graph.nodes
do(n1) ->
@graph.connect(n1,n2) for n2 in @graph.nodes[i..]
# adds a random edge, uses the Fisher-Yates algo
add_random_edge: (r1) ->
ls = @graph.nodes
i = @graph.nodes.length
if @graph.edges.length >= (i * (i-1) * 0.5)
return;
while i-- > 0
do() =>
j = Math.floor( Math.random() * (i+1) )
ti = ls[i]
ls[i] = ls[j]
ls[j] = ti
r1 = ls[0]
j = 1
j++ while (typeof(ls[j]) != undefined) and @graph.connected( r1, ls[j] )
if ls[j]? and typeof(ls[j]) != "undefined"
@graph.connect( r1, ls[j] )
delete_random_edge: ->
if @graph.edges.length == 0
return true
@graph.disconnect_edge @random_of( @graph.edges )
add_random_node: (e)->
n = new Node("rn" + Math.random())
@add_random_edge @graph.add( n )
# @graph.trigger('add_node', n ) # why is this being trigerred elsewhere? from where?
delete_random_node: ->
@graph.remove_node @random_of(@graph.nodes)
data_reset: ->
x = () -> Math.random() * 5
[node.x,node.y,node.z] = [x(),x(),x()] for node in @graph.nodes
data_loop: (num = null, offset = 0) ->
num ?= parseInt(prompt("Number of nodes in loop [3-inf]", 10))
num = num + offset - 2
@graph.connect "a"+i, "a"+(i+1) for i in [offset..num]
@graph.connect "a"+i, "a"+offset
data_grid: (w = null, h = null) ->
w ?= parseInt prompt "Grid width", 10
h ?= parseInt prompt "Grid height", w
@graph.add "x1y1"
for x in [1..w]
do(x) ->
for y in [1..h]
do(y) ->
if x < w then @graph.connect( "x"+x+"y"+y, "x"+(x+1)+"y"+y )
if y < h then @graph.connect( "x"+x+"y"+y, "x"+x+"y"+(y+1) )
data_grid_3d: (w = null, h = null, d = null) ->
w ?= parseInt prompt "Grid width", 5
h ?= parseInt prompt "Grid height", w
d ?= parseInt prompt "Grid depth", h
#@graph.add "p111"
for x in [1..w]
do(x) ->
for y in [1..h]
do(y) ->
for z in [1..d]
do(z) ->
if x < w then @graph.connect( "p"+x+y+z, "p"+(x+1)+y+z )
if y < h then @graph.connect( "p"+x+y+z, "p"+x+(y+1)+z )
if z < d then @graph.connect( "p"+x+y+z, "p"+x+y+(z+1) )
data_cube: ->
c = { "a": ["b","d","e"], "b": ["c","f"], "c": ["d","g"], "h": ["d","g"], "e": ["f","h"], "f": ["g"] }
for a, v of c
do(a) =>
@graph.connect a, b for b in v
data_clique: (size = null, offset = 0 ) ->
size ?= parseInt prompt "Clique size", 6
if size == 1
return true
@graph.connect( "n"+(size+offset), "n"+(i+offset) ) for i in [1..size]
@data_clique size - 1, offset
data_kneser: (n = 5, k = 2) ->
n ?= parseInt prompt "Set size", 5
k ?= parseInt prompt "Subset size", 2
@graph.subsets = new Subsets();
s = @graph.subsets.list(n, k)
for i in [0...s.length]
for j in [(i+1)...s.length]
if @graph.subsets.disjoint( s[i], s[j] )
@graph.connect( s[i].join(), s[j].join() )
data_random: (n=10, p=0.1) ->
for i in [0...n]
for j in [(i+1)...n]
if Math.random() < p
@graph.connect("n"+i, "n"+j)
random_of: (ls) ->
return ls[ Math.floor( Math.random() * ls.length ) ]
window.Testing = Testing