-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathTrinket.pde
274 lines (232 loc) · 9.13 KB
/
Trinket.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
class Trinket {
PVector start_pos; // start position X,Y
PVector[] contour; // vector list holding contour
PVector[] param; // input parameter from pcm data
int segm, sides; // segments and sides of sphere like form
// segments must be 18,36
int sizeZ; // total height
Trinket(PVector pos, PVector[] parameter, int segments, int sides_Z, int size_Z) {
start_pos = pos.get();
param = parameter;
segm = segments;
sides = sides_Z;
sizeZ = size_Z;
getXZContour();
}
public void draw() {
float angle = 360 / sides;
//draw torus
float tposY = contour[contour.length-1].y + 4;
pushMatrix();
translate(0,0,tposY);
rotateZ(PI/2);
drawTorus(4,2.5,20,20,1);
popMatrix();
pushStyle();
noStroke();
noFill();
// draw top shape
beginShape();
for (int j = 0 ; j < sides; j++) {
float x = cos( radians( j * angle ) ) * (contour[contour.length-1].x - start_pos.x);
float y = sin( radians( j * angle ) ) * (contour[contour.length-1].x - start_pos.x);
vertex( x, y,(contour[contour.length-1].y));
}
endShape(CLOSE);
for (int i = 0; i < contour.length-1; i++) {
// draw body
if ( i == 0 ) {
beginShape(TRIANGLE_STRIP);
for (int j = sides + 1; j > 0; j--) {
float x1 = cos( radians( j * angle ) ) * (contour[i].x - start_pos.x);
float y1 = sin( radians( j * angle ) ) * (contour[i].x - start_pos.x);
float x2 = cos( radians( j * angle ) ) * (contour[i+1].x - start_pos.x);
float y2 = sin( radians( j * angle ) ) * (contour[i+1].x - start_pos.x);
vertex( x2 + start_pos.x, y2, contour[i+1].y - 0.25);
vertex( x1 + start_pos.x, y1, contour[i].y);
}
endShape(CLOSE);
} else if ( i == contour.length-2) {
beginShape(TRIANGLE_STRIP);
for (int j = sides + 1; j > 0; j--) {
float x1 = cos( radians( j * angle ) ) * (contour[i].x - start_pos.x);
float y1 = sin( radians( j * angle ) ) * (contour[i].x - start_pos.x);
float x2 = cos( radians( j * angle ) ) * (contour[i+1].x - start_pos.x);
float y2 = sin( radians( j * angle ) ) * (contour[i+1].x - start_pos.x);
vertex( x2 + start_pos.x, y2, contour[i+1].y);
vertex( x1 + start_pos.x, y1, contour[i].y - 0.25);
}
endShape(CLOSE);
} else {
beginShape(TRIANGLE_STRIP);
for (int j = sides + 1; j > 0; j--) {
float x1 = cos( radians( j * angle ) ) * (contour[i].x - start_pos.x);
float y1 = sin( radians( j * angle ) ) * (contour[i].x - start_pos.x);
float x2 = cos( radians( j * angle ) ) * (contour[i+1].x - start_pos.x);
float y2 = sin( radians( j * angle ) ) * (contour[i+1].x - start_pos.x);
vertex( x1 + start_pos.x, y1, contour[i].y + 0.25);
vertex( x1 + start_pos.x, y1, contour[i].y - 0.25);
}
endShape(CLOSE);
beginShape(TRIANGLE_STRIP);
for (int j = sides + 1; j > 0; j--) {
float x1 = cos( radians( j * angle ) ) * (contour[i].x - start_pos.x);
float y1 = sin( radians( j * angle ) ) * (contour[i].x - start_pos.x);
float x2 = cos( radians( j * angle ) ) * (contour[i+1].x - start_pos.x);
float y2 = sin( radians( j * angle ) ) * (contour[i+1].x - start_pos.x);
vertex( x2 + start_pos.x, y2, contour[i+1].y - 0.25);
vertex( x1 + start_pos.x, y1, contour[i].y + 0.25);
}
endShape(CLOSE);
}
}
// draw bottom shape
beginShape();
for (int j = sides; j > 0; j--) {
float x = cos( radians( j * angle ) ) * (contour[0].x - start_pos.x);
float y = sin( radians( j * angle ) ) * (contour[0].x - start_pos.x);
vertex( x, y, contour[0].y);
}
endShape(CLOSE);
popStyle();
}
// creates XZ contour from param
private void getXZContour() {
contour = new PVector[(param.length)+3];
float segm_height_increase = (float)sizeZ/(float)(param.length+1);
float segm_height = start_pos.y;
contour[0] = new PVector(5,segm_height);
segm_height += segm_height_increase;
for ( int i = 0; i < param.length; i++) {
contour[i+1] = new PVector(param[i].x,param[i].y);
// make shure all angels ar bigger than 30 degree
float angle = getAngle(contour[i], contour[i+1], new PVector((contour[i].x * 2),contour[i].y));
if(angle < 40) {
PVector h = PVector.fromAngle(radians(40));
float diff_fac = (contour[i+1].x - contour[i].x) / h.x;
h.mult(diff_fac);
h.add(contour[i]);
contour[i+1] = h;
} else if ( angle > 140) {
PVector h = PVector.fromAngle(radians(140));
float diff_fac = (contour[i+1].x - contour[i].x) / h.x;
h.mult(diff_fac);
h.add(contour[i]);
contour[i+1] = h;
}
segm_height += segm_height_increase;
}
contour[contour.length-2] = new PVector(2.5,segm_height);
segm_height += 2;
contour[contour.length-1] = new PVector(2.5,segm_height);
}
// creates XZ contour line with arc's between param
private void getXZContour_arc() {
PVector lpos, npos; // last and next point
float x_middle = start_pos.x; // middle of curve
float angle, angle_step, angle_rad; // angle of arc
int array_c = 0;
contour = new PVector[(param.length*segm)-segm];
lpos = new PVector(start_pos.x+param[0].x,start_pos.y);
npos = new PVector();
// skip first ellement
for(int i=1; i < param.length; i++) {
npos.set(start_pos.x + param[i].x, lpos.y + (sizeZ/(param.length -1) ));
println(npos);
PVector center = getArcCenter(lpos,npos,new PVector(x_middle,0),new PVector(x_middle, height));
float radius = center.dist(lpos);
angle = getAngle(center, npos, lpos);
angle_rad = radians(angle);
angle_step = angle_rad/segm;
for (int j = 0; j<segm; j++) {
float x = center.x+((lpos.x - center.x)*cos(angle_step*j))+((center.y-lpos.y)*sin(angle_step*j));
float y = center.y+((lpos.y-center.y)*cos(angle_step*j))+((lpos.x-center.x)*sin(angle_step*j));
contour[array_c] = new PVector(x,y);
array_c++;
}
lpos = npos.get();
}
}
// find intersection between line p1-p2 and m1-m2
private PVector getArcCenter(PVector p1, PVector p2, PVector m1, PVector m2) {
// first get the center between the two points
PVector c1 = new PVector( ((p1.x + p2.x)/2), ((p1.y + p2.y)/2));
PVector c2;
// get a new point that is 45deg from c1 and p1 or p2
if ( p1.x < p2.x ) {
c2 = new PVector( (c1.x + (p2.y - c1.y)), (c1.y - (p2.x - c1.x)));
}
else {
c2 = new PVector( (c1.x + (p2.y - c1.y)), (c1.y - (p2.x - c1.x)));
}
// now calculate where the lines passing c1-c2 and m1-m2 intersect
float bx = m2.x - m1.x;
float by = m2.y - m1.y;
float dx = c2.x - c1.x;
float dy = c2.y - c1.y;
float b_dot_d_perp = bx*dy - by*dx;
if(b_dot_d_perp == 0) {
return null;
}
float cx = c1.x-m1.x;
float cy = c1.y-m1.y;
float t = (cx*dy - cy*dx) / b_dot_d_perp;
return new PVector(m1.x+t*bx, m1.y+t*by);
}
// function to calculate the angle using three points.
private float getAngle(PVector v1, PVector v2, PVector v3) {
float l1x = v2.x - v1.x;
float l1y = v2.y - v1.y;
float l2x = v3.x - v1.x;
float l2y = v3.y - v1.y;
float angle1 = (float)Math.atan2(l1y, l1x);
float angle2 = (float) Math.atan2(l2y, l2x);
float degree=degrees((float)angle1-(float)angle2);
if (degree < 0) {
degree += 360;
}
return degree;
}
private void drawTorus(float outerRad, float innerRad, int numc, int numt, int axis) {
float x, y, z, s, t, u, v;
float nx, ny, nz;
float aInner, aOuter;
int idx = 0;
beginShape(QUAD_STRIP);
for (int i = numc-1; i >= 0; i--) {
for (int j = numt; j >= 0; j--) {
t = j;
v = t / (float)numt;
aOuter = v * TWO_PI;
float cOut = cos(aOuter);
float sOut = sin(aOuter);
for (int k = 1; k >= 0; k--) {
s = (i + k);
u = s / (float)numc;
aInner = u * TWO_PI;
float cIn = cos(aInner);
float sIn = sin(aInner);
if (axis == 0) {
x = (outerRad + innerRad * cIn) * cOut;
y = (outerRad + innerRad * cIn) * sOut;
z = innerRad * sIn;
} else if (axis == 1) {
x = innerRad * sIn;
y = (outerRad + innerRad * cIn) * sOut;
z = (outerRad + innerRad * cIn) * cOut;
} else {
x = (outerRad + innerRad * cIn) * cOut;
y = innerRad * sIn;
z = (outerRad + innerRad * cIn) * sOut;
}
nx = cIn * cOut;
ny = cIn * sOut;
nz = sIn;
normal(nx, nx, nz);
vertex(x, y, z);
}
}
}
endShape();
}
}