-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathlayout.nls
55 lines (49 loc) · 1.73 KB
/
layout.nls
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
;; Different people are displayed in 5 different colors depending on health
;; green is a survivor of the infection
;; red is an infected person
;; white is neither infected nor cured
to assign-color ;; turtle procedure
ifelse cured?
[ set color green ]
[ ifelse infected?
[set color red ]
[set color white]]
end
;;;;;;;;;;;;;;
;;; Layout ;;;
;;;;;;;;;;;;;;
;; resize-nodes, change back and forth from size based on degree to a size of 1
to resize-nodes
ifelse all? turtles [size <= 1]
[
;; a node is a circle with diameter determined by
;; the SIZE variable; using SQRT makes the circle's
;; area proportional to its degree
ask turtles [ set size sqrt count link-neighbors ]
]
[ask turtles [ set size 1 ]]
end
to layout
;; the number 3 here is arbitrary; more repetitions slows down the
;; model, but too few gives poor layouts
repeat 3 [
;; the more turtles we have to fit into the same amount of space,
;; the smaller the inputs to layout-spring we'll need to use
let factor sqrt count turtles
;; numbers here are arbitrarily chosen for pleasing appearance
layout-spring turtles links (1 / factor) (7 / factor) (1 / factor)
display ;; for smooth animation
]
;; don't bump the edges of the world
let x-offset max [xcor] of turtles + min [xcor] of turtles
let y-offset max [ycor] of turtles + min [ycor] of turtles
;; big jumps look funny, so only adjust a little each time
set x-offset limit-magnitude x-offset 0.1
set y-offset limit-magnitude y-offset 0.1
ask turtles [ setxy (xcor - x-offset / 2) (ycor - y-offset / 2) ]
end
to-report limit-magnitude [number limit]
if number > limit [ report limit ]
if number < (- limit) [ report (- limit) ]
report number
end