~pvigo/+junk/processing-3.1.2

« back to all changes in this revision

Viewing changes to debian/processing/usr/share/processing-3.1.2/modes/java/libraries/pdf/examples/Complex3D/Complex3D.pde

  • Committer: Pablo Vigo
  • Date: 2016-08-16 10:54:55 UTC
  • Revision ID: pvigo@xtec.cat-20160816105455-4y3x00r4w5anrpr4
2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/**
 
2
 * PDF Complex
 
3
 * by Marius Watz (workshop.evolutionzone.com). 
 
4
 * 
 
5
 * Example using PDF to output complex 3D geometry for print.
 
6
 * Press "s" to save a PDF.
 
7
*/
 
8
 
 
9
 
 
10
import processing.opengl.*;
 
11
import processing.pdf.*;
 
12
 
 
13
// Trig lookup tables borrowed from Toxi. Cryptic but effective
 
14
float sinLUT[];
 
15
float cosLUT[];
 
16
float SINCOS_PRECISION=1.0;
 
17
int SINCOS_LENGTH= int((360.0/SINCOS_PRECISION));
 
18
 
 
19
// System data
 
20
boolean dosave=false;
 
21
int num;
 
22
float pt[];
 
23
int style[];
 
24
 
 
25
 
 
26
void setup() {
 
27
  size(600, 600, OPENGL);
 
28
  frameRate(24);
 
29
  background(255);
 
30
  
 
31
  // Fill the tables
 
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);
 
37
  }
 
38
 
 
39
  num = 150;
 
40
  pt = new float[6*num]; // rotx, roty, deg, rad, w, speed
 
41
  style = new int[2*num]; // color, render style
 
42
 
 
43
  // Set up arc shapes
 
44
  int index=0;
 
45
  float prob;
 
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
 
49
 
 
50
    pt[index++] = random(60,80); // Short to quarter-circle arcs
 
51
    if(random(100)>90) pt[index]=(int)random(8,27)*10;
 
52
 
 
53
    pt[index++] = int(random(2,50)*5); // Radius. Space them out nicely
 
54
 
 
55
    pt[index++] = random(4,32); // Width of band
 
56
    if(random(100)>90) pt[index]=random(40,60); // Width of band
 
57
 
 
58
    pt[index++] = radians(random(5,30))/5; // Speed of rotation
 
59
 
 
60
    // get colors
 
61
    prob = random(100);
 
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);
 
66
 
 
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);
 
70
 
 
71
    style[i*2+1]=(int)(random(100))%3;
 
72
  }
 
73
}
 
74
 
 
75
void draw() {
 
76
 
 
77
  if(dosave) {
 
78
    // set up PGraphicsPDF for use with beginRaw()
 
79
    PGraphicsPDF pdf = (PGraphicsPDF)beginRaw(PDF, "pdf_complex_out.pdf"); 
 
80
 
 
81
    // set default Illustrator stroke styles and paint background rect.
 
82
    pdf.strokeJoin(MITER);
 
83
    pdf.strokeCap(SQUARE);
 
84
    pdf.fill(0);
 
85
    pdf.noStroke();
 
86
    pdf.rect(0,0, width,height);
 
87
  }
 
88
 
 
89
  background(0);
 
90
 
 
91
  int index=0;
 
92
  translate(width/2,height/2,0);
 
93
  rotateX(PI/6);
 
94
  rotateY(PI/6);
 
95
 
 
96
  for (int i=0; i<num; i++) {
 
97
    pushMatrix();
 
98
 
 
99
    rotateX(pt[index++]);
 
100
    rotateY(pt[index++]);
 
101
 
 
102
    if(style[i*2+1]==0) {
 
103
      stroke(style[i*2]);
 
104
      noFill();
 
105
      strokeWeight(1);
 
106
      arcLine(0,0, pt[index++],pt[index++],pt[index++]);
 
107
    }
 
108
    else if(style[i*2+1]==1) {
 
109
      fill(style[i*2]);
 
110
      noStroke();
 
111
      arcLineBars(0,0, pt[index++],pt[index++],pt[index++]);
 
112
    }
 
113
    else {
 
114
      fill(style[i*2]);
 
115
      noStroke();
 
116
      arc(0,0, pt[index++],pt[index++],pt[index++]);
 
117
    }
 
118
 
 
119
    // increase rotation
 
120
    pt[index-5]+=pt[index]/10;
 
121
    pt[index-4]+=pt[index++]/20;
 
122
 
 
123
    popMatrix();
 
124
  }
 
125
 
 
126
  if(dosave) {
 
127
    endRaw();
 
128
    dosave=false;
 
129
  }
 
130
}
 
131
 
 
132
 
 
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) {
 
137
 
 
138
  r2 = (r2 - r);
 
139
  g2 = (g2 - g);
 
140
  b2 = (b2 - b);
 
141
  return color(r + r2 * fract, g + g2 * fract, b + b2 * fract, a);
 
142
}
 
143
 
 
144
 
 
145
// Draw arc line
 
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);
 
149
 
 
150
  for (int j=0; j<numlines; j++) {
 
151
    beginShape();
 
152
    for (int i=0; i<a; i++) { 
 
153
      vertex(cosLUT[i]*rad+x,sinLUT[i]*rad+y);
 
154
    }
 
155
    endShape();
 
156
    rad += 2;
 
157
  }
 
158
}
 
159
 
 
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)));
 
163
  a /= 4;
 
164
 
 
165
  beginShape(QUADS);
 
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);
 
171
  }
 
172
  endShape();
 
173
}
 
174
 
 
175
// Draw solid arc
 
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);
 
182
  }
 
183
  endShape();
 
184
}
 
185
 
 
186
void keyPressed() {
 
187
  if (key == 's') { 
 
188
    dosave=true;
 
189
  }
 
190
}
 
191
 
 
192
void mouseReleased() {
 
193
  background(255);
 
194
}