-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathGraphAPI.java
More file actions
171 lines (151 loc) · 3.57 KB
/
GraphAPI.java
File metadata and controls
171 lines (151 loc) · 3.57 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
import nub.core.Node;
import nub.primitives.Matrix;
import nub.processing.Scene;
import processing.core.PApplet;
import processing.core.PShape;
import processing.event.MouseEvent;
/**
* Thorough graph api test based upon the following graph:
* 1: red; 2: green; 3: blue; 4: yellow; 5: magenta; detached1: cyan; detached2:grey
* World
* ^
* |\
* 1 eye
* ^
* |\
* 2 3
* |
* 4
* |
* 5
*/
public class GraphAPI extends PApplet {
Scene scene;
Node n1, n2, n3, n4, n5, detached1, detached2, clone;
//Choose FX2D, JAVA2D, P2D or P3D
String renderer = P3D;
public void settings() {
size(900, 900, renderer);
}
public void setup() {
scene = new Scene(this);
// red
n1 = new Node(shape(color(255, 0, 0, 125)));
// green
n2 = new Node(n1, shape(color(0, 255, 0, 125)));
scene.randomize(n2);
n2.scale(0.5f);
// blue
n3 = new Node(n1, shape(color(0, 0, 255, 125)));
scene.randomize(n3);
// yellow
n4 = new Node(n2, shape(color(255, 255, 0, 125)));
scene.randomize(n4);
// magenta
n5 = new Node(n4, shape(color(255, 0, 255, 125)));
scene.randomize(n5);
// cyan
//detached1 = new Node();
//Graph.detach(detached1);
// same as two prev lines
detached1 = new Node(false);
detached1.set(n3);
detached1.setShape(shape(color(0, 255, 255, 125)));
// grey
detached2 = new Node(false);
detached2.setShape(shape(color(125, 125)));
float dx = -10;
float dy = 15;
float dz = 5;
Matrix t = new Matrix(1, 0, 0, 0,
0, 1, 0, 0,
0, 0, 1, 0,
dx, dy, dz, 0);
float b = QUARTER_PI;
Matrix r = new Matrix(cos(b), sin(b), 0, 0,
-sin(b), cos(b), 0, 0,
0, 0, 1, 0,
0, 0, 0, 1);
println(t.toString());
println(r.toString());
println(Matrix.multiply(t, r).toString());
t.rotateZ(b);
println(t.toString());
}
PShape shape(int c) {
PShape pShape = createShape(BOX, 30);
pShape.setFill(c);
return pShape;
}
public void draw() {
background(125);
scene.render();
scene.drawAxes();
}
public void keyPressed() {
if (key == 'c') {
clone = n5.copy();
clone.resetHint();
clone.enableHint(Node.TORUS);
n5.resetHint();
n5.enableHint(Node.AXES);
}
if (key == '1') {
n4.setReference(n3);
}
if (key == '2') {
detached1.setReference(n2);
}
if (key == '3') {
n2.setReference(detached1);
}
if (key == '4') {
detached2.setReference(detached1);
}
if (key == '5') {
if (detached1 != null)
detached1.attach();
}
if (key == '6') {
if (detached2 != null)
detached2.attach();
}
if (key == '7') {
n2.copy();
}
if (key == '8') {
n2.detach();
}
if (key == '9') {
detached1.detach();
}
}
@Override
public void mouseMoved() {
scene.tag();
}
@Override
public void mouseDragged() {
if (mouseButton == LEFT)
scene.spin();
else if (mouseButton == RIGHT)
scene.shift();
else
scene.moveForward(mouseX - pmouseX);
}
@Override
public void mouseWheel(MouseEvent event) {
scene.zoom(event.getCount() * 20);
}
@Override
public void mouseClicked(MouseEvent event) {
if (event.getCount() == 2)
if (event.getButton() == LEFT)
scene.focus();
else
scene.align();
}
public static void main(String[] args) {
PApplet.main(new String[]{"GraphAPI"});
}
}