-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvisualizer_class.pde
120 lines (111 loc) · 2.91 KB
/
visualizer_class.pde
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
import java.util.Map;
class Visualizer {
int C = 130000;
int J = 1;
int L = 50;
int M = 10;
int f_limit = 10;
int min_dist = 30;
int size_limit = 1500;
ArrayList<Node> nodes;
String method = "FORCE_DIRECTED";
color c = color(0);
Visualizer(ArrayList<Node> in_nodes) {
nodes = in_nodes;
}
void visualize() {
if (method == "FORCE_DIRECTED") {
force_directed();
}else if (method == "CIRCLE") {
circle();
}else if (method == "LINEUP") {
line_up();
}
fill(c);
stroke(c);
show();
fill(0);
stroke(0);
}
void show() {
for (Node node : nodes) {
for (Link link : node.links) {
line(node.p.x, node.p.y, link.to_node.p.x, link.to_node.p.y);
strokeWeight(2);
line(node.p.x, node.p.y, node.p.x + (link.to_node.p.x - node.p.x) / 4, node.p.y + (link.to_node.p.y - node.p.y) / 4);
strokeWeight(1);
}
}
for (Node node : nodes) {
textSize(20);
ellipse(node.p.x, node.p.y, 10, 10);
text(node.name, node.p.x, node.p.y - 5);
}
}
void line_up() {
int interval = min_dist * 3;
int div = (int)sqrt(nodes.size());
for(int i=0; i<nodes.size(); i++){
Node node = nodes.get(i);
node.p.x = interval * (i % div);
node.p.y = interval * (int)(i / div);
}
}
void circle() {
int cnt = 0;
for (Node node : nodes) {
cnt++;
}
float circle_rad = min_dist / (2 * sin(PI / cnt));
float interval = 2 * PI / cnt;
PVector center = new PVector(width/2, height/2);
cnt = 0;
for (Node node : nodes) {
cnt++;
node.p.x = center.x + circle_rad * cos(interval * cnt);
node.p.y = center.y + circle_rad * sin(interval * cnt);
}
}
void force_directed() {
for (Node node1 : nodes) {
PVector f = new PVector(0, 0);
for (Node node2 : nodes) {
if ( node1 == node2 ) continue;
f.add(force(node1, node2));
}
node1.v.x += f.x/M;
node1.v.y += f.y/M;
node1.v.x *= 0.9;
node1.v.y *= 0.9;
node1.p.x += node1.v.x;
node1.p.y += node1.v.y;
}
}
PVector force(Node n1, Node n2) {
PVector f = new PVector();
float F = C / pow(PVector.dist(n1.p, n2.p), 2);
F = F > f_limit ? f_limit : F;
f.x -= F * (n2.p.x - n1.p.x) / PVector.dist(n1.p, n2.p);
f.y -= F * (n2.p.y - n1.p.y) / PVector.dist(n1.p, n2.p);
if (is_connected(n1, n2)) {
F = J * ( PVector.dist(n1.p, n2.p) - L);
F = F > f_limit ? f_limit : F;
f.x += F * (n2.p.x - n1.p.x) / PVector.dist(n1.p, n2.p);
f.y += F * (n2.p.y - n1.p.y) / PVector.dist(n1.p, n2.p);
}
return f;
}
boolean is_connected(Node node1, Node node2) {
for (Link link : node1.links) {
if (link.to_node == node2) {
return true;
}
}
for (Link link : node2.links) {
if (link.to_node == node1) {
return true;
}
}
return false;
}
}