/
Toroid.pde
182 lines (169 loc) · 3.65 KB
/
Toroid.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
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
172
173
174
175
176
177
178
179
180
181
/**
* Interactive Toroid
* by Ira Greenberg.
*
* Illustrates the geometric relationship between Toroid, Sphere, and Helix
* 3D primitives, as well as lathing principal.
*
* Instructions: <br />
* UP arrow key pts++ <br />
* DOWN arrow key pts-- <br />
* LEFT arrow key segments-- <br />
* RIGHT arrow key segments++ <br />
* 'a' key toroid radius-- <br />
* 's' key toroid radius++ <br />
* 'z' key initial polygon radius-- <br />
* 'x' key initial polygon radius++ <br />
* 'w' key toggle wireframe/solid shading <br />
* 'h' key toggle sphere/helix <br />
*/
int pts = 40;
float angle = 0;
float radius = 60.0;
// lathe segments
int segments = 60;
float latheAngle = 0;
float latheRadius = 100.0;
//vertices
PVector vertices[], vertices2[];
// for shaded or wireframe rendering
boolean isWireFrame = false;
// for optional helix
boolean isHelix = false;
float helixOffset = 5.0;
void setup(){
size(640, 360, P3D);
}
void draw(){
background(50, 64, 42);
// basic lighting setup
lights();
// 2 rendering styles
// wireframe or solid
if (isWireFrame){
stroke(255, 255, 150);
noFill();
}
else {
noStroke();
fill(150, 195, 125);
}
//center and spin toroid
translate(width/2, height/2, -100);
rotateX(frameCount*PI/150);
rotateY(frameCount*PI/170);
rotateZ(frameCount*PI/90);
// initialize point arrays
vertices = new PVector[pts+1];
vertices2 = new PVector[pts+1];
// fill arrays
for(int i=0; i<=pts; i++){
vertices[i] = new PVector();
vertices2[i] = new PVector();
vertices[i].x = latheRadius + sin(radians(angle))*radius;
if (isHelix){
vertices[i].z = cos(radians(angle))*radius-(helixOffset*
segments)/2;
}
else{
vertices[i].z = cos(radians(angle))*radius;
}
angle+=360.0/pts;
}
// draw toroid
latheAngle = 0;
for(int i=0; i<=segments; i++){
beginShape(QUAD_STRIP);
for(int j=0; j<=pts; j++){
if (i>0){
vertex(vertices2[j].x, vertices2[j].y, vertices2[j].z);
}
vertices2[j].x = cos(radians(latheAngle))*vertices[j].x;
vertices2[j].y = sin(radians(latheAngle))*vertices[j].x;
vertices2[j].z = vertices[j].z;
// optional helix offset
if (isHelix){
vertices[j].z+=helixOffset;
}
vertex(vertices2[j].x, vertices2[j].y, vertices2[j].z);
}
// create extra rotation for helix
if (isHelix){
latheAngle+=720.0/segments;
}
else {
latheAngle+=360.0/segments;
}
endShape();
}
}
/*
left/right arrow keys control ellipse detail
up/down arrow keys control segment detail.
'a','s' keys control lathe radius
'z','x' keys control ellipse radius
'w' key toggles between wireframe and solid
'h' key toggles between toroid and helix
*/
void keyPressed(){
if(key == CODED) {
// pts
if (keyCode == UP) {
if (pts<40){
pts++;
}
}
else if (keyCode == DOWN) {
if (pts>3){
pts--;
}
}
// extrusion length
if (keyCode == LEFT) {
if (segments>3){
segments--;
}
}
else if (keyCode == RIGHT) {
if (segments<80){
segments++;
}
}
}
// lathe radius
if (key =='a'){
if (latheRadius>0){
latheRadius--;
}
}
else if (key == 's'){
latheRadius++;
}
// ellipse radius
if (key =='z'){
if (radius>10){
radius--;
}
}
else if (key == 'x'){
radius++;
}
// wireframe
if (key =='w'){
if (isWireFrame){
isWireFrame=false;
}
else {
isWireFrame=true;
}
}
// helix
if (key =='h'){
if (isHelix){
isHelix=false;
}
else {
isHelix=true;
}
}
}