1
+ /*
2
+ P3D_toroid
3
+ Minimal version of Ira Greenbergs Toroid example
4
+ for demonstrating P3D points and transformations can be used.
5
+ - mouseX » adjust pts
6
+ - mouseY » adjust segments
7
+ - keyboard » look below for details
8
+ https://processing.org/examples/toroid.html
9
+ cc teddavis.org 2017
10
+ */
11
+
12
+ // import and create instance of XYscope
13
+ import xyscope.* ;
14
+ XYscope xy;
15
+
16
+ // minim is required to generate audio
17
+ import ddf.minim.* ;
18
+
19
+
20
+ int pts = 40 ;
21
+ float angle = 0 ;
22
+ float radius = 100.0 ;
23
+
24
+ // lathe segments
25
+ int segments = 20 ;
26
+ float latheAngle = 0 ;
27
+ float latheRadius = 100.0 ;
28
+
29
+ // vertices
30
+ PVector vertices[], vertices2[];
31
+
32
+ // for optional helix
33
+ boolean isHelix = false ;
34
+ float helixOffset = 5.0 ;
35
+
36
+ void setup () {
37
+ size (512 , 512 , P3D );
38
+
39
+ // initialize XYscope with default sound out
40
+ xy = new XYscope (this );
41
+ }
42
+
43
+ void draw () {
44
+ background (0 );
45
+
46
+ // clear waves like refreshing background
47
+ xy. clearWaves();
48
+
49
+ // change shape with mouse
50
+ pts = floor (map (mouseX , 0 , width , 1 , 50 ));
51
+ segments = floor (map (mouseY , 0 , height , 1 , 50 ));
52
+
53
+ // center and spin toroid
54
+ pushMatrix ();
55
+
56
+ translate (width / 2 , height / 2 , - 100 );
57
+
58
+ rotateX (frameCount * PI / 150 );
59
+ rotateY (frameCount * PI / 170 );
60
+ rotateZ (frameCount * PI / 90 );
61
+
62
+ // initialize point arrays
63
+ vertices = new PVector [pts+ 1 ];
64
+ vertices2 = new PVector [pts+ 1 ];
65
+
66
+ // fill arrays
67
+ for (int i= 0 ; i<= pts; i++ ) {
68
+ vertices[i] = new PVector ();
69
+ vertices2[i] = new PVector ();
70
+ vertices[i]. x = latheRadius + sin (radians (angle))* radius;
71
+ if (isHelix) {
72
+ vertices[i]. z = cos (radians (angle))* radius- (helixOffset*
73
+ segments)/ 2 ;
74
+ } else {
75
+ vertices[i]. z = cos (radians (angle))* radius;
76
+ }
77
+ angle+= 360.0 / pts;
78
+ }
79
+
80
+ // draw toroid
81
+ latheAngle = 0 ;
82
+ for (int i= 0 ; i<= segments; i++ ) {
83
+ xy. beginShape();
84
+
85
+ for (int j= 0 ; j<= pts; j++ ) {
86
+ if (i> 0 ) {
87
+ xy. vertex(vertices2[j]. x, vertices2[j]. y, vertices2[j]. z);
88
+ }
89
+ vertices2[j]. x = cos (radians (latheAngle))* vertices[j]. x;
90
+ vertices2[j]. y = sin (radians (latheAngle))* vertices[j]. x;
91
+ vertices2[j]. z = vertices[j]. z;
92
+ // optional helix offset
93
+ if (isHelix) {
94
+ vertices[j]. z+= helixOffset;
95
+ }
96
+ xy. vertex(vertices2[j]. x, vertices2[j]. y, vertices2[j]. z);
97
+ }
98
+ // create extra rotation for helix
99
+ if (isHelix) {
100
+ latheAngle+= 720.0 / segments;
101
+ } else {
102
+ latheAngle+= 360.0 / segments;
103
+ }
104
+ xy. endShape();
105
+ }
106
+ popMatrix ();
107
+
108
+ // build audio from shapes
109
+ xy. buildWaves();
110
+
111
+ // draw all analytics
112
+ xy. drawAll();
113
+ }
114
+
115
+ /*
116
+ left/right arrow keys control ellipse detail
117
+ up/down arrow keys control segment detail.
118
+ 'a','s' keys control lathe radius
119
+ 'z','x' keys control ellipse radius
120
+ 'w' key toggles between wireframe and solid
121
+ 'h' key toggles between toroid and helix
122
+ */
123
+ void keyPressed () {
124
+ if (key == CODED ) {
125
+ // pts
126
+ if (keyCode == UP ) {
127
+ if (pts< 40 ) {
128
+ pts++ ;
129
+ }
130
+ } else if (keyCode == DOWN ) {
131
+ if (pts> 3 ) {
132
+ pts-- ;
133
+ }
134
+ }
135
+ // extrusion length
136
+ if (keyCode == LEFT ) {
137
+ if (segments> 3 ) {
138
+ segments-- ;
139
+ }
140
+ } else if (keyCode == RIGHT ) {
141
+ if (segments< 80 ) {
142
+ segments++ ;
143
+ }
144
+ }
145
+ }
146
+ // lathe radius
147
+ if (key == ' a' ) {
148
+ if (latheRadius> 0 ) {
149
+ latheRadius-- ;
150
+ }
151
+ } else if (key == ' s' ) {
152
+ latheRadius++ ;
153
+ }
154
+ // ellipse radius
155
+ if (key == ' z' ) {
156
+ if (radius> 10 ) {
157
+ radius-- ;
158
+ }
159
+ } else if (key == ' x' ) {
160
+ radius++ ;
161
+ }
162
+ // helix
163
+ if (key == ' h' ) {
164
+ if (isHelix) {
165
+ isHelix= false ;
166
+ } else {
167
+ isHelix= true ;
168
+ }
169
+ }
170
+ }
0 commit comments