3
* by Marius Watz (workshop.evolutionzone.com).
5
* Example using PDF to output complex 3D geometry for print.
6
* Press "s" to save a PDF.
10
import processing.opengl.*;
11
import processing.pdf.*;
13
// Trig lookup tables borrowed from Toxi. Cryptic but effective
16
float SINCOS_PRECISION=1.0;
17
int SINCOS_LENGTH= int((360.0/SINCOS_PRECISION));
27
size(600, 600, OPENGL);
32
sinLUT=new float[SINCOS_LENGTH];
33
cosLUT=new float[SINCOS_LENGTH];
34
for (int i = 0; i < SINCOS_LENGTH; i++) {
35
sinLUT[i]= (float)Math.sin(i*DEG_TO_RAD*SINCOS_PRECISION);
36
cosLUT[i]= (float)Math.cos(i*DEG_TO_RAD*SINCOS_PRECISION);
40
pt = new float[6*num]; // rotx, roty, deg, rad, w, speed
41
style = new int[2*num]; // color, render style
46
for (int i=0; i<num; i++) {
47
pt[index++] = random(PI*2); // Random X axis rotation
48
pt[index++] = random(PI*2); // Random Y axis rotation
50
pt[index++] = random(60,80); // Short to quarter-circle arcs
51
if(random(100)>90) pt[index]=(int)random(8,27)*10;
53
pt[index++] = int(random(2,50)*5); // Radius. Space them out nicely
55
pt[index++] = random(4,32); // Width of band
56
if(random(100)>90) pt[index]=random(40,60); // Width of band
58
pt[index++] = radians(random(5,30))/5; // Speed of rotation
62
if(prob<30) style[i*2]=colorBlended(random(1), 255,0,100, 255,0,0, 210);
63
else if(prob<70) style[i*2]=colorBlended(random(1), 0,153,255, 170,225,255, 210);
64
else if(prob<90) style[i*2]=colorBlended(random(1), 200,255,0, 150,255,0, 210);
65
else style[i*2]=color(255,255,255, 220);
67
if(prob<50) style[i*2]=colorBlended(random(1), 200,255,0, 50,120,0, 210);
68
else if(prob<90) style[i*2]=colorBlended(random(1), 255,100,0, 255,255,0, 210);
69
else style[i*2]=color(255,255,255, 220);
71
style[i*2+1]=(int)(random(100))%3;
78
// set up PGraphicsPDF for use with beginRaw()
79
PGraphicsPDF pdf = (PGraphicsPDF)beginRaw(PDF, "pdf_complex_out.pdf");
81
// set default Illustrator stroke styles and paint background rect.
82
pdf.strokeJoin(MITER);
83
pdf.strokeCap(SQUARE);
86
pdf.rect(0,0, width,height);
92
translate(width/2,height/2,0);
96
for (int i=0; i<num; i++) {
100
rotateY(pt[index++]);
102
if(style[i*2+1]==0) {
106
arcLine(0,0, pt[index++],pt[index++],pt[index++]);
108
else if(style[i*2+1]==1) {
111
arcLineBars(0,0, pt[index++],pt[index++],pt[index++]);
116
arc(0,0, pt[index++],pt[index++],pt[index++]);
120
pt[index-5]+=pt[index]/10;
121
pt[index-4]+=pt[index++]/20;
133
// Get blend of two colors
134
public int colorBlended(float fract,
135
float r, float g, float b,
136
float r2, float g2, float b2, float a) {
141
return color(r + r2 * fract, g + g2 * fract, b + b2 * fract, a);
146
public void arcLine(float x,float y,float deg,float rad,float w) {
147
int a=(int)(min (deg/SINCOS_PRECISION,SINCOS_LENGTH-1));
148
int numlines=(int)(w/2);
150
for (int j=0; j<numlines; j++) {
152
for (int i=0; i<a; i++) {
153
vertex(cosLUT[i]*rad+x,sinLUT[i]*rad+y);
160
// Draw arc line with bars
161
public void arcLineBars(float x,float y,float deg,float rad,float w) {
162
int a = int((min (deg/SINCOS_PRECISION,SINCOS_LENGTH-1)));
166
for (int i=0; i<a; i+=4) {
167
vertex(cosLUT[i]*(rad)+x,sinLUT[i]*(rad)+y);
168
vertex(cosLUT[i]*(rad+w)+x,sinLUT[i]*(rad+w)+y);
169
vertex(cosLUT[i+2]*(rad+w)+x,sinLUT[i+2]*(rad+w)+y);
170
vertex(cosLUT[i+2]*(rad)+x,sinLUT[i+2]*(rad)+y);
176
public void arc(float x,float y,float deg,float rad,float w) {
177
int a = int(min (deg/SINCOS_PRECISION,SINCOS_LENGTH-1));
178
beginShape(QUAD_STRIP);
179
for (int i = 0; i < a; i++) {
180
vertex(cosLUT[i]*(rad)+x,sinLUT[i]*(rad)+y);
181
vertex(cosLUT[i]*(rad+w)+x,sinLUT[i]*(rad+w)+y);
192
void mouseReleased() {