# Untitled

a guest
Nov 6th, 2017
121
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. /********************************************************
2. * See tutorial at:
3. * http://www.petercollingridge.appspot.com/3D-tutorial
4. *********************************************************/
5.
6. var backgroundColour = color(255, 255, 255);
7. var nodeColour = color(40, 168, 107);
8. var edgeColour = color(34, 68, 204);
9. var nodeSize = 8;
10.
11. var node0 = [-100, -100, -100];
12. var node1 = [-100, -100, 100];
13. var node2 = [-100, 100, -100];
14. var node3 = [-100, 100, 100];
15. var node4 = [ 100, -100, -100];
16. var node5 = [ 100, -100, 100];
17. var node6 = [ 100, 100, -100];
18. var node7 = [ 100, 100, 100];
19. var nodes = [node0, node1, node2, node3, node4, node5, node6, node7];
20.
21. var edge0 = [0, 1];
22. var edge1 = [1, 3];
23. var edge2 = [3, 2];
24. var edge3 = [2, 0];
25. var edge4 = [4, 5];
26. var edge5 = [5, 7];
27. var edge6 = [7, 6];
28. var edge7 = [6, 4];
29. var edge8 = [0, 4];
30. var edge9 = [1, 5];
31. var edge10 = [2, 6];
32. var edge11 = [3, 7];
33. var edges = [edge0, edge1, edge2, edge3, edge4, edge5, edge6, edge7, edge8, edge9, edge10, edge11];
34.
35. // Rotate shape around the z-axis
36. var rotateZ3D = function(theta) {
37. var sin_t = sin(theta);
38. var cos_t = cos(theta);
39.
40. for (var n=0; n<nodes.length; n++) {
41. var node = nodes[n];
42. var x = node[0];
43. var y = node[1];
44. node[0] = x * cos_t - y * sin_t;
45. node[1] = y * cos_t + x * sin_t;
46. }
47. };
48.
49. var rotateY3D = function(theta) {
50. var sin_t = sin(theta);
51. var cos_t = cos(theta);
52.
53. for (var n=0; n<nodes.length; n++) {
54. var node = nodes[n];
55. var x = node[0];
56. var z = node[2];
57. node[0] = x * cos_t - z * sin_t;
58. node[2] = z * cos_t + x * sin_t;
59. }
60. };
61.
62. var rotateX3D = function(theta) {
63. var sin_t = sin(theta);
64. var cos_t = cos(theta);
65.
66. for (var n=0; n<nodes.length; n++) {
67. var node = nodes[n];
68. var y = node[1];
69. var z = node[2];
70. node[1] = y * cos_t - z * sin_t;
71. node[2] = z * cos_t + y * sin_t;
72. }
73. };
74.
75. //rotateZ3D(30);
76. rotateY3D(45);
77. //rotateX3D(30);
78.
79. var draw= function() {
80. background(backgroundColour);
81.
82. // Draw edges
83. stroke(edgeColour);
84. for (var e=0; e<edges.length; e++) {
85. var n0 = edges[e][0];
86. var n1 = edges[e][1];
87. var node0 = nodes[n0];
88. var node1 = nodes[n1];
89. var x1 = -node0[0] - 0.5*node0[2] * sqrt(2)/2;
90. var y1 = node0[1] - 0.5*node0[2] * sqrt(2)/2;
91. var x2 = -node1[0] - 0.5*node1[2] * sqrt(2)/2;
92. var y2 = node1[1] - 0.5*node1[2] * sqrt(2)/2;
93. line(x1, y1, x2, y2);
94. }
95.
96. // Draw nodes
97. fill(nodeColour);
98. noStroke();
99. for (var n=0; n<nodes.length; n++) {
100. var node = nodes[n];
101. var x1 = -node[0] - 0.5*node[2] * sqrt(2)/2;
102. var y1 = node[1] - 0.5*node[2] * sqrt(2)/2;
103. ellipse(x1, y1, nodeSize, nodeSize);
104. }
105.
106. };
107.
108. var mouseDragged = function() {
109. rotateY3D(-mouseX + pmouseX);
110. rotateX3D(mouseY - pmouseY);
111. };
112.
113. translate(200, 200);