blue book examples
@@ -0,0 +1,3 @@
|
||||
// The expression 4 + 5 evaluates to 9, then the
|
||||
// value 9 is assigned to the variable x
|
||||
int x = 4 + 5;
|
||||
@@ -0,0 +1,4 @@
|
||||
// The expression 5 * 10 evaluates to 50, then the
|
||||
// expression 4 + 50 evaluates to 54, then the
|
||||
// value 54 is then assigned to the variable x
|
||||
int x = 4 + 5 * 10;
|
||||
@@ -0,0 +1,4 @@
|
||||
// The expression 4 + 5 evaluates to 9, then the
|
||||
// expression 9 * 10 evaluates to 90, then the
|
||||
// value 90 is assigned to the variable x
|
||||
int x = (4 + 5) * 10;
|
||||
@@ -0,0 +1,4 @@
|
||||
float w = 12.0 - 6.0 + 3.0; // Assigns 9 to w
|
||||
float x = 3.0 + 6.0 / 12.0; // Assigns 3.5 to x
|
||||
float y = 12.0 / 6.0 * 3.0; // Assigns 6 to y
|
||||
float z = 3.0 * 6.0 / 12.0; // Assigns 1.5 to z
|
||||
@@ -0,0 +1,2 @@
|
||||
float int = 50; // ERROR! Unexpected token: float
|
||||
line(int, 0, int, 100);
|
||||
@@ -0,0 +1,2 @@
|
||||
int line = 50; // This does not create a program error
|
||||
line(line, 0, line, 100); // but it's very confusing
|
||||
@@ -0,0 +1,4 @@
|
||||
int a = 205; // In binary: 00000000000000000000000011001101
|
||||
int b = 45; // In binary: 00000000000000000000000000101101
|
||||
a = a << 24; // Converts to 11001101000000000000000000000000
|
||||
b = b << 8; // Converts to 00000000000000000010110100000000
|
||||
@@ -0,0 +1,6 @@
|
||||
color c = color(204, 153, 102, 255);
|
||||
float r = (c >> 16) & 0xFF; // Faster version of red(c)
|
||||
float g = (c >> 8) & 0xFF; // Faster version of green(c)
|
||||
float b = c & 0xFF; // Faster version of blue(c)
|
||||
float a = (c >> 24) & 0xFF; // Faster version of alpha(c)
|
||||
println(r + ", " + g + ", " + b + ", " + a);
|
||||
@@ -0,0 +1,5 @@
|
||||
int a = 255;
|
||||
int r = 102;
|
||||
int g = 51;
|
||||
int b = 255;
|
||||
color c = (a << 24) | (r << 16) | (g << 8) | b;
|
||||
@@ -0,0 +1,5 @@
|
||||
// AVOID loading an image within draw(), it is slow
|
||||
void draw() {
|
||||
PImage img = loadImage("tower.jpg");
|
||||
image(img, 0, 0);
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
// AVOID creating an array inside draw(), it is slow
|
||||
void draw() {
|
||||
int[] values = new int[200];
|
||||
// Do something with the array here
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
// Converts (x,y) coordinates into a position in the pixels[] array
|
||||
loadPixels();
|
||||
for (int y = 0; y < height; y++) {
|
||||
for (int x = 0; x < width; x++) {
|
||||
pixels[y*height + x] = color(102);
|
||||
}
|
||||
}
|
||||
updatePixels();
|
||||
@@ -0,0 +1,10 @@
|
||||
// Replaces the multiplication y*height with an addition
|
||||
int offset = 0;
|
||||
loadPixels();
|
||||
for (int y = 0; y < height; y++) {
|
||||
for (int x = 0; x < width; x++) {
|
||||
pixels[offset + x] = color(102);
|
||||
}
|
||||
offset += width; // Avoids the multiply
|
||||
}
|
||||
updatePixels();
|
||||
@@ -0,0 +1,9 @@
|
||||
// Avoid the calculation y*height+width
|
||||
int index = 0;
|
||||
loadPixels();
|
||||
for (int y = 0; y < height; y++) {
|
||||
for (int x = 0; x < width; x++) {
|
||||
pixels[index++] = color(102);
|
||||
}
|
||||
}
|
||||
updatePixels();
|
||||
@@ -0,0 +1,7 @@
|
||||
// Avoids (x,y) coordinates
|
||||
int wh = width*height;
|
||||
loadPixels();
|
||||
for (int index = 0; index < wh; index++) {
|
||||
pixels[index] = color(102);
|
||||
}
|
||||
updatePixels();
|
||||
@@ -0,0 +1,8 @@
|
||||
// Only calculate the color once
|
||||
int wh = width*height;
|
||||
color c = color(102);
|
||||
loadPixels();
|
||||
for (int index = 0; index < wh; index++) {
|
||||
pixels[index] = c;
|
||||
}
|
||||
updatePixels();
|
||||
@@ -0,0 +1,17 @@
|
||||
int res = 16; // Number of data elements
|
||||
float[] x = new float[res]; // Create x-coordinate array
|
||||
float[] y = new float[res]; // Create y-coordinate array
|
||||
|
||||
void setup() {
|
||||
size(100, 100);
|
||||
for (int i = 0; i < res; i++) {
|
||||
x[i] = cos(PI/res * i); // Sets x-coordinates
|
||||
y[i] = sin(PI/res * i); // Sets y-coordinates
|
||||
}
|
||||
}
|
||||
|
||||
void draw() {
|
||||
for (int i = 0; i < res; i++) { // Access each point
|
||||
point(50 + x[i]*40, 50 + y[i]*40); // Draws point on a curve
|
||||
}
|
||||
}
|
||||
16
java/examples/Books/Processing Handbook/Extensions/3D/_01/_01.pde
Executable file
@@ -0,0 +1,16 @@
|
||||
// Rotate a rectangle around the y-axis and x-axis
|
||||
void setup() {
|
||||
size(400, 400, P3D);
|
||||
fill(204);
|
||||
}
|
||||
|
||||
void draw() {
|
||||
background(0);
|
||||
translate(width / 2, height / 2, -width);
|
||||
rotateY(map(mouseX, 0, width, -PI, PI));
|
||||
rotateX(map(mouseY, 0, height, -PI, PI));
|
||||
noStroke();
|
||||
rect(-200, -200, 400, 400);
|
||||
stroke(255);
|
||||
line(0, 0, -200, 0, 0, 200);
|
||||
}
|
||||
22
java/examples/Books/Processing Handbook/Extensions/3D/_02/_02.pde
Executable file
@@ -0,0 +1,22 @@
|
||||
// Draw a sphere on top of a box and moves the coordinates with the mouse
|
||||
// Press a mouse button to turn on the lights
|
||||
void setup() {
|
||||
size(400, 400, P3D);
|
||||
}
|
||||
|
||||
void draw() {
|
||||
background(0);
|
||||
if (mousePressed == true) { // If the mouse is pressed,
|
||||
lights(); // turn on lights
|
||||
}
|
||||
noStroke();
|
||||
pushMatrix();
|
||||
translate(mouseX, mouseY, -500);
|
||||
rotateY(PI / 6); // Rotate around y-axis
|
||||
box(400, 100, 400); // Draw box
|
||||
pushMatrix();
|
||||
popMatrix();
|
||||
translate(0, -200, 0); // Position the sphere
|
||||
sphere(150); // Draw sphere on top of box
|
||||
popMatrix();
|
||||
}
|
||||
61
java/examples/Books/Processing Handbook/Extensions/3D/_03/_03.pde
Executable file
@@ -0,0 +1,61 @@
|
||||
// Draw a cylinder centered on the y-axis, going down from y=0 to y=height.
|
||||
// The radius at the top can be different from the radius at the bottom,
|
||||
// and the number of sides drawn is variable.
|
||||
|
||||
void setup() {
|
||||
size(400, 400, P3D);
|
||||
}
|
||||
|
||||
void draw() {
|
||||
background(0);
|
||||
lights();
|
||||
translate(width / 2, height / 2);
|
||||
rotateY(map(mouseX, 0, width, 0, PI));
|
||||
rotateZ(map(mouseY, 0, height, 0, -PI));
|
||||
noStroke();
|
||||
fill(255, 255, 255);
|
||||
translate(0, -40, 0);
|
||||
drawCylinder(10, 180, 200, 16); // Draw a mix between a cylinder and a cone
|
||||
//drawCylinder(70, 70, 120, 64); // Draw a cylinder
|
||||
//drawCylinder(0, 180, 200, 4); // Draw a pyramid
|
||||
}
|
||||
|
||||
void drawCylinder(float topRadius, float bottomRadius, float tall, int sides) {
|
||||
float angle = 0;
|
||||
float angleIncrement = TWO_PI / sides;
|
||||
beginShape(QUAD_STRIP);
|
||||
for (int i = 0; i < sides + 1; ++i) {
|
||||
vertex(topRadius*cos(angle), 0, topRadius*sin(angle));
|
||||
vertex(bottomRadius*cos(angle), tall, bottomRadius*sin(angle));
|
||||
angle += angleIncrement;
|
||||
}
|
||||
endShape();
|
||||
|
||||
// If it is not a cone, draw the circular top cap
|
||||
if (topRadius != 0) {
|
||||
angle = 0;
|
||||
beginShape(TRIANGLE_FAN);
|
||||
|
||||
// Center point
|
||||
vertex(0, 0, 0);
|
||||
for (int i = 0; i < sides + 1; i++) {
|
||||
vertex(topRadius * cos(angle), 0, topRadius * sin(angle));
|
||||
angle += angleIncrement;
|
||||
}
|
||||
endShape();
|
||||
}
|
||||
|
||||
// If it is not a cone, draw the circular bottom cap
|
||||
if (bottomRadius != 0) {
|
||||
angle = 0;
|
||||
beginShape(TRIANGLE_FAN);
|
||||
|
||||
// Center point
|
||||
vertex(0, tall, 0);
|
||||
for (int i = 0; i < sides + 1; i++) {
|
||||
vertex(bottomRadius * cos(angle), tall, bottomRadius * sin(angle));
|
||||
angle += angleIncrement;
|
||||
}
|
||||
endShape();
|
||||
}
|
||||
}
|
||||
41
java/examples/Books/Processing Handbook/Extensions/3D/_04/_04.pde
Executable file
@@ -0,0 +1,41 @@
|
||||
// Export a DXF file when the R key is pressed
|
||||
import processing.dxf.*;
|
||||
boolean record = false;
|
||||
|
||||
void setup() {
|
||||
size(400, 400, P3D);
|
||||
noStroke();
|
||||
sphereDetail(12);
|
||||
}
|
||||
|
||||
void draw() {
|
||||
if (record == true) {
|
||||
beginRaw(DXF, "output.dxf"); // Start recording to the file
|
||||
}
|
||||
lights();
|
||||
background(0);
|
||||
translate(width / 3, height / 3, -200);
|
||||
rotateZ(map(mouseY, 0, height, 0, PI));
|
||||
rotateY(map(mouseX, 0, width, 0, HALF_PI));
|
||||
for (int y = -2; y < 2; y++) {
|
||||
for (int x = -2; x < 2; x++) {
|
||||
for (int z = -2; z < 2; z++) {
|
||||
pushMatrix();
|
||||
translate(120*x, 120*y, -120*z);
|
||||
sphere(30);
|
||||
popMatrix();
|
||||
}
|
||||
}
|
||||
}
|
||||
if (record == true) {
|
||||
endRaw();
|
||||
record = false; // Stop recording to the file
|
||||
}
|
||||
}
|
||||
|
||||
void keyPressed() {
|
||||
if (key == 'R' || key == 'r') { // Press R to save the file
|
||||
record = true;
|
||||
}
|
||||
}
|
||||
|
||||
24
java/examples/Books/Processing Handbook/Extensions/3D/_05/_05.pde
Executable file
@@ -0,0 +1,24 @@
|
||||
// Import and display an OBJ model
|
||||
// requires OBJ loader from: http://users.design.ucla.edu/%7Etatsuyas/tools/objloader/index.htm
|
||||
import saito.objloader.*;
|
||||
OBJModel model;
|
||||
|
||||
void setup() {
|
||||
size(400, 400, P3D);
|
||||
model = new OBJModel(this);
|
||||
model.load("chair.obj"); // Model must be in the data directory
|
||||
model.drawMode(POLYGON);
|
||||
noStroke();
|
||||
}
|
||||
|
||||
void draw() {
|
||||
background(0);
|
||||
lights();
|
||||
pushMatrix();
|
||||
translate(width / 2, height, -width);
|
||||
rotateY(map(mouseX, 0, width, -PI, PI));
|
||||
rotateX(PI / 4);
|
||||
scale(6.0);
|
||||
model.draw();
|
||||
popMatrix();
|
||||
}
|
||||
418
java/examples/Books/Processing Handbook/Extensions/3D/_05/data/chair.obj
Executable file
@@ -0,0 +1,418 @@
|
||||
# Max2Obj Version 6.0 Jul 29th, 2003
|
||||
#
|
||||
# object Zidle_A_01 to come ...
|
||||
#
|
||||
v 25.899904251099 23.408611297607 -0.629936158657
|
||||
v 25.836200714111 25.364303588867 -0.263177931309
|
||||
v 27.800601959229 25.365760803223 0.069837525487
|
||||
v 27.862743377686 23.408617019653 -0.297489136457
|
||||
v 18.490375518799 14.684046745300 47.418430328369
|
||||
v 18.440059661865 16.955835342407 47.789150238037
|
||||
v 20.043802261353 16.503309249878 49.659057617188
|
||||
v 20.093072891235 14.531045913696 49.332595825195
|
||||
v -16.846343994141 14.688239097595 46.918945312500
|
||||
v -16.802133560181 16.952724456787 47.287803649902
|
||||
v -18.368827819824 16.504928588867 49.132251739502
|
||||
v -18.412220001221 14.528832435608 48.805358886719
|
||||
v -25.921289443970 23.417802810669 -0.664852082729
|
||||
v -25.860712051392 25.372976303101 -0.295692503452
|
||||
v -27.811634063721 25.357145309448 0.108631089330
|
||||
v -27.870645523071 23.400211334229 -0.261199921370
|
||||
v 25.899911880493 -23.408607482910 -0.629936039448
|
||||
v 18.490377426147 -14.684038162231 47.418426513672
|
||||
v 18.440063476563 -16.955827713013 47.789150238037
|
||||
v 25.836208343506 -25.364299774170 -0.263179510832
|
||||
v 20.043806076050 -16.503301620483 49.659053802490
|
||||
v 27.800609588623 -25.365755081177 0.069835931063
|
||||
v 20.093074798584 -14.531042098999 49.332595825195
|
||||
v 27.862751007080 -23.408609390259 -0.297488987446
|
||||
v -16.846340179443 -14.688243865967 46.918941497803
|
||||
v -16.802129745483 -16.952732086182 47.287799835205
|
||||
v -18.368824005127 -16.504932403564 49.132247924805
|
||||
v -18.412216186523 -14.528839111328 48.805355072021
|
||||
v -25.921281814575 -23.417812347412 -0.664851963520
|
||||
v -25.860704421997 -25.372985839844 -0.295694082975
|
||||
v -27.811626434326 -25.357158660889 0.108629494905
|
||||
v -27.870637893677 -23.400218963623 -0.261199772358
|
||||
v -18.623430252075 16.727024078369 47.560352325439
|
||||
v -16.677518844604 16.706054687500 47.985088348389
|
||||
v -16.678075790405 14.717395782471 47.891605377197
|
||||
v -18.623422622681 14.736910820007 47.466884613037
|
||||
v -27.272499084473 15.222574234009 84.866127014160
|
||||
v -25.269447326660 15.205821037292 85.282051086426
|
||||
v -24.996809005737 13.190135955811 84.316917419434
|
||||
v -27.163030624390 13.216644287109 83.865890502930
|
||||
v -28.435161590576 9.060468673706 90.615600585938
|
||||
v -26.399826049805 9.061535835266 91.022407531738
|
||||
v -25.888158798218 8.663914680481 88.721542358398
|
||||
v -28.023075103760 8.663537025452 88.295997619629
|
||||
v -27.277370452881 -14.577423095703 84.897369384766
|
||||
v -25.276775360107 -14.559969902039 85.312210083008
|
||||
v -24.988948822021 -12.567503929138 84.289779663086
|
||||
v -27.159948348999 -12.594882965088 83.838508605957
|
||||
v -18.626295089722 -16.608646392822 47.575656890869
|
||||
v -16.680219650269 -16.586662292480 48.000041961670
|
||||
v -16.675401687622 -14.599632263184 47.876705169678
|
||||
v -18.620721817017 -14.620159149170 47.452362060547
|
||||
v -28.497978210449 -7.499320507050 90.926193237305
|
||||
v -26.461677551270 -7.500489711761 91.333389282227
|
||||
v -25.948324203491 -7.194749355316 89.017547607422
|
||||
v -28.081348419189 -7.194259643555 88.592170715332
|
||||
v -28.929925918579 0.780594170094 93.339157104492
|
||||
v -26.910228729248 0.780539214611 93.742111206055
|
||||
v -26.363113403320 0.734772324562 91.350273132324
|
||||
v -28.515964508057 0.734819710255 90.918952941895
|
||||
v 19.686010360718 14.649426460266 47.787284851074
|
||||
v -17.228097915649 14.649420738220 47.787284851074
|
||||
v 19.686014175415 -14.441827774048 47.787284851074
|
||||
v -17.228094100952 -14.441833496094 47.787284851074
|
||||
v 19.686010360718 14.649426460266 48.887374877930
|
||||
v -17.228097915649 14.649420738220 48.887374877930
|
||||
v 19.686014175415 -14.441827774048 48.887374877930
|
||||
v -17.228094100952 -14.441833496094 48.887374877930
|
||||
v 19.105789184570 14.173642158508 49.527408599854
|
||||
v -16.647876739502 14.173636436462 49.527408599854
|
||||
v -16.647872924805 -13.966049194336 49.527408599854
|
||||
v 19.105792999268 -13.966043472290 49.527408599854
|
||||
v 12.830347061157 9.088206291199 49.338916778564
|
||||
v -10.372433662415 9.088202476501 49.338916778564
|
||||
v -10.372431755066 -8.880614280701 49.338916778564
|
||||
v 12.830348968506 -8.880610466003 49.338916778564
|
||||
v 22.474662780762 4.952341556549 44.959941864014
|
||||
v 22.474662780762 -4.744742870331 44.959941864014
|
||||
v 23.136726379395 -4.744740962982 45.838500976563
|
||||
v 23.136726379395 4.952343463898 45.838500976563
|
||||
v 23.058534622192 -4.586146831512 46.698844909668
|
||||
v 23.058536529541 4.793749332428 46.698841094971
|
||||
v 16.227718353271 -2.891003131866 49.338916778564
|
||||
v 16.227716445923 3.098601818085 49.338916778564
|
||||
v -26.214628219604 -13.175243377686 83.052528381348
|
||||
v -19.325101852417 -14.434352874756 53.286815643311
|
||||
v -26.214639663696 13.382827758789 83.052528381348
|
||||
v -19.325113296509 14.641944885254 53.286819458008
|
||||
v -25.143526077271 -13.175243377686 83.303398132324
|
||||
v -18.253999710083 -14.434352874756 53.537693023682
|
||||
v -25.143537521362 13.382827758789 83.303405761719
|
||||
v -18.254011154175 14.641945838928 53.537693023682
|
||||
v -24.388038635254 -12.740889549255 82.884429931641
|
||||
v -17.763151168823 -13.958812713623 54.248584747314
|
||||
v -17.763162612915 14.166405677795 54.248584747314
|
||||
v -24.388048171997 12.948475837708 82.884429931641
|
||||
v -22.705802917480 -8.098277091980 76.833168029785
|
||||
v -18.943153381348 -8.875991821289 60.417484283447
|
||||
v -18.943161010742 9.083584785461 60.417484283447
|
||||
v -22.705812454224 8.305867195129 76.833168029785
|
||||
v -26.989404678345 -4.322551250458 86.360374450684
|
||||
v -26.989406585693 4.530138492584 86.360374450684
|
||||
v -25.918304443359 4.530136585236 86.611251831055
|
||||
v -25.918302536011 -4.322553157806 86.611244201660
|
||||
v -25.162815093994 4.385353565216 86.192276000977
|
||||
v -25.162813186646 -4.177768230438 86.192276000977
|
||||
v -23.480581283569 2.837816715240 80.141014099121
|
||||
v -23.480579376221 -2.630231380463 80.141014099121
|
||||
v -19.325109481812 4.949844360352 53.286819458008
|
||||
v -19.325107574463 -4.742256164551 53.286819458008
|
||||
v -18.254005432129 -4.742254257202 53.537693023682
|
||||
v -18.254007339478 4.949846267700 53.537693023682
|
||||
v -17.763156890869 -4.583739757538 54.248584747314
|
||||
v -17.763158798218 4.791332721710 54.248584747314
|
||||
v -18.943157196045 -2.889466762543 60.417484283447
|
||||
v -18.943159103394 3.097059726715 60.417484283447
|
||||
# 116 vertices
|
||||
|
||||
vt -24.983833312988 -26.063531875610 -0.694362461567
|
||||
vt -24.938697814941 -25.800083160400 0.694362521172
|
||||
vt -26.332603454590 -25.561277389526 0.694362461567
|
||||
vt -26.377738952637 -25.824726104736 -0.694362521172
|
||||
vt -17.487480163574 21.850849151611 -9.238789558411
|
||||
vt -17.448310852051 22.079463958740 -7.843263626099
|
||||
vt -18.641181945801 23.491695404053 -8.072401046753
|
||||
vt -18.680351257324 23.263076782227 -9.467928886414
|
||||
vt 18.140369415283 21.356575012207 -9.239566802979
|
||||
vt 18.105138778687 21.584888458252 -7.843820571899
|
||||
vt 19.258998870850 22.964744567871 -8.071624755859
|
||||
vt 19.294229507446 22.736431121826 -9.467370033264
|
||||
vt 27.285417556763 -26.089975357056 -0.687983930111
|
||||
vt 27.242393493652 -25.824773788452 0.700474679470
|
||||
vt 28.626508712769 -25.534833908081 0.687984049320
|
||||
vt 28.669532775879 -25.800035476685 -0.700474560261
|
||||
vt -24.983833312988 -26.063533782959 -47.974983215332
|
||||
vt -17.487480163574 21.850845336914 -39.430557250977
|
||||
vt -17.448310852051 22.079463958740 -40.826084136963
|
||||
vt -24.938697814941 -25.800086975098 -49.363708496094
|
||||
vt -18.641181945801 23.491691589355 -40.596946716309
|
||||
vt -26.332603454590 -25.561281204224 -49.363708496094
|
||||
vt -18.680351257324 23.263076782227 -39.201419830322
|
||||
vt -26.377738952637 -25.824728012085 -47.974983215332
|
||||
vt 18.140369415283 21.356571197510 -39.429779052734
|
||||
vt 18.105138778687 21.584884643555 -40.825527191162
|
||||
vt 19.258998870850 22.964740753174 -40.597721099854
|
||||
vt 19.294229507446 22.736427307129 -39.201976776123
|
||||
vt 27.285417556763 -26.089977264404 -47.981361389160
|
||||
vt 27.242393493652 -25.824777603149 -49.369819641113
|
||||
vt 28.626508712769 -25.534837722778 -49.357330322266
|
||||
vt 28.669532775879 -25.800037384033 -47.968868255615
|
||||
vt 19.550821304321 22.079088211060 -7.911951065063
|
||||
vt 18.168672561646 22.378185272217 -7.926216125488
|
||||
vt 18.168672561646 22.310815811157 -9.338824272156
|
||||
vt 19.550821304321 22.011718750000 -9.324559211731
|
||||
vt 28.077938079834 58.962402343750 -9.466709136963
|
||||
vt 26.692619323730 59.246894836426 -9.480277061462
|
||||
vt 26.553951263428 58.540180206299 -10.862394332886
|
||||
vt 27.939270019531 58.255687713623 -10.848826408386
|
||||
vt 29.193300247192 64.552856445313 -15.466491699219
|
||||
vt 27.806550979614 64.830207824707 -15.467369079590
|
||||
vt 27.461914062500 63.104469299316 -15.501105308533
|
||||
vt 28.848716735840 62.827384948730 -15.500129699707
|
||||
vt 28.081987380981 58.982704162598 -38.561283111572
|
||||
vt 26.696582794189 59.266757965088 -38.547275543213
|
||||
vt 26.549911499023 58.519870758057 -37.177703857422
|
||||
vt 27.935316085815 58.235816955566 -37.191711425781
|
||||
vt 19.552829742432 22.089979171753 -40.634887695313
|
||||
vt 18.170631408691 22.388816833496 -40.619941711426
|
||||
vt 18.166673660278 22.299924850464 -39.208530426025
|
||||
vt 19.548871994019 22.001087188721 -39.223476409912
|
||||
vt 29.254201889038 64.856658935547 -31.654993057251
|
||||
vt 27.867467880249 65.134078979492 -31.654163360596
|
||||
vt 27.522718429565 63.408370971680 -31.687810897827
|
||||
vt 28.909502029419 63.131198883057 -31.688722610474
|
||||
vt 29.685031890869 67.214920043945 -23.560743331909
|
||||
vt 28.298290252686 67.492309570313 -23.560766220093
|
||||
vt 27.953598022461 65.766586303711 -23.594459533691
|
||||
vt 29.340389251709 65.489463806152 -23.594427108765
|
||||
vt 1.000000000000 0.000000000000 0.000000000000
|
||||
vt 0.000000000000 0.000000000000 0.000000000000
|
||||
vt 1.000000000000 1.000000000000 0.000000000000
|
||||
vt 0.000000000000 1.000000000000 0.000000000000
|
||||
vt 0.000000000000 0.000000000000 0.000000000000
|
||||
vt 1.000000000000 0.000000000000 0.000000000000
|
||||
vt 0.000000000000 1.000000000000 0.000000000000
|
||||
vt 1.000000000000 1.000000000000 0.000000000000
|
||||
vt 0.000000000000 0.000000000000 0.000000000000
|
||||
vt 1.000000000000 0.000000000000 0.000000000000
|
||||
vt 0.000000000000 1.000000000000 0.000000000000
|
||||
vt 1.000000000000 1.000000000000 0.000000000000
|
||||
vt 0.000000000000 0.000000000000 0.000000000000
|
||||
vt 1.000000000000 0.000000000000 0.000000000000
|
||||
vt 0.000000000000 1.000000000000 0.000000000000
|
||||
vt 1.000000000000 1.000000000000 0.000000000000
|
||||
vt 0.000000000000 0.000000000000 0.000000000000
|
||||
vt 1.000000000000 0.000000000000 0.000000000000
|
||||
vt 0.000000000000 1.000000000000 0.000000000000
|
||||
vt 1.000000000000 1.000000000000 0.000000000000
|
||||
vt 0.000000000000 0.000000000000 0.000000000000
|
||||
vt 1.000000000000 0.000000000000 0.000000000000
|
||||
vt 1.000000000000 1.000000000000 0.000000000000
|
||||
vt 0.000000000000 1.000000000000 0.000000000000
|
||||
vt 0.000000000000 0.000000000000 0.000000000000
|
||||
vt 1.000000000000 0.000000000000 0.000000000000
|
||||
vt 1.000000000000 1.000000000000 0.000000000000
|
||||
vt 0.000000000000 1.000000000000 0.000000000000
|
||||
vt 1.000000000000 0.333333343267 0.000000000000
|
||||
vt 1.000000000000 0.666666686535 0.000000000000
|
||||
vt 0.666666626930 0.000000000000 0.000000000000
|
||||
vt 0.333333313465 0.000000000000 0.000000000000
|
||||
vt 0.333333343267 1.000000000000 0.000000000000
|
||||
vt 0.666666686535 1.000000000000 0.000000000000
|
||||
vt 0.000000000000 0.666666626930 0.000000000000
|
||||
vt 0.000000000000 0.333333313465 0.000000000000
|
||||
vt 0.000000000000 0.666666626930 0.000000000000
|
||||
vt 0.000000000000 0.333333313465 0.000000000000
|
||||
vt 1.000000000000 0.000000000000 0.000000000000
|
||||
vt 0.000000000000 0.000000000000 0.000000000000
|
||||
vt 1.000000000000 1.000000000000 0.000000000000
|
||||
vt 0.000000000000 1.000000000000 0.000000000000
|
||||
vt 0.000000000000 0.000000000000 0.000000000000
|
||||
vt 1.000000000000 0.000000000000 0.000000000000
|
||||
vt 0.000000000000 1.000000000000 0.000000000000
|
||||
vt 1.000000000000 1.000000000000 0.000000000000
|
||||
vt 0.000000000000 0.000000000000 0.000000000000
|
||||
vt 1.000000000000 0.000000000000 0.000000000000
|
||||
vt 0.000000000000 1.000000000000 0.000000000000
|
||||
vt 1.000000000000 1.000000000000 0.000000000000
|
||||
vt 0.000000000000 0.000000000000 0.000000000000
|
||||
vt 1.000000000000 0.000000000000 0.000000000000
|
||||
vt 0.000000000000 1.000000000000 0.000000000000
|
||||
vt 1.000000000000 1.000000000000 0.000000000000
|
||||
vt 0.000000000000 0.000000000000 0.000000000000
|
||||
vt 1.000000000000 0.000000000000 0.000000000000
|
||||
vt 0.000000000000 1.000000000000 0.000000000000
|
||||
vt 1.000000000000 1.000000000000 0.000000000000
|
||||
vt 0.000000000000 0.000000000000 0.000000000000
|
||||
vt 1.000000000000 0.000000000000 0.000000000000
|
||||
vt 1.000000000000 1.000000000000 0.000000000000
|
||||
vt 0.000000000000 1.000000000000 0.000000000000
|
||||
vt 0.000000000000 0.000000000000 0.000000000000
|
||||
vt 1.000000000000 0.000000000000 0.000000000000
|
||||
vt 1.000000000000 1.000000000000 0.000000000000
|
||||
vt 0.000000000000 1.000000000000 0.000000000000
|
||||
vt 1.000000000000 0.333333343267 0.000000000000
|
||||
vt 1.000000000000 0.666666686535 0.000000000000
|
||||
vt 0.666666626930 0.000000000000 0.000000000000
|
||||
vt 0.333333313465 0.000000000000 0.000000000000
|
||||
vt 0.333333343267 1.000000000000 0.000000000000
|
||||
vt 0.666666686535 1.000000000000 0.000000000000
|
||||
vt 0.000000000000 0.666666626930 0.000000000000
|
||||
vt 0.000000000000 0.333333313465 0.000000000000
|
||||
vt 0.000000000000 0.666666626930 0.000000000000
|
||||
vt 0.000000000000 0.333333313465 0.000000000000
|
||||
vt 0.000000000000 0.666666626930 0.000000000000
|
||||
vt 0.000000000000 0.333333313465 0.000000000000
|
||||
vt 0.666666626930 0.000000000000 0.000000000000
|
||||
vt 0.333333313465 0.000000000000 0.000000000000
|
||||
vt 0.333333343267 1.000000000000 0.000000000000
|
||||
vt 0.666666686535 1.000000000000 0.000000000000
|
||||
vt 1.000000000000 0.333333343267 0.000000000000
|
||||
vt 1.000000000000 0.666666686535 0.000000000000
|
||||
vt 1.000000000000 0.333333343267 0.000000000000
|
||||
vt 1.000000000000 0.666666686535 0.000000000000
|
||||
# 146 texture vertices
|
||||
|
||||
g Zidle_A_01
|
||||
s 1
|
||||
f 1/1 5/5 6/6 2/2
|
||||
f 2/2 6/6 7/7 3/3
|
||||
f 3/3 7/7 8/8 4/4
|
||||
f 4/4 8/8 5/5 1/1
|
||||
f 5/5 9/9 10/10 6/6
|
||||
f 6/6 10/10 11/11 7/7
|
||||
f 7/7 11/11 12/12 8/8
|
||||
f 8/8 12/12 9/9 5/5
|
||||
f 9/9 13/13 14/14 10/10
|
||||
f 10/10 14/14 15/15 11/11
|
||||
f 11/11 15/15 16/16 12/12
|
||||
f 12/12 16/16 13/13 9/9
|
||||
f 1/1 2/2 3/3 4/4
|
||||
f 17/17 20/20 19/19 18/18
|
||||
f 20/20 22/22 21/21 19/19
|
||||
f 22/22 24/24 23/23 21/21
|
||||
f 24/24 17/17 18/18 23/23
|
||||
f 18/18 19/19 26/26 25/25
|
||||
f 19/19 21/21 27/27 26/26
|
||||
f 21/21 23/23 28/28 27/27
|
||||
f 23/23 18/18 25/25 28/28
|
||||
f 25/25 26/26 30/30 29/29
|
||||
f 26/26 27/27 31/31 30/30
|
||||
f 27/27 28/28 32/32 31/31
|
||||
f 28/28 25/25 29/29 32/32
|
||||
f 17/17 24/24 22/22 20/20
|
||||
f 33/33 37/37 38/38 34/34
|
||||
f 34/34 38/38 39/39 35/35
|
||||
f 35/35 39/39 40/40 36/36
|
||||
f 36/36 40/40 37/37 33/33
|
||||
f 37/37 41/41 42/42 38/38
|
||||
f 38/38 42/42 43/43 39/39
|
||||
f 39/39 43/43 44/44 40/40
|
||||
f 40/40 44/44 41/41 37/37
|
||||
f 53/53 45/45 46/46 54/54
|
||||
f 54/54 46/46 47/47 55/55
|
||||
f 55/55 47/47 48/48 56/56
|
||||
f 56/56 48/48 45/45 53/53
|
||||
f 45/45 49/49 50/50 46/46
|
||||
f 46/46 50/50 51/51 47/47
|
||||
f 47/47 51/51 52/52 48/48
|
||||
f 48/48 52/52 49/49 45/45
|
||||
f 33/33 34/34 35/35 36/36
|
||||
f 42/42 41/41 57/57 58/58
|
||||
f 43/43 42/42 58/58 59/59
|
||||
f 44/44 43/43 59/59 60/60
|
||||
f 41/41 44/44 60/60 57/57
|
||||
f 58/58 57/57 53/53 54/54
|
||||
f 59/59 58/58 54/54 55/55
|
||||
f 60/60 59/59 55/55 56/56
|
||||
f 57/57 60/60 56/56 53/53
|
||||
s 0
|
||||
f 64/64 77/89 78/90 63/63
|
||||
f 64/64 62/62 61/61 77/89
|
||||
s 1
|
||||
f 75/87 84/98 73/85 74/86
|
||||
f 83/97 84/98 75/87 76/88
|
||||
s 2
|
||||
f 66/68 65/67 61/65 62/66
|
||||
s 4
|
||||
f 68/72 66/71 62/69 64/70
|
||||
s 2
|
||||
f 67/76 68/75 64/73 63/74
|
||||
s 4
|
||||
f 79/93 78/92 77/91 80/94
|
||||
s 2
|
||||
f 70/82 69/81 65/67 66/68
|
||||
s 4
|
||||
f 71/83 70/82 66/71 68/72
|
||||
s 2
|
||||
f 72/84 71/83 68/75 67/76
|
||||
s 5
|
||||
f 81/95 79/93 80/94 82/96
|
||||
s 3
|
||||
f 74/86 73/85 69/81 70/82
|
||||
s 5
|
||||
f 75/87 74/86 70/82 71/83
|
||||
s 3
|
||||
f 76/88 75/87 71/83 72/84
|
||||
s 1
|
||||
f 83/97 81/95 82/96 84/98
|
||||
s 4
|
||||
f 80/94 77/91 61/78 65/80
|
||||
f 78/92 79/93 67/79 63/77
|
||||
f 69/81 82/96 80/94 65/80
|
||||
f 79/93 81/95 72/84 67/79
|
||||
s 5
|
||||
f 73/85 84/98 82/96 69/81
|
||||
f 81/95 83/97 76/88 72/84
|
||||
s 2
|
||||
f 110/138 86/100 85/99 101/127
|
||||
s 1
|
||||
f 115/145 116/146 97/123 98/124
|
||||
f 99/125 108/136 97/123 116/146
|
||||
f 107/135 108/136 99/125 100/126
|
||||
s 4
|
||||
f 90/106 89/105 85/103 86/104
|
||||
s 8
|
||||
f 111/141 110/140 109/139 112/142
|
||||
s 4
|
||||
f 91/114 92/113 88/111 87/112
|
||||
f 103/131 102/130 101/129 104/132
|
||||
f 94/120 93/119 89/105 90/106
|
||||
s 9
|
||||
f 113/143 111/141 112/142 114/144
|
||||
s 4
|
||||
f 96/122 95/121 92/113 91/114
|
||||
s 5
|
||||
f 105/133 103/131 104/132 106/134
|
||||
f 98/124 97/123 93/119 94/120
|
||||
s 1
|
||||
f 115/145 113/143 114/144 116/146
|
||||
s 5
|
||||
f 100/126 99/125 95/121 96/122
|
||||
s 1
|
||||
f 107/135 105/133 106/134 108/136
|
||||
s 4
|
||||
f 104/132 101/129 85/116 89/118
|
||||
f 102/130 103/131 91/117 87/115
|
||||
s 5
|
||||
f 93/119 106/134 104/132 89/118
|
||||
f 103/131 105/133 96/122 91/117
|
||||
s 1
|
||||
f 97/123 108/136 106/134 93/119
|
||||
f 105/133 107/135 100/126 96/122
|
||||
s 8
|
||||
f 112/142 109/139 88/108 92/110
|
||||
f 110/140 111/141 90/109 86/107
|
||||
f 95/121 114/144 112/142 92/110
|
||||
f 111/141 113/143 94/120 90/109
|
||||
s 9
|
||||
f 99/125 116/146 114/144 95/121
|
||||
f 113/143 115/145 98/124 94/120
|
||||
s 2
|
||||
f 109/137 102/128 87/101 88/102
|
||||
f 110/138 101/127 102/128 109/137
|
||||
s 1
|
||||
f 15/15 14/14 13/13
|
||||
f 16/16 15/15 13/13
|
||||
f 31/31 29/29 30/30
|
||||
f 32/32 29/29 31/31
|
||||
f 51/51 50/50 49/49
|
||||
f 52/52 51/51 49/49
|
||||
# 109 faces
|
||||
|
||||
g
|
||||
20
java/examples/Books/Processing Handbook/Extensions/3D/_06/_06.pde
Executable file
@@ -0,0 +1,20 @@
|
||||
// The camera lifts up while looking at the same point
|
||||
void setup() {
|
||||
size(400, 400, P3D);
|
||||
fill(204);
|
||||
}
|
||||
|
||||
void draw() {
|
||||
lights();
|
||||
background(0);
|
||||
// Change height of the camera with mouseY
|
||||
camera(30.0, mouseY, 220.0, // eyeX, eyeY, eyeZ
|
||||
0.0, 0.0, 0.0, // centerX, centerY, centerZ
|
||||
0.0, 1.0, 0.0); // upX, upY, upZ
|
||||
noStroke();
|
||||
box(90);
|
||||
stroke(255);
|
||||
line(-100, 0, 0, 100, 0, 0);
|
||||
line(0, -100, 0, 0, 100, 0);
|
||||
line(0, 0, -100, 0, 0, 100);
|
||||
}
|
||||
19
java/examples/Books/Processing Handbook/Extensions/3D/_07/_07.pde
Executable file
@@ -0,0 +1,19 @@
|
||||
// Vary the specular reflection component of a material
|
||||
// with vertical position of the mouse
|
||||
void setup() {
|
||||
size(400, 400, P3D);
|
||||
noStroke();
|
||||
colorMode(RGB, 1);
|
||||
fill(0.4);
|
||||
}
|
||||
|
||||
void draw() {
|
||||
background(0);
|
||||
translate(width / 2, height / 2);
|
||||
// Set the specular color of lights that follow
|
||||
lightSpecular(1, 1, 1);
|
||||
directionalLight(0.8, 0.8, 0.8, 0, 0, -1);
|
||||
float s = mouseX / float(width);
|
||||
specular(s, s, s);
|
||||
sphere(100);
|
||||
}
|
||||
27
java/examples/Books/Processing Handbook/Extensions/3D/_08/_08.pde
Executable file
@@ -0,0 +1,27 @@
|
||||
// Draw a box with three different kinds of lights
|
||||
void setup() {
|
||||
size(400, 400, P3D);
|
||||
noStroke();
|
||||
}
|
||||
|
||||
void draw() {
|
||||
background(0);
|
||||
translate(width / 2, height / 2);
|
||||
// Orange point light on the right
|
||||
pointLight(150, 100, 0, // Color
|
||||
200, -150, 0); // Position
|
||||
|
||||
// Blue directional light from the left
|
||||
directionalLight(0, 102, 255, // Color
|
||||
1, 0, 0); // The x-, y-, z-axis direction
|
||||
|
||||
// Yellow spotlight from the front
|
||||
spotLight(255, 255, 109, // Color
|
||||
0, 40, 200, // Position
|
||||
0, -0.5, -0.5, // Direction
|
||||
PI / 2, 2); // Angle, concentration
|
||||
|
||||
rotateY(map(mouseX, 0, width, 0, PI));
|
||||
rotateX(map(mouseY, 0, height, 0, PI));
|
||||
box(200);
|
||||
}
|
||||
40
java/examples/Books/Processing Handbook/Extensions/3D/_09/_09.pde
Executable file
@@ -0,0 +1,40 @@
|
||||
// Load an image and draw it onto a cylinder and a quad
|
||||
int tubeRes = 32;
|
||||
float[] tubeX = new float[tubeRes];
|
||||
float[] tubeY = new float[tubeRes];
|
||||
PImage img;
|
||||
|
||||
void setup() {
|
||||
size(400, 400, P3D);
|
||||
img = loadImage("berlin-1.jpg");
|
||||
float angle = 270.0 / tubeRes;
|
||||
for (int i = 0; i < tubeRes; i++) {
|
||||
tubeX[i] = cos(radians(i * angle));
|
||||
tubeY[i] = sin(radians(i * angle));
|
||||
}
|
||||
noStroke();
|
||||
}
|
||||
|
||||
void draw() {
|
||||
background(0);
|
||||
translate(width / 2, height / 2);
|
||||
rotateX(map(mouseY, 0, height, -PI, PI));
|
||||
rotateY(map(mouseX, 0, width, -PI, PI));
|
||||
beginShape(QUAD_STRIP);
|
||||
texture(img);
|
||||
for (int i = 0; i < tubeRes; i++) {
|
||||
float x = tubeX[i] * 100;
|
||||
float z = tubeY[i] * 100;
|
||||
float u = img.width / tubeRes * i;
|
||||
vertex(x, -100, z, u, 0);
|
||||
vertex(x, 100, z, u, img.height);
|
||||
}
|
||||
endShape();
|
||||
beginShape(QUADS);
|
||||
texture(img);
|
||||
vertex(0, -100, 0, 0, 0);
|
||||
vertex(100, -100, 0, 100, 0);
|
||||
vertex(100, 100, 0, 100, 100);
|
||||
vertex(0, 100, 0, 0, 100);
|
||||
endShape();
|
||||
}
|
||||
16
java/examples/Books/Processing Handbook/Extensions/Electronics/_01A/_01A.pde
Executable file
@@ -0,0 +1,16 @@
|
||||
// Code for sensing a switch status and writing the value to the serial port
|
||||
int switchPin = 0; // Switch connected to pin 0
|
||||
|
||||
void setup() {
|
||||
pinMode(switchPin, INPUT); // Set pin 0 as an input
|
||||
Serial.begin(9600); // Start serial communication at 9600 bps
|
||||
}
|
||||
|
||||
void loop() {
|
||||
if (digitalRead(switchPin) == HIGH) { // If switch is ON,
|
||||
Serial.print(1, BYTE); // send 1 to Processing
|
||||
} else { // If the switch is not ON,
|
||||
Serial.print(0, BYTE); // send 0 to Processing
|
||||
}
|
||||
delay(100); // Wait 100 milliseconds
|
||||
}
|
||||
25
java/examples/Books/Processing Handbook/Extensions/Electronics/_01B/_01B.pde
Executable file
@@ -0,0 +1,25 @@
|
||||
// Read data from the serial port and change the color of a rectangle
|
||||
// when a switch connected to the board is pressed and released
|
||||
import processing.serial.*;
|
||||
Serial port; // Create object from Serial class
|
||||
int val; // Data received from the serial port
|
||||
|
||||
void setup() {
|
||||
size(200, 200);
|
||||
frameRate(10);
|
||||
// Open the port that the board is connected to and use the same speed (9600 bps)
|
||||
port = new Serial(this, 9600);
|
||||
}
|
||||
|
||||
void draw() {
|
||||
if (0 < port.available()) { // If data is available,
|
||||
val = port.read(); // read it and store it in val
|
||||
}
|
||||
background(255); // Set background to white
|
||||
if (val == 0) { // If the serial value is 0,
|
||||
fill(0); // set fill to black
|
||||
} else { // If the serial value is not 0,
|
||||
fill(204); // set fill to light gray
|
||||
}
|
||||
rect(50, 50, 100, 100);
|
||||
}
|
||||
13
java/examples/Books/Processing Handbook/Extensions/Electronics/_02A/_02A.pde
Executable file
@@ -0,0 +1,13 @@
|
||||
// Code to read an analog value and write it to the serial port
|
||||
int val;
|
||||
int inputPin = 0; // Set the input to analog in pin 0
|
||||
|
||||
void setup() {
|
||||
Serial.begin(9600); // Start serial communication at 9600 bps
|
||||
}
|
||||
|
||||
void loop() {
|
||||
val = analogRead(inputPin) / 4; // Read analog input pin, put in range 0 to 255
|
||||
Serial.print(val, BYTE); // Send the value
|
||||
delay(100); // Wait 100ms for next reading
|
||||
}
|
||||
23
java/examples/Books/Processing Handbook/Extensions/Electronics/_02B/_02B.pde
Executable file
@@ -0,0 +1,23 @@
|
||||
// Read data from the serial port and assign it to a variable. Set the fill a
|
||||
// rectangle on the screen using the value read from a light sensor connected
|
||||
// to the Wiring or Arduino board
|
||||
import processing.serial.*;
|
||||
Serial port; // Create object from Serial class
|
||||
int val; // Data received from the serial port
|
||||
|
||||
void setup() {
|
||||
size(200, 200);
|
||||
noStroke();
|
||||
frameRate(10); // Run 10 frames per second
|
||||
// Open the port that the board is connected to and use the same speed (9600 bps)
|
||||
port = new Serial(this, 9600);
|
||||
}
|
||||
|
||||
void draw() {
|
||||
if (0 < port.available()) { // If data is available to read,
|
||||
val = port.read(); // read it and store it in val
|
||||
}
|
||||
background(204); // Clear background
|
||||
fill(val); // Set fill color with the value read
|
||||
rect(50, 50, 100, 100); // Draw square
|
||||
}
|
||||
20
java/examples/Books/Processing Handbook/Extensions/Electronics/_03A/_03A.pde
Executable file
@@ -0,0 +1,20 @@
|
||||
// Read data from the serial and turn ON or OFF a light depending on the value
|
||||
char val; // Data received from the serial port
|
||||
int ledPin = 0; // Set the pin to digital I/O 0
|
||||
|
||||
void setup() {
|
||||
pinMode(ledPin, OUTPUT); // Set pin as OUTPUT
|
||||
Serial.begin(9600); // Start serial communication at 9600 bps
|
||||
}
|
||||
|
||||
void loop() {
|
||||
if (Serial.available()) { // If data is available to read,
|
||||
val = Serial.read(); // read it and store it in val
|
||||
}
|
||||
if (val == 'H') { // If H was received
|
||||
digitalWrite(ledPin, HIGH); // turn the LED on
|
||||
} else {
|
||||
digitalWrite(ledPin, LOW); // Otherwise turn it OFF
|
||||
}
|
||||
delay(100); // Wait 100 milliseconds for next reading
|
||||
}
|
||||
28
java/examples/Books/Processing Handbook/Extensions/Electronics/_03B/_03B.pde
Executable file
@@ -0,0 +1,28 @@
|
||||
// Check if the mouse is over a rectangle and writes the status to the serial port
|
||||
import processing.serial.*;
|
||||
|
||||
Serial port; // Create object from Serial class
|
||||
|
||||
void setup() {
|
||||
size(200, 200);
|
||||
noStroke();
|
||||
frameRate(10);
|
||||
// Open the port that the board is connected to and use the same speed (9600 bps)
|
||||
port = new Serial(this, 9600);
|
||||
}
|
||||
|
||||
void draw() {
|
||||
background(255);
|
||||
if (mouseOverRect() == true) { // If mouse is over square,
|
||||
fill(204); // change color and
|
||||
port.write('H'); // send an H to indicate mouse is over square
|
||||
} else { // If mouse is not over square,
|
||||
fill(0); // change color and
|
||||
port.write('L'); // send an L otherwise
|
||||
}
|
||||
rect(50, 50, 100, 100); // Draw a square
|
||||
}
|
||||
|
||||
boolean mouseOverRect() { // Test if mouse is over square
|
||||
return ((mouseX >= 50) && (mouseX <= 150) && (mouseY >= 50) && (mouseY <= 150));
|
||||
}
|
||||
19
java/examples/Books/Processing Handbook/Extensions/Electronics/_04A/_04A.pde
Executable file
@@ -0,0 +1,19 @@
|
||||
// Read data from the serial port and set the position of a servomotor
|
||||
// according to the value
|
||||
Servo myservo; // Create servo object to control a servo
|
||||
|
||||
int servoPin = 0; // Connect yellow servo wire to digital I/O pin 0
|
||||
int val = 0; // Data received from the serial port
|
||||
|
||||
void setup() {
|
||||
myservo.attach(servoPin); // Attach the servo to the PWM pin
|
||||
Serial.begin(9600); // Start serial communication at 9600 bps
|
||||
}
|
||||
|
||||
void loop() {
|
||||
if (Serial.available()) { // If data is available to read,
|
||||
val = Serial.read(); // read it and store it in val
|
||||
}
|
||||
myservo.write(val); // Set the servo position
|
||||
delay(15); // Wait for the servo to get there
|
||||
}
|
||||
32
java/examples/Books/Processing Handbook/Extensions/Electronics/_04B/_04B.pde
Executable file
@@ -0,0 +1,32 @@
|
||||
// Write data to the serial port according to the mouseX value
|
||||
import processing.serial.*;
|
||||
|
||||
Serial port; // Create object from Serial class
|
||||
float mx = 0.0;
|
||||
|
||||
void setup() {
|
||||
size(200, 200);
|
||||
noStroke();
|
||||
frameRate(10);
|
||||
// Open the port that the board is connected to and use the same speed (9600 bps)
|
||||
port = new Serial(this, 9600);
|
||||
}
|
||||
|
||||
void draw() {
|
||||
background(0); // Clear background
|
||||
fill(204); // Set fill color
|
||||
rect(40, height / 2 - 15, 120, 25); // Draw square
|
||||
float dif = mouseX - mx;
|
||||
if (abs(dif) > 1.0) {
|
||||
mx += dif / 4.0;
|
||||
}
|
||||
mx = constrain(mx, 50, 149); // Keeps marker on the screen
|
||||
noStroke();
|
||||
fill(255);
|
||||
rect(50, (height / 2) - 5, 100, 5);
|
||||
fill(204, 102, 0);
|
||||
rect(mx - 2, height / 2 - 5, 4, 5); // Draw the position marker
|
||||
int angle = int(map(mx, 50, 149, 0, 180)); // Scale the value the range 0-180
|
||||
//print(angle + " "); // Print the current angle (debug)
|
||||
port.write(angle); // Write the angle to the serial port
|
||||
}
|
||||
20
java/examples/Books/Processing Handbook/Extensions/Electronics/_05A/_05A.pde
Executable file
@@ -0,0 +1,20 @@
|
||||
// Read data from the serial and turns a DC motor on or off according to the value
|
||||
char val; // Data received from the serial port
|
||||
int motorpin = 0; // Wiring: Connect L293D Pin En1 connected to Pin PWM 0
|
||||
// int motorpin = 9; // Arduino: Connect L293D Pin En1 to Pin PWM 9
|
||||
|
||||
void setup() {
|
||||
Serial.begin(9600); // Start serial communication at 9600 bps
|
||||
}
|
||||
|
||||
void loop() {
|
||||
if (Serial.available()) { // If data is available,
|
||||
val = Serial.read(); // read it and store it in val
|
||||
}
|
||||
if (val == 'H') { // If 'H' was received,
|
||||
analogWrite(motorpin, 125); // turn the motor on at medium speed
|
||||
} else { // If 'H' was not received
|
||||
analogWrite(motorpin, 0); // turn the motor off
|
||||
}
|
||||
delay(100); // Wait 100 milliseconds for next reading
|
||||
}
|
||||
56
java/examples/Books/Processing Handbook/Extensions/Electronics/_05B/_05B.pde
Executable file
@@ -0,0 +1,56 @@
|
||||
// Write data to the serial port according to the status of a button controlled
|
||||
// by the mouse
|
||||
import processing.serial.*;
|
||||
Serial port; // Create serial port object
|
||||
boolean rectOver = false;
|
||||
int rectX, rectY; // Position of square button
|
||||
int rectSize = 100; // Diameter of rect
|
||||
color rectColor;
|
||||
boolean buttonOn = false; // Status of the button
|
||||
|
||||
void setup() {
|
||||
size(200, 200);
|
||||
noStroke();
|
||||
frameRate(10);
|
||||
rectColor = color(100);
|
||||
rectX = width / 2 - rectSize / 2;
|
||||
rectY = height / 2 - rectSize / 2;
|
||||
// Open the port that the board is connected to and use the same speed (9600 bps)
|
||||
port = new Serial(this, 9600);
|
||||
}
|
||||
|
||||
void draw() {
|
||||
update(mouseX, mouseY);
|
||||
background(0); // Clear background to black
|
||||
fill(rectColor);
|
||||
rect(rectX, rectY, rectSize, rectSize);
|
||||
}
|
||||
void update(int x, int y) {
|
||||
if (overRect(rectX, rectY, rectSize, rectSize) == true) {
|
||||
rectOver = true;
|
||||
} else {
|
||||
rectOver = false;
|
||||
}
|
||||
}
|
||||
|
||||
void mouseReleased() {
|
||||
if (rectOver == true) {
|
||||
if (buttonOn == true) {
|
||||
rectColor = color(100);
|
||||
buttonOn = false;
|
||||
port.write('L'); // Send an L to indicate button is OFF
|
||||
} else {
|
||||
rectColor = color(180);
|
||||
buttonOn = true;
|
||||
port.write('H'); // Send an H to indicate button is ON
|
||||
}
|
||||
}
|
||||
}
|
||||
boolean overRect(int x, int y, int width, int height) {
|
||||
if ((mouseX >= x) && (mouseX <= x + width) &&
|
||||
(mouseY >= y) && (mouseY <= y + height)) {
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
10
java/examples/Books/Processing Handbook/Extensions/Mobile/_01/_01.pde
Executable file
@@ -0,0 +1,10 @@
|
||||
// The image file, named sprite.png in this example, must be located in the
|
||||
// sketch data folder. From the Sketch menu, choose "Add File" to copy files into
|
||||
// the sketch data folder.
|
||||
PImage img = loadImage("sprite.png");
|
||||
// The coordinates (0, 0) refer to the top-left corder of the screen
|
||||
image(img, 0, 0);
|
||||
// The following coordinate calculations will center the image in the screen
|
||||
image(img, (width – img.width) / 2, (height – img.height) / 2);
|
||||
// Finally, the next line will position the image in the bottom-right corner
|
||||
image(img, width – img.width, height – img.height);
|
||||
21
java/examples/Books/Processing Handbook/Extensions/Mobile/_02/_02.pde
Executable file
@@ -0,0 +1,21 @@
|
||||
String s;
|
||||
PFont font;
|
||||
void setup() {
|
||||
font = loadFont(); // Load and set the default font for drawing text
|
||||
textFont(font);
|
||||
softkey("Input"); // Create a softkey called Input
|
||||
s = "No input"; // Initialize s with an initial message
|
||||
}
|
||||
void draw() {
|
||||
background(200);
|
||||
text(s, 0, height / 2); // Draw the String s in the middle of the screen
|
||||
}
|
||||
|
||||
void softkeyPressed(String label) {
|
||||
// Check the value of the softkey label to determine the action to take
|
||||
if (label.equals("Input")) {
|
||||
// If the Input softkey is pressed, open a textInput window for the user
|
||||
// to type text. It will be drawn on the screen by the draw() method
|
||||
s = textInput();
|
||||
}
|
||||
}
|
||||
18
java/examples/Books/Processing Handbook/Extensions/Mobile/_03/_03.pde
Executable file
@@ -0,0 +1,18 @@
|
||||
PFont font;
|
||||
|
||||
void setup() {
|
||||
font = loadFont();
|
||||
textFont(font);
|
||||
softkey("Delete"); // Use softkey to to delete characters from the multitap buffer
|
||||
multitap(); // Turn on multitap key input
|
||||
}
|
||||
|
||||
void draw() {
|
||||
background(200);
|
||||
text(multitapText, 0, height / 2); // Draw the text captured with multitap
|
||||
}
|
||||
void softkeyPressed(String label) {
|
||||
if (label.equals("Delete")) {
|
||||
multitapDeleteChar(); // Delete a character
|
||||
}
|
||||
}
|
||||
80
java/examples/Books/Processing Handbook/Extensions/Mobile/_04/_04.pde
Executable file
@@ -0,0 +1,80 @@
|
||||
// The PClient object is used to initiate requests to the server
|
||||
PClient c;
|
||||
// The PRequest object represents an active request from which we receive
|
||||
// status information and data from the server
|
||||
PRequest request;
|
||||
int counter;
|
||||
PFont font;
|
||||
PImage img;
|
||||
String version;
|
||||
String error;
|
||||
|
||||
void setup() {
|
||||
font = loadFont(); // Load and set the default font for drawing text
|
||||
textFont(font);
|
||||
fill(0);
|
||||
// Create a new network connection to connect to the Mobile Processing website
|
||||
c = new PClient(this, "mobile.processing.org");
|
||||
// Start by fetching the logo for Mobile Processing the filename is a relative path
|
||||
// specified in the same way as a URL in a webpage
|
||||
request = c.GET("/images/mobile.png");
|
||||
// Use the counter to keep track of what we're fetching
|
||||
counter = 0;
|
||||
}
|
||||
|
||||
void draw() {
|
||||
background(255);
|
||||
int y = 0;
|
||||
if (error != null) {
|
||||
// A network error has occured, so display the message
|
||||
y += font.baseline;
|
||||
text(error, 0, y);
|
||||
} else if (img == null) {
|
||||
// The img is not yet fetched, so draw a status message
|
||||
y += font.baseline;
|
||||
text("Fetching image...", 0, y);
|
||||
} else {
|
||||
// Draw the image
|
||||
image(img, (width - img.width) / 2, y);
|
||||
y += img.height + font.baseline;
|
||||
if (version == null) {
|
||||
// The version text is not yet fetched, so draw a status message
|
||||
text("Checking version...", 0, y);
|
||||
} else {
|
||||
// Draw the version as reported by the website
|
||||
text("Latest version: " + version, 0, y);
|
||||
}
|
||||
}
|
||||
}
|
||||
// The libraryEvent() will be called when a library, in this case the Net
|
||||
// library, has an event to report back to the program
|
||||
void libraryEvent(Object library, int event, Object data) {
|
||||
// Make sure we handle the event from the right library
|
||||
if (library == request) {
|
||||
if (event == PRequest.EVENT_CONNECTED) {
|
||||
// This event occurs when the connection is complete, so we can start
|
||||
// reading the data. The readBytes() method will read all the data returned
|
||||
// by the server and send another event when completed.
|
||||
request.readBytes();
|
||||
} else if (event == PRequest.EVENT_DONE) {
|
||||
// Reading is complete! Check the counter to see what we're transferring,
|
||||
// then process the data. The data object in this case is an array of bytes.
|
||||
byte[] bytes = (byte[]) data;
|
||||
if (counter == 0) {
|
||||
// This is the logo, so create an image from the bytes
|
||||
img = new PImage(bytes);
|
||||
// Now that we have the logo image, fetch the latest version text for
|
||||
// Mobile Processing. We use the client object to initiate a new request
|
||||
request = c.GET("/download/latest.txt");
|
||||
// Set the counter to 1 to represent the tex
|
||||
counter = 1;
|
||||
} else if (counter == 1) {
|
||||
// This is the version text, so create a string from the bytes
|
||||
version = new String(bytes);
|
||||
}
|
||||
} else if (event == PRequest.EVENT_ERROR) {
|
||||
// The data object in this case is an error message
|
||||
error = (String) data;
|
||||
}
|
||||
}
|
||||
}
|
||||
14
java/examples/Books/Processing Handbook/Extensions/Mobile/_05/_05.pde
Executable file
@@ -0,0 +1,14 @@
|
||||
import processing.sound.*;
|
||||
// Notes range from 0 to 127 as in the MIDI specification
|
||||
int[] notes = { 60, 62, 64, 65, 67, 69, 71, 72, 74 };
|
||||
|
||||
void setup() {
|
||||
noLoop(); // No drawing in this sketch, so we don't need to run the draw() loop
|
||||
}
|
||||
|
||||
void keyPressed() {
|
||||
if ((key >= '1') && (key <= '9')) {
|
||||
// Use the key as an index into the array of notes
|
||||
Sound.playTone(notes[key - '1'], 500, 80);
|
||||
}
|
||||
}
|
||||
18
java/examples/Books/Processing Handbook/Extensions/Mobile/_06/_06.pde
Executable file
@@ -0,0 +1,18 @@
|
||||
import processing.sound.*;
|
||||
Sound s;
|
||||
|
||||
void setup() {
|
||||
// The file, soundtrack.mid, must be copied into the data folder of this sketch
|
||||
s = new Sound("soundtrack.mid");
|
||||
softkey("Play");
|
||||
noLoop();
|
||||
}
|
||||
void softkeyPressed(String label) {
|
||||
if (label.equals("Play")) {
|
||||
s.play();
|
||||
softkey("Pause"); // Change the label of the softkey to Pause
|
||||
} else if (label.equals("Pause")) {
|
||||
s.pause();
|
||||
softkey("Play"); // Change the label of the softkey back to Play
|
||||
}
|
||||
}
|
||||
18
java/examples/Books/Processing Handbook/Extensions/Mobile/_07/_07.pde
Executable file
@@ -0,0 +1,18 @@
|
||||
import processing.sound.*;
|
||||
PFont font = loadFont();
|
||||
textFont(font);
|
||||
background(255);
|
||||
fill(0);
|
||||
|
||||
// Get a list of the supported types of media on the phone
|
||||
String[] types = Sound.supportedTypes();
|
||||
// Start at the top of the screen
|
||||
int y = font.baseline;
|
||||
// Draw each of the supported types on the screen
|
||||
for (int i = 0, length = types.length; i < length; i++) {
|
||||
// Draw the supported type (represented as an
|
||||
// Internet MIME type string, such as audio/x-wav)
|
||||
text(types[i], 0, y);
|
||||
// Go to the next line
|
||||
y += font.height;
|
||||
}
|
||||
30
java/examples/Books/Processing Handbook/Extensions/Mobile/_08/_08.pde
Executable file
@@ -0,0 +1,30 @@
|
||||
import processing.phone.*;
|
||||
Phone p;
|
||||
|
||||
void setup() {
|
||||
p = new Phone(this);
|
||||
noLoop(); // No drawing in this sketch, so we don't need to run the draw() loop
|
||||
}
|
||||
|
||||
void keyPressed() {
|
||||
switch (key) {
|
||||
case '1':
|
||||
// Vibrate the phone for 200 milliseconds
|
||||
p.vibrate(200);
|
||||
break;
|
||||
|
||||
case '2':
|
||||
// Flash the backlight for 200 milliseconds
|
||||
p.flash(200);
|
||||
break;
|
||||
|
||||
case '3':
|
||||
// Dial 411 on the phone
|
||||
p.call("411");
|
||||
break;
|
||||
case '4':
|
||||
// Launch the Web browser
|
||||
p.launch("http://mobile.processing.org/");
|
||||
break;
|
||||
}
|
||||
}
|
||||
21
java/examples/Books/Processing Handbook/Extensions/Network/_01/_01.pde
Executable file
@@ -0,0 +1,21 @@
|
||||
// A simple Web client using HTTP
|
||||
import processing.net.*;
|
||||
|
||||
Client c;
|
||||
String data;
|
||||
|
||||
void setup() {
|
||||
size(200, 200);
|
||||
background(50);
|
||||
fill(200);
|
||||
c = new Client(this, "www.processing.org", 80); // Connect to server on port 80
|
||||
c.write("GET / HTTP/1.0\n"); // Use the HTTP "GET" command to ask for a Web page
|
||||
c.write("Host: my_domain_name.com\n\n"); // Be polite and say who we are
|
||||
}
|
||||
|
||||
void draw() {
|
||||
if (c.available() > 0) { // If there's incoming data from the client...
|
||||
data += c.readString(); // ...then grab it and print it
|
||||
println(data);
|
||||
}
|
||||
}
|
||||
34
java/examples/Books/Processing Handbook/Extensions/Network/_02A/_02A.pde
Executable file
@@ -0,0 +1,34 @@
|
||||
import processing.net.*;
|
||||
|
||||
Server s;
|
||||
Client c;
|
||||
String input;
|
||||
int data[];
|
||||
|
||||
void setup() {
|
||||
size(450, 255);
|
||||
background(204);
|
||||
stroke(0);
|
||||
frameRate(5); // Slow it down a little
|
||||
s = new Server(this, 12345); // Start a simple server on a port
|
||||
}
|
||||
|
||||
void draw() {
|
||||
if (mousePressed == true) {
|
||||
// Draw our line
|
||||
stroke(255);
|
||||
line(pmouseX, pmouseY, mouseX, mouseY);
|
||||
// Send mouse coords to other person
|
||||
s.write(pmouseX + " " + pmouseY + " " + mouseX + " " + mouseY + "\n");
|
||||
}
|
||||
// Receive data from client
|
||||
c = s.available();
|
||||
if (c != null) {
|
||||
input = c.readString();
|
||||
input = input.substring(0, input.indexOf("\n")); // Only up to the newline
|
||||
data = int(split(input, ' ')); // Split values into an array
|
||||
// Draw line using received coords
|
||||
stroke(0);
|
||||
line(data[0], data[1], data[2], data[3]);
|
||||
}
|
||||
}
|
||||
33
java/examples/Books/Processing Handbook/Extensions/Network/_02B/_02B.pde
Executable file
@@ -0,0 +1,33 @@
|
||||
import processing.net.*;
|
||||
|
||||
Client c;
|
||||
String input;
|
||||
int data[];
|
||||
|
||||
void setup() {
|
||||
size(450, 255);
|
||||
background(204);
|
||||
stroke(0);
|
||||
frameRate(5); // Slow it down a little
|
||||
// Connect to the server’s IP address and port
|
||||
c = new Client(this, "127.0.0.1", 12345); // Replace with your server’s IP and port
|
||||
}
|
||||
|
||||
void draw() {
|
||||
if (mousePressed == true) {
|
||||
// Draw our line
|
||||
stroke(255);
|
||||
line(pmouseX, pmouseY, mouseX, mouseY);
|
||||
// Send mouse coords to other person
|
||||
c.write(pmouseX + " " + pmouseY + " " + mouseX + " " + mouseY + "\n");
|
||||
}
|
||||
// Receive data from server
|
||||
if (c.available() > 0) {
|
||||
input = c.readString();
|
||||
input = input.substring(0, input.indexOf("\n")); // Only up to the newline
|
||||
data = int(split(input, ' ')); // Split values into an array
|
||||
// Draw line using received coords
|
||||
stroke(0);
|
||||
line(data[0], data[1], data[2], data[3]);
|
||||
}
|
||||
}
|
||||
40
java/examples/Books/Processing Handbook/Extensions/Network/_03/_03.pde
Executable file
@@ -0,0 +1,40 @@
|
||||
// Download the Yahoo! Search SDK from http://developer.yahoo.com/download
|
||||
// Inside the download, find the yahoo_search-2.X.X.jar file somewhere inside
|
||||
// the "Java" subdirectory. Drag the jar file to your sketch and it will be
|
||||
// added to your 'code' folder for use.
|
||||
// This example is based on the based on yahoo api example
|
||||
// Replace this with a developer key from http://developer.yahoo.com
|
||||
String appid = "YOUR_DEVELOPER_KEY_HERE";
|
||||
SearchClient client = new SearchClient(appid);
|
||||
String query = "processing.org";
|
||||
WebSearchRequest request = new WebSearchRequest(query);
|
||||
|
||||
// (Optional) Set the maximum number of results to download
|
||||
//request.setResults(30);
|
||||
|
||||
try {
|
||||
WebSearchResults results = client.webSearch(request);
|
||||
// Print out how many hits were found
|
||||
println("Displaying " + results.getTotalResultsReturned() +
|
||||
" out of " + results.getTotalResultsAvailable() + " hits.");
|
||||
println();
|
||||
// Get a list of the search results
|
||||
WebSearchResult[] resultList = results.listResults();
|
||||
// Loop through the results and print them to the console
|
||||
|
||||
for (int i = 0; i < resultList.length; i++) {
|
||||
// Print out the document title and URL.
|
||||
println((i + 1) + ".");
|
||||
println(resultList[i].getTitle());
|
||||
println(resultList[i].getUrl());
|
||||
println();
|
||||
}
|
||||
|
||||
// Error handling below, see the documentation of the Yahoo! API for details
|
||||
} catch (IOException e) {
|
||||
println("Error calling Yahoo! Search Service: " + e.toString());
|
||||
e.printStackTrace();
|
||||
} catch (SearchException e) {
|
||||
println("Error calling Yahoo! Search Service: " + e.toString());
|
||||
e.printStackTrace();
|
||||
}
|
||||
68
java/examples/Books/Processing Handbook/Extensions/Network/_04/_04.pde
Executable file
@@ -0,0 +1,68 @@
|
||||
// Note: requires Carnivore Library for Processing v2.2 (http://r-s-g.org/carnivore)
|
||||
// Windows, first install winpcap (http://winpcap.org)
|
||||
// Mac, first open a Terminal and execute this commmand: sudo chmod 777 /dev/bpf*
|
||||
// (must be done each time you reboot your mac)
|
||||
import java.util.Iterator;
|
||||
import org.rsg.carnivore.*;
|
||||
import org.rsg.carnivore.net.*;
|
||||
HashMap nodes = new HashMap();
|
||||
float startDiameter = 100.0;
|
||||
float shrinkSpeed = 0.97;
|
||||
int splitter, x, y;
|
||||
PFont font;
|
||||
void setup() {
|
||||
size(800, 600);
|
||||
background(255);
|
||||
frameRate(10);
|
||||
Log.setDebug(true); // Uncomment this for verbose mode
|
||||
CarnivoreP5 c = new CarnivoreP5(this);
|
||||
//c.setVolumeLimit(4);
|
||||
// Use the "Create Font" tool to add a 12 point font to your sketch,
|
||||
// then use its name as the parameter to loadFont().
|
||||
font = loadFont("CourierNew-12.vlw");
|
||||
textFont(font);
|
||||
}
|
||||
|
||||
void draw() {
|
||||
background(255);
|
||||
drawNodes();
|
||||
}
|
||||
|
||||
// Iterate through each node
|
||||
synchronized void drawNodes() {
|
||||
Iterator it = nodes.keySet().iterator();
|
||||
while (it.hasNext()) {
|
||||
String ip = (String)it.next();
|
||||
float d = float(nodes.get(ip).toString());
|
||||
|
||||
// Use last two IP address bytes for x/y coords
|
||||
splitter = ip.lastIndexOf(".");
|
||||
y = int(ip.substring(splitter + 1)) * height / 255; // Scale to applet size
|
||||
String tmp = ip.substring(0, splitter);
|
||||
splitter = tmp.lastIndexOf(".");
|
||||
x = int(tmp.substring(splitter + 1)) * width / 255; // Scale to applet size
|
||||
|
||||
// Draw the node
|
||||
stroke(0);
|
||||
fill(color(100, 200)); // Rim
|
||||
ellipse(x, y, d, d); // Node circle
|
||||
noStroke();
|
||||
fill(color(100, 50)); // Halo
|
||||
ellipse(x, y, d + 20, d + 20);
|
||||
|
||||
// Draw the text
|
||||
fill(0);
|
||||
text(ip, x, y);
|
||||
|
||||
// Shrink the nodes a little
|
||||
nodes.put(ip, str(d * shrinkSpeed));
|
||||
}
|
||||
}
|
||||
|
||||
// Called each time a new packet arrives
|
||||
synchronized void packetEvent(CarnivorePacket packet) {
|
||||
println("[PDE] packetEvent: " + packet);
|
||||
// Remember these nodes in our hash map
|
||||
nodes.put(packet.receiverAddress.toString(), str(startDiameter));
|
||||
nodes.put(packet.senderAddress.toString(), str(startDiameter));
|
||||
}
|
||||
7
java/examples/Books/Processing Handbook/Extensions/Print/_01/_01.pde
Executable file
@@ -0,0 +1,7 @@
|
||||
import processing.pdf.*; // Import PDF code
|
||||
|
||||
size(600, 600, PDF, "line.pdf"); // Set PDF as the renderer
|
||||
background(255);
|
||||
stroke(0);
|
||||
line(200, 0, width/2, height); // Draw line to PDF
|
||||
exit(); // Stop the program
|
||||
9
java/examples/Books/Processing Handbook/Extensions/Print/_02/_02.pde
Executable file
@@ -0,0 +1,9 @@
|
||||
import processing.pdf.*; // Import PDF code
|
||||
|
||||
size(600, 600);
|
||||
beginRecord(PDF, "line.pdf"); // Start writing to PDF
|
||||
background(255);
|
||||
stroke(0, 20);
|
||||
strokeWeight(20);
|
||||
line(200, 0, 400, height); // Draw line to screen and to PDF
|
||||
endRecord(); // Stop writing to PDF
|
||||
25
java/examples/Books/Processing Handbook/Extensions/Print/_03/_03.pde
Executable file
@@ -0,0 +1,25 @@
|
||||
import processing.pdf.*; // Import PDF code
|
||||
|
||||
boolean saveOneFrame = false;
|
||||
|
||||
void setup() {
|
||||
size(600, 600);
|
||||
}
|
||||
|
||||
void draw() {
|
||||
if (saveOneFrame == true) { // When the saveOneFrame boolean is true,
|
||||
beginRecord(PDF, "line-####.pdf"); // start recording to the PDF
|
||||
}
|
||||
background(255);
|
||||
stroke(0, 20);
|
||||
strokeWeight(20);
|
||||
line(mouseX, 0, width - mouseY, height);
|
||||
if (saveOneFrame == true) { // If the PDF has been recording,
|
||||
endRecord(); // stop recording,
|
||||
saveOneFrame = false; // and set the boolean value to false
|
||||
}
|
||||
}
|
||||
|
||||
void mousePressed() { // When a mouse button is pressed,
|
||||
saveOneFrame = true; // trigger PDF recording within the draw()
|
||||
}
|
||||
22
java/examples/Books/Processing Handbook/Extensions/Print/_04/_04.pde
Executable file
@@ -0,0 +1,22 @@
|
||||
import processing.pdf.*; // Import PDF code
|
||||
|
||||
void setup() {
|
||||
size(600, 600);
|
||||
background(255);
|
||||
}
|
||||
|
||||
void draw() {
|
||||
stroke(0, 20);
|
||||
strokeWeight(20);
|
||||
line(mouseX, 0, width - mouseY, height);
|
||||
}
|
||||
|
||||
void keyPressed() {
|
||||
if (key == 'B' || key == 'b') { // When 'B' or 'b' is pressed,
|
||||
beginRecord(PDF, "lines.pdf"); // start recording to the PDF
|
||||
background(255); // Set a white background
|
||||
} else if (key == 'E' || key == 'e') { // When 'E' or 'e' is pressed,
|
||||
endRecord(); // stop recording the PDF and
|
||||
exit(); // quit the program
|
||||
}
|
||||
}
|
||||
10
java/examples/Books/Processing Handbook/Extensions/Print/_05/_05.pde
Executable file
@@ -0,0 +1,10 @@
|
||||
PGraphics big; // Declare a PGraphics variable
|
||||
|
||||
void setup() {
|
||||
big = createGraphics(3000, 3000, JAVA2D); // Create a new PGraphics object
|
||||
big.beginDraw(); // Start drawing to the PGraphics object
|
||||
big.background(128); // Set the background
|
||||
big.line(20, 1800, 1800, 900); // Draw a line
|
||||
big.endDraw(); // Stop drawing to the PGraphics object
|
||||
big.save("big.tif");
|
||||
}
|
||||
31
java/examples/Books/Processing Handbook/Extensions/Print/_06/_06.pde
Executable file
@@ -0,0 +1,31 @@
|
||||
// Draws an image larger than the screen by tiling it into small sections.
|
||||
// The scaleValue variable sets amount of scaling: 1 is 100%, 2 is 200%, etc.
|
||||
int scaleValue = 3; // Multiplication factor
|
||||
int xoffset = 0; // x-axis offset
|
||||
int yoffset = 0; // y-axis offset
|
||||
|
||||
void setup() {
|
||||
size(600, 600);
|
||||
stroke(0, 100);
|
||||
}
|
||||
|
||||
void draw() {
|
||||
scale(scaleValue);
|
||||
translate(xoffset *(-width / scaleValue), yoffset *(-height / scaleValue));
|
||||
line(10, 150, 500, 50);
|
||||
line(0, 600, 600, 0);
|
||||
setOffset();
|
||||
}
|
||||
|
||||
void setOffset() {
|
||||
save("lines-" + xoffset + "-" + yoffset + ".jpg");
|
||||
xoffset++;
|
||||
if (xoffset == scaleValue) {
|
||||
xoffset = 0;
|
||||
yoffset++;
|
||||
if (yoffset == scaleValue) {
|
||||
exit();
|
||||
}
|
||||
}
|
||||
background(204);
|
||||
}
|
||||
66
java/examples/Books/Processing Handbook/Extensions/Sound/_01/_01.pde
Executable file
@@ -0,0 +1,66 @@
|
||||
/**
|
||||
* Sound is generated in real time by summing together harmonically related
|
||||
* sine tones. Overall pitch and harmonic detuning is controlled by the mouse.
|
||||
* Based on the Spooky Stream Save Ess example
|
||||
*/
|
||||
|
||||
import krister.Ess.*;
|
||||
|
||||
int numSines = 5; // Number of oscillators to use
|
||||
AudioStream myStream; // Audio stream to write into
|
||||
SineWave[] myWave; // Array of sines
|
||||
FadeOut myFadeOut; // Amplitude ramp function
|
||||
FadeIn myFadeIn; // Amplitude ramp function
|
||||
|
||||
void setup() {
|
||||
size(256, 200);
|
||||
Ess.start(this); // Start Ess
|
||||
myStream = new AudioStream(); // Create a new AudioStream
|
||||
myStream.smoothPan = true;
|
||||
myWave = new SineWave[numSines]; // Initialize the oscillators
|
||||
for (int i = 0; i < myWave.length; i++) {
|
||||
float sinVolume = (1.0 / myWave.length) / (i + 1);
|
||||
myWave[i] = new SineWave(0, sinVolume);
|
||||
}
|
||||
myFadeOut = new FadeOut(); // Create amplitude ramp
|
||||
myFadeIn = new FadeIn(); // Create amplitude ramp
|
||||
myStream.start(); // Start audio
|
||||
}
|
||||
|
||||
void draw() {
|
||||
noStroke();
|
||||
fill(0, 20);
|
||||
rect(0, 0, width, height); // Draw the background
|
||||
float offset = millis() - myStream.bufferStartTime;
|
||||
int interp = int((offset / myStream.duration) * myStream.size);
|
||||
stroke(255);
|
||||
for (int i = 0; i < width; i++) {
|
||||
float y1 = mouseY;
|
||||
float y2 = y1;
|
||||
if (i + interp + 1 < myStream.buffer2.length) {
|
||||
y1 -= myStream.buffer2[i+interp] * height / 2;
|
||||
y2 -= myStream.buffer2[i+interp+1] * height / 2;
|
||||
}
|
||||
line(i, y1, i + 1, y2); // Draw the waves
|
||||
}
|
||||
}
|
||||
void audioStreamWrite(AudioStream s) {
|
||||
// Figure out frequencies and detune amounts from the mouse
|
||||
// using exponential scaling to approximate pitch perception
|
||||
float yoffset = (height - mouseY) / float(height);
|
||||
float frequency = pow(1000, yoffset) + 150;
|
||||
float detune = float(mouseX) / width - 0.5;
|
||||
myWave[0].generate(myStream); // Generate first sine, replace Stream
|
||||
myWave[0].phase += myStream.size; // Increment the phase
|
||||
myWave[0].phase %= myStream.sampleRate;
|
||||
for (int i = 1; i < myWave.length; i++) { // Add remaining sines into the Stream
|
||||
myWave[i].generate(myStream, Ess.ADD);
|
||||
myWave[i].phase = myWave[0].phase;
|
||||
}
|
||||
myFadeOut.filter(myStream); // Fade down the audio
|
||||
for (int i = 0; i < myWave.length; i++) { // Set the frequencies
|
||||
myWave[i].frequency = round(frequency * (i + 1 + i * detune));
|
||||
myWave[i].phase = 0;
|
||||
}
|
||||
myFadeIn.filter(myStream); // Fade up the audio
|
||||
}
|
||||
47
java/examples/Books/Processing Handbook/Extensions/Sound/_02/_02.pde
Executable file
@@ -0,0 +1,47 @@
|
||||
/**
|
||||
Sound is generated at setup with a triangle waveform and a simple envelope
|
||||
generator. Insert your own array of notes as 'rawSequence' and let it roll.
|
||||
*/
|
||||
import krister.Ess.*;
|
||||
AudioChannel myChannel; // Create channel
|
||||
TriangleWave myWave; // Create triangle waveform
|
||||
Envelope myEnvelope; // Create envelope
|
||||
int numNotes = 200; // Number of notes
|
||||
int noteDuration = 300; // Duration of each note in milliseconds
|
||||
float[] rawSequence = { 293.6648, 293.6648, 329.62756, 329.62756, 391.995, 369.99445 , 293.6648, 293.6648,
|
||||
329.62756, 293.6648, 439.997, 391.995, 293.6648, 293.6648, 587.3294, 493.8834,
|
||||
391.995, 369.9945, 329.62756, 523.2516, 523.2516, 493.8834, 391.995,
|
||||
439.997, 391.995 }; // Happy birthday
|
||||
|
||||
void setup() {
|
||||
size(100, 100);
|
||||
Ess.start(this); // Start Ess
|
||||
myChannel = new AudioChannel(); // Create a new AudioChannel
|
||||
myChannel.initChannel(myChannel.frames(rawSequence.length * noteDuration));
|
||||
int current = 0;
|
||||
myWave = new TriangleWave(480, 0.3); // Create triangle wave
|
||||
EPoint[] myEnv = new EPoint[3]; // Three-step breakpoint function
|
||||
myEnv[0] = new EPoint(0, 0); // Start at 0
|
||||
myEnv[1] = new EPoint(0.25, 1); // Attack
|
||||
myEnv[2] = new EPoint(2, 0); // Release
|
||||
myEnvelope = new Envelope(myEnv); // Bind Envelope to the breakpoint function
|
||||
int time = 0;
|
||||
for (int i = 0; i < rawSequence.length; i++) {
|
||||
myWave.frequency = rawSequence[current]; // Update waveform frequency
|
||||
int begin = myChannel.frames(time); // Starting position within Channel
|
||||
int e = int(noteDuration * 0.8);
|
||||
int end = myChannel.frames(e); // Ending position with Channel
|
||||
myWave.generate(myChannel, begin, end); // Render triangle wave
|
||||
myEnvelope.filter(myChannel, begin, end); // Apply envelope
|
||||
current++; // Move to next note
|
||||
time += noteDuration; // Increment the Channel output point
|
||||
}
|
||||
myChannel.play(); // Play the sound!
|
||||
}
|
||||
|
||||
void draw() { } // Empty draw() keeps the program running
|
||||
|
||||
public void stop() {
|
||||
Ess.stop(); // When program stops, stop Ess too
|
||||
super.stop();
|
||||
}
|
||||
88
java/examples/Books/Processing Handbook/Extensions/Sound/_03/_03.pde
Executable file
@@ -0,0 +1,88 @@
|
||||
/**
|
||||
Loads a sound file off disk and plays it in multiple voices at multiple sampling
|
||||
increments (demonstrating voice allocation), panning it back and forth between
|
||||
the speakers. Based on Ping Pong by Krister Olsson <http://tree-axis.com>
|
||||
*/
|
||||
import krister.Ess.*;
|
||||
|
||||
AudioChannel[] mySound = new AudioChannel[6]; // Six channels of audio playback
|
||||
Envelope myEnvelope; // Create Envelope
|
||||
boolean left = true;
|
||||
boolean middle = false;
|
||||
boolean right = false;
|
||||
// Sampling rates to choose from
|
||||
int[] rates = { 44100, 22050, 2943, 49500, 11025, 37083 };
|
||||
|
||||
void setup() {
|
||||
size(256, 200);
|
||||
stroke(255);
|
||||
Ess.start(this); // Start Ess
|
||||
// Load sounds and set initial panning
|
||||
// Sounds must be located in the sketch's "data" folder
|
||||
for (int i = 0; i < 6; i++) {
|
||||
mySound[i] = new AudioChannel("cela3.aif");
|
||||
mySound[i].smoothPan = true;
|
||||
mySound[i].pan(Ess.LEFT);
|
||||
mySound[i].panTo(1, 4000);
|
||||
}
|
||||
EPoint[] myEnv = new EPoint[3]; // Three-step breakpoint function
|
||||
myEnv[0] = new EPoint(0, 0); // Start at 0
|
||||
myEnv[1] = new EPoint(0.25, 1); // Attack
|
||||
myEnv[2] = new EPoint(2, 0); // Release
|
||||
myEnvelope = new Envelope(myEnv); // Bind an Envelope to the breakpoint function
|
||||
}
|
||||
|
||||
void draw() {
|
||||
int playSound = 0; // How many sounds do we play on this frame?
|
||||
int which = -1; // If so, on which voice?
|
||||
noStroke();
|
||||
fill(0, 15);
|
||||
rect(0, 0, width, height); // Fade background
|
||||
stroke(102);
|
||||
line(width / 2, 0, width / 2, height); // Center line
|
||||
float interp = lerp(0, width, (mySound[0].pan + 1) / 2.0);
|
||||
stroke(255);
|
||||
line(interp, 0, interp, height); // Moving line
|
||||
// Trigger 1-3 samples when the line passes the center line or hits an edge
|
||||
if ((mySound[0].pan < 0) && (middle == true)) {
|
||||
playSound = int(random(1, 3));
|
||||
middle = false;
|
||||
} else if ((mySound[0].pan > 0) && (middle == false)) {
|
||||
playSound = int(random(1, 3));
|
||||
middle = true;
|
||||
} else if ((mySound[0].pan < -0.9) && (left == true)) {
|
||||
playSound = int(random(1, 3));
|
||||
left = false;
|
||||
} else if ((mySound[0].pan > -0.9) && (left == false)) {
|
||||
left = true;
|
||||
} else if ((mySound[0].pan > 0.9) && (right == true)) {
|
||||
playSound = int(random(1, 3));
|
||||
right = false;
|
||||
} else if ((mySound[0].pan < 0.9) && (right == false)) {
|
||||
right = true;
|
||||
}
|
||||
// Voice allocation block, figure out which AudioChannels are free
|
||||
while (playSound > 0) {
|
||||
for (int i = 0; i < mySound.length; i++) {
|
||||
if (mySound[i].state == Ess.STOPPED) {
|
||||
which = i; // Find a free voice
|
||||
}
|
||||
}
|
||||
// If a voice is available and selected, play it
|
||||
if (which != -1) {
|
||||
mySound[which].sampleRate(rates[int(random(0,6))], false);
|
||||
mySound[which].play();
|
||||
myEnvelope.filter(mySound[which]); // Apply envelope
|
||||
}
|
||||
playSound--;
|
||||
}
|
||||
}
|
||||
|
||||
public void stop() {
|
||||
Ess.stop(); // When program stops, stop Ess too
|
||||
super.stop();
|
||||
}
|
||||
|
||||
void audioOutputPan(AudioOutput c) {
|
||||
c.panTo(-c.pan, 4000); // Reverse pan direction
|
||||
}
|
||||
BIN
java/examples/Books/Processing Handbook/Extensions/Sound/_03/data/cela3.aif
Executable file
51
java/examples/Books/Processing Handbook/Extensions/Sound/_04/_04.pde
Executable file
@@ -0,0 +1,51 @@
|
||||
/**
|
||||
Applies reverb 10 times to a succession of guitar chords.
|
||||
Inspired by Alvin Lucier's "I am Sitting in a Room."
|
||||
Based on Reverb by Krister Olsson <http://www.tree-axis.com>
|
||||
*/
|
||||
import krister.Ess.*;
|
||||
|
||||
AudioChannel myChannel;
|
||||
Reverb myReverb;
|
||||
Normalize myNormalize;
|
||||
int numRepeats = 9;
|
||||
int repeats = 0;
|
||||
float rectWidth;
|
||||
|
||||
void setup() {
|
||||
size(256, 200);
|
||||
noStroke();
|
||||
background(0);
|
||||
rectWidth = width / (numRepeats + 1.0);
|
||||
Ess.start(this); // Start Ess
|
||||
// Load audio file into a AudioChannel, file must be in the sketch's "data" folder
|
||||
myChannel = new AudioChannel("guitar.aif");
|
||||
myReverb = new Reverb();
|
||||
myNormalize = new Normalize();
|
||||
myNormalize.filter(myChannel); // Normalize the audio
|
||||
myChannel.play(1);
|
||||
}
|
||||
|
||||
void draw() {
|
||||
if (repeats < numRepeats) {
|
||||
if (myChannel.state == Ess.STOPPED) { // If the audio isn't playing
|
||||
myChannel.adjustChannel(myChannel.size / 16, Ess.END);
|
||||
myChannel.out(myChannel.size);
|
||||
// Apply reverberation "in place" to the audio in the channel
|
||||
myReverb.filter(myChannel);
|
||||
// Normalize the signal
|
||||
myNormalize.filter(myChannel);
|
||||
myChannel.play(1);
|
||||
repeats++;
|
||||
}
|
||||
} else {
|
||||
exit(); // Quit the program
|
||||
}
|
||||
// Draw rectangle to show the current repeat (1 of 9)
|
||||
rect(rectWidth * repeats, 0, rectWidth - 1, height);
|
||||
}
|
||||
|
||||
public void stop() {
|
||||
Ess.stop(); // When program stops, stop Ess too
|
||||
super.stop();
|
||||
}
|
||||
BIN
java/examples/Books/Processing Handbook/Extensions/Sound/_04/data/guitar.aif
Executable file
43
java/examples/Books/Processing Handbook/Extensions/Sound/_05/_05.pde
Executable file
@@ -0,0 +1,43 @@
|
||||
/**
|
||||
Analyzes a sound file using a Fast Fourier Transform, and plots both the current
|
||||
spectral frame and a "peak-hold" plot of the maximum over time using logarithmic
|
||||
scaling. Based on examples by Krister Olsson <http://tree-axis.com>
|
||||
*/
|
||||
import krister.Ess.*;
|
||||
|
||||
AudioChannel myChannel;
|
||||
FFT myFFT;
|
||||
int bands = 256; // Number of FFT frequency bands to calculate
|
||||
|
||||
void setup() {
|
||||
size(1024, 200);
|
||||
Ess.start(this); // Start Ess
|
||||
// Load "test.aif" into a new AudioChannel, file must be in the "data" folder
|
||||
myChannel = new AudioChannel("test.aif");
|
||||
myChannel.play(Ess.FOREVER);
|
||||
myFFT = new FFT(bands * 2); // We want 256 frequency bands, so we pass in 512
|
||||
}
|
||||
|
||||
void draw() {
|
||||
background(176);
|
||||
// Get spectrum
|
||||
myFFT.getSpectrum(myChannel);
|
||||
// Draw FFT data
|
||||
stroke(255);
|
||||
for (int i = 0; i < bands; i++) {
|
||||
float x = width - pow(1024, (255.0 - i) / bands);
|
||||
float maxY = max(0, myFFT.maxSpectrum[i] * height * 2);
|
||||
float freY = max(0, myFFT.spectrum[i] * height * 2);
|
||||
// Draw maximum lines
|
||||
stroke(255);
|
||||
line(x, height, x, height - maxY);
|
||||
// Draw frequency lines
|
||||
stroke(0);
|
||||
line(x, height, x, height - freY);
|
||||
}
|
||||
}
|
||||
|
||||
public void stop() {
|
||||
Ess.stop(); // When program stops, stop Ess too
|
||||
super.stop();
|
||||
}
|
||||
BIN
java/examples/Books/Processing Handbook/Extensions/Sound/_05/data/test.aif
Executable file
54
java/examples/Books/Processing Handbook/Extensions/Vision/_01/_01.pde
Executable file
@@ -0,0 +1,54 @@
|
||||
// Quantify the amount of movement in the video frame using frame-differencing
|
||||
import processing.video.*;
|
||||
|
||||
int numPixels;
|
||||
int[] previousFrame;
|
||||
Capture video;
|
||||
void setup() {
|
||||
size(640, 480); // Change size to 320 x 240 if too slow at 640 x 480
|
||||
video = new Capture(this, width, height, 24);
|
||||
numPixels = video.width * video.height;
|
||||
// Create an array to store the previously captured frame
|
||||
previousFrame = new int[numPixels];
|
||||
}
|
||||
|
||||
void draw() {
|
||||
if (video.available()) {
|
||||
// When using video to manipulate the screen, use video.available() and
|
||||
// video.read() inside the draw() method so that it's safe to draw to the screen
|
||||
video.read(); // Read the new frame from the camera
|
||||
video.loadPixels(); // Make its pixels[] array available
|
||||
int movementSum = 0; // Amount of movement in the frame
|
||||
loadPixels();
|
||||
for (int i = 0; i < numPixels; i++) { // For each pixel in the video frame...
|
||||
color currColor = video.pixels[i];
|
||||
color prevColor = previousFrame[i];
|
||||
// Extract the red, green, and blue components from current pixel
|
||||
int currR = (currColor >> 16) & 0xFF; // Like red(), but faster (see p. 673)
|
||||
int currG = (currColor >> 8) & 0xFF;
|
||||
int currB = currColor & 0xFF;
|
||||
// Extract red, green, and blue components from previous pixel
|
||||
int prevR = (prevColor >> 16) & 0xFF;
|
||||
int prevG = (prevColor >> 8) & 0xFF;
|
||||
int prevB = prevColor & 0xFF;
|
||||
// Compute the difference of the red, green, and blue values
|
||||
int diffR = abs(currR - prevR);
|
||||
int diffG = abs(currG - prevG);
|
||||
int diffB = abs(currB - prevB);
|
||||
// Add these differences to the running tally
|
||||
movementSum += diffR + diffG + diffB;
|
||||
// Render the difference image to the screen
|
||||
pixels[i] = color(diffR, diffG, diffB);
|
||||
// The following line is much faster, but more confusing to read
|
||||
//pixels[i] = 0xff000000 | (diffR << 16) | (diffG << 8) | diffB;
|
||||
// Save the current color into the 'previous' buffer
|
||||
previousFrame[i] = currColor;
|
||||
}
|
||||
// To prevent flicker from frames that are all black (no movement),
|
||||
// only update the screen if the image has changed.
|
||||
if (movementSum > 0) {
|
||||
updatePixels();
|
||||
println(movementSum); // Print the total amount of movement to the console
|
||||
}
|
||||
}
|
||||
}
|
||||
59
java/examples/Books/Processing Handbook/Extensions/Vision/_02/_02.pde
Executable file
@@ -0,0 +1,59 @@
|
||||
// Detect the presence of people and objects in the frame using a simple
|
||||
// background-subtraction technique. To initialize the background, press a key.
|
||||
import processing.video.*;
|
||||
|
||||
int numPixels;
|
||||
int[] backgroundPixels;
|
||||
Capture video;
|
||||
|
||||
void setup() {
|
||||
size(640, 480); // Change size to 320 x 240 if too slow at 640 x 480
|
||||
video = new Capture(this, width, height, 24);
|
||||
numPixels = video.width * video.height;
|
||||
// Create array to store the background image
|
||||
backgroundPixels = new int[numPixels];
|
||||
// Make the pixels[] array available for direct manipulation
|
||||
loadPixels();
|
||||
}
|
||||
|
||||
void draw() {
|
||||
if (video.available()) {
|
||||
video.read(); // Read a new video frame
|
||||
video.loadPixels(); // Make the pixels of video available
|
||||
// Difference between the current frame and the stored background
|
||||
int presenceSum = 0;
|
||||
for (int i = 0; i < numPixels; i++) { // For each pixel in the video frame...
|
||||
// Fetch the current color in that location, and also the color
|
||||
// of the background in that spot
|
||||
color currColor = video.pixels[i];
|
||||
color bkgdColor = backgroundPixels[i];
|
||||
// Extract the red, green, and blue components of the current pixelÕs color
|
||||
int currR = (currColor >> 16) & 0xFF;
|
||||
int currG = (currColor >> 8) & 0xFF;
|
||||
int currB = currColor & 0xFF;
|
||||
// Extract the red, green, and blue components of the background pixelÕs color
|
||||
int bkgdR = (bkgdColor >> 16) & 0xFF;
|
||||
int bkgdG = (bkgdColor >> 8) & 0xFF;
|
||||
int bkgdB = bkgdColor & 0xFF;
|
||||
// Compute the difference of the red, green, and blue values
|
||||
int diffR = abs(currR - bkgdR);
|
||||
int diffG = abs(currG - bkgdG);
|
||||
int diffB = abs(currB - bkgdB);
|
||||
// Add these differences to the running tally
|
||||
presenceSum += diffR + diffG + diffB;
|
||||
// Render the difference image to the screen
|
||||
pixels[i] = color(diffR, diffG, diffB);
|
||||
// The following line does the same thing much faster, but is more technical
|
||||
//pixels[i] = 0xFF000000 | (diffR << 16) | (diffG << 8) | diffB;
|
||||
}
|
||||
updatePixels(); // Notify that the pixels[] array has changed
|
||||
println(presenceSum); // Print out the total amount of movement
|
||||
}
|
||||
}
|
||||
|
||||
// When a key is pressed, capture the background image into the backgroundPixels
|
||||
// buffer, by copying each of the current frameÕs pixels into it.
|
||||
void keyPressed() {
|
||||
video.loadPixels();
|
||||
arraycopy(video.pixels, backgroundPixels);
|
||||
}
|
||||
49
java/examples/Books/Processing Handbook/Extensions/Vision/_03/_03.pde
Executable file
@@ -0,0 +1,49 @@
|
||||
// Determines whether a test location (such as the cursor) is contained within
|
||||
// the silhouette of a dark object
|
||||
import processing.video.*;
|
||||
|
||||
color black = color(0);
|
||||
color white = color(255);
|
||||
int numPixels;
|
||||
Capture video;
|
||||
|
||||
void setup() {
|
||||
size(640, 480); // Change size to 320 x 240 if too slow at 640 x 480
|
||||
strokeWeight(5);
|
||||
video = new Capture(this, width, height, 24);
|
||||
numPixels = video.width * video.height;
|
||||
noCursor();
|
||||
smooth();
|
||||
}
|
||||
|
||||
void draw() {
|
||||
if (video.available()) {
|
||||
video.read();
|
||||
video.loadPixels();
|
||||
int threshold = 127; // Set the threshold value
|
||||
float pixelBrightness; // Declare variable to store a pixel's color
|
||||
// Turn each pixel in the video frame black or white depending on its brightness
|
||||
loadPixels();
|
||||
for (int i = 0; i < numPixels; i++) {
|
||||
pixelBrightness = brightness(video.pixels[i]);
|
||||
if (pixelBrightness > threshold) { // If the pixel is brighter than the
|
||||
pixels[i] = white; // threshold value, make it white
|
||||
}
|
||||
else { // Otherwise,
|
||||
pixels[i] = black; // make it black
|
||||
}
|
||||
}
|
||||
updatePixels();
|
||||
// Test a location to see where it is contained. Fetch the pixel at the test
|
||||
// location (the cursor), and compute its brightness
|
||||
int testValue = get(mouseX, mouseY);
|
||||
float testBrightness = brightness(testValue);
|
||||
if (testBrightness > threshold) { // If the test location is brighter than
|
||||
fill(black); // the threshold set the fill to black
|
||||
}
|
||||
else { // Otherwise,
|
||||
fill(white); // set the fill to white
|
||||
}
|
||||
ellipse(mouseX, mouseY, 20, 20);
|
||||
}
|
||||
}
|
||||
44
java/examples/Books/Processing Handbook/Extensions/Vision/_04/_04.pde
Executable file
@@ -0,0 +1,44 @@
|
||||
// Tracks the brightest pixel in a live video signal
|
||||
import processing.video.*;
|
||||
|
||||
Capture video;
|
||||
|
||||
void setup() {
|
||||
size(640, 480); // Change size to 320 x 240 if too slow at 640 x 480
|
||||
video = new Capture(this, width, height, 30);
|
||||
noStroke();
|
||||
smooth();
|
||||
}
|
||||
|
||||
void draw() {
|
||||
if (video.available()) {
|
||||
video.read();
|
||||
image(video, 0, 0, width, height); // Draw the webcam video onto the screen
|
||||
int brightestX = 0; // X-coordinate of the brightest video pixel
|
||||
int brightestY = 0; // Y-coordinate of the brightest video pixel
|
||||
float brightestValue = 0; // Brightness of the brightest video pixel
|
||||
// Search for the brightest pixel: For each row of pixels in the video image and
|
||||
// for each pixel in the yth row, compute each pixel's index in the video
|
||||
video.loadPixels();
|
||||
int index = 0;
|
||||
for (int y = 0; y < video.height; y++) {
|
||||
for (int x = 0; x < video.width; x++) {
|
||||
// Get the color stored in the pixel
|
||||
int pixelValue = video.pixels[index];
|
||||
// Determine the brightness of the pixel
|
||||
float pixelBrightness = brightness(pixelValue);
|
||||
// If that value is brighter than any previous, then store the
|
||||
// brightness of that pixel, as well as its (x,y) location
|
||||
if (pixelBrightness > brightestValue) {
|
||||
brightestValue = pixelBrightness;
|
||||
brightestY = y;
|
||||
brightestX = x;
|
||||
}
|
||||
index++;
|
||||
}
|
||||
}
|
||||
// Draw a large, yellow circle at the brightest pixel
|
||||
fill(255, 204, 0, 128);
|
||||
ellipse(brightestX, brightestY, 200, 200);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
|
||||
// Based on code from Shape 1 (p. 23) and code 6-07 (p. 65)
|
||||
|
||||
|
||||
size(713, 938);
|
||||
//size(713, 938, PDF, "page_022.pdf");
|
||||
background(255);
|
||||
int ydiv = height/54;
|
||||
|
||||
strokeWeight(0.25);
|
||||
noStroke();
|
||||
fill(0);
|
||||
rectMode(CENTER);
|
||||
|
||||
for (int y = 0; y < height; y += ydiv) {
|
||||
for (int x = 0; x < width; x += ydiv) {
|
||||
ellipse(x, y, 0.5, 0.5);
|
||||
}
|
||||
}
|
||||
|
||||
float s = ydiv * 10;
|
||||
float x = ydiv * 28;
|
||||
|
||||
ellipse(x, ydiv * 12, s, s);
|
||||
rect(x, ydiv*26, ydiv*12, ydiv*12);
|
||||
fill(255);
|
||||
ellipse(x, ydiv*26, s, s);
|
||||
|
||||
fill(0);
|
||||
ellipse(x, ydiv*40, s*1.07, s*1.07);
|
||||
fill(255);
|
||||
rect(x, ydiv*40, s*.7, s*.7);
|
||||
@@ -0,0 +1,35 @@
|
||||
|
||||
// Based on code 4-02 (p. 44) and code 6-01 (p. 63)
|
||||
|
||||
|
||||
size(442, 550);
|
||||
//size(442, 550, PDF, "page_042.pdf");
|
||||
background(255);
|
||||
strokeWeight(0.25);
|
||||
strokeCap(SQUARE);
|
||||
|
||||
float x1 = 0;
|
||||
float x2 = 0;
|
||||
float x3 = 0;
|
||||
float x5 = 0;
|
||||
float x4 = 0;
|
||||
|
||||
for(int i = 0; i <= 50; i++) {
|
||||
|
||||
line(width-x2, height * 0.0, width-x2, height * 0.2);
|
||||
x2 += 4.5;
|
||||
|
||||
line(width-x1, height * 0.2, width-x1, height * 0.4);
|
||||
x1 += 9;
|
||||
|
||||
line(width-x3, height * 0.4, width-x3, height * 0.6);
|
||||
x3 += 6.25;
|
||||
|
||||
line(width-x5, height * 0.6, width-x5, height * 0.8);
|
||||
x5 += 3.125;
|
||||
|
||||
line(width-x4, height * 0.8, width-x4, height);
|
||||
x4 += 5.375;
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,21 @@
|
||||
|
||||
// Based on the code examples on page 66
|
||||
|
||||
|
||||
size(180, 666);
|
||||
//size(180, 666, PDF, "page_060.pdf");
|
||||
background(255);
|
||||
strokeWeight(0.25);
|
||||
strokeCap(SQUARE);
|
||||
|
||||
for (int y = 0; y <= height; y += 5) {
|
||||
for (int x = 0; x <= width; x += 5) {
|
||||
if (x % 20 == 0) {
|
||||
line(x, y, x-3, y-3);
|
||||
}
|
||||
else {
|
||||
line(x, y, x-3, y+3);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,28 @@
|
||||
|
||||
// Based on code 8-08 (p. 83)
|
||||
|
||||
|
||||
size(360, 550);
|
||||
//size(360, 550, PDF, "page_078.pdf");
|
||||
background(255);
|
||||
stroke(0);
|
||||
noFill();
|
||||
|
||||
float ax = 0.0;
|
||||
float ay = 0.0;
|
||||
|
||||
for (float e = 2; e < 36; e += 1.0) {
|
||||
for (int x = 0; x < width; x += 2) {
|
||||
float n = norm(x, 0.0, width); // Range 0.0 to 1.0
|
||||
float y = 1 - pow(n, e); // Calculate curve
|
||||
y *= height; // Range 0.0 to height
|
||||
if (x > 0) {
|
||||
line(ax, ay, x, y);
|
||||
}
|
||||
ax = x;
|
||||
ay = y;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,21 @@
|
||||
|
||||
// Based on code 10-07 (p. 98) and code 35-03 (p. 322)
|
||||
|
||||
|
||||
size(2400, 1200);
|
||||
background(255);
|
||||
noStroke();
|
||||
fill(0);
|
||||
|
||||
PImage img = loadImage("rockies-color.jpg");
|
||||
|
||||
for(int i = 0; i < width; i += 50) {
|
||||
int x = int(random(img.width-50));
|
||||
PImage crop = img.get(x, 0, 50, height);
|
||||
image(crop, i, 0);
|
||||
}
|
||||
|
||||
//saveFrame("page_094.tif");
|
||||
|
||||
|
||||
|
||||
45
java/examples/Books/Processing Handbook/Illustrations/page_100/page_100.pde
Executable file
@@ -0,0 +1,45 @@
|
||||
|
||||
// Based on code 11-04 (p. 103) and code 13-01 (p. 113)
|
||||
|
||||
|
||||
//size(360, 666, PDF, "page_100.pdf");
|
||||
size(360, 666);
|
||||
background(255);
|
||||
|
||||
println(PFont.list()); // List the available fonts
|
||||
String s = "Ziggurat-HTF-Black";
|
||||
PFont font = createFont(s, 34);
|
||||
textFont(font);
|
||||
|
||||
fill(204);
|
||||
|
||||
int a = 37;
|
||||
|
||||
for (int i = 20; i < height+40; i += 38) {
|
||||
textSize(34);
|
||||
text(char(a), 50, i);
|
||||
textSize(10);
|
||||
text(a, 10, i-9);
|
||||
a++;
|
||||
}
|
||||
|
||||
for (int i = 20; i < height+40; i += 38) {
|
||||
textSize(34);
|
||||
text(char(a), 170, i);
|
||||
textSize(10);
|
||||
text(a, 130, i-9);
|
||||
a++;
|
||||
}
|
||||
|
||||
for (int i = 20; i < height+40; i += 38) {
|
||||
textSize(34);
|
||||
text(char(a), 290, i);
|
||||
textSize(10);
|
||||
text(a, 250, i-9);
|
||||
a++;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
32
java/examples/Books/Processing Handbook/Illustrations/page_110/page_110.pde
Executable file
@@ -0,0 +1,32 @@
|
||||
|
||||
// Based on code 13-05 (p. 113)
|
||||
|
||||
|
||||
size(360, 550);
|
||||
//size(360, 550, PDF, "page_110.pdf");
|
||||
background(255);
|
||||
|
||||
println(PFont.list()); // List the available fonts
|
||||
String s = "Ziggurat-HTF-Black";
|
||||
PFont font = createFont(s, 34);
|
||||
//PFont font = createFont("ZigguratHTFBlack", 34);
|
||||
textFont(font);
|
||||
|
||||
textAlign(CENTER);
|
||||
|
||||
fill(0, 50); // Black with low opacity
|
||||
|
||||
textSize(460);
|
||||
|
||||
for(int i=0; i<6; i++) {
|
||||
text(i, width/2, height*0.98 - i*40);
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
text("1", width/2, height*.9);
|
||||
text("2", width/2, height*.9);
|
||||
text("3", width/2, height*.9);
|
||||
text("4", width/2, height*.9);
|
||||
text("5", width/2, height*.9);
|
||||
*/
|
||||
21
java/examples/Books/Processing Handbook/Illustrations/page_126/page_126.pde
Executable file
@@ -0,0 +1,21 @@
|
||||
|
||||
// Based on code 15-09 (p. 131)
|
||||
|
||||
|
||||
size(750, 2775);
|
||||
|
||||
float xnoise = 0.0;
|
||||
float ynoise = 0.0;
|
||||
float inc = 0.005;
|
||||
for (int y = 0; y < height; y++) {
|
||||
for (int x = 0; x < width; x++) {
|
||||
float gray = noise(xnoise, ynoise) * 255;
|
||||
stroke(gray);
|
||||
point(x, y);
|
||||
xnoise = xnoise + inc;
|
||||
}
|
||||
xnoise = 0;
|
||||
ynoise = ynoise + inc;
|
||||
}
|
||||
|
||||
//saveFrame("page_126.tif");
|
||||
17
java/examples/Books/Processing Handbook/Illustrations/page_136/page_136.pde
Executable file
@@ -0,0 +1,17 @@
|
||||
|
||||
// Based on code 17-14 (p. 142)
|
||||
|
||||
|
||||
size(360, 550);
|
||||
//size(360, 550, PDF, "page_136.pdf");
|
||||
background(255);
|
||||
|
||||
smooth();
|
||||
noStroke();
|
||||
fill(0, 10);
|
||||
|
||||
translate(33, 66); // Set initial offset
|
||||
for (int i = 0; i < 45; i++) { // 12 repetitions
|
||||
scale(1.02); // Accumulate the scaling
|
||||
ellipse(25, 90, 200, 200);
|
||||
}
|
||||
23
java/examples/Books/Processing Handbook/Illustrations/page_144/page_144.pde
Executable file
@@ -0,0 +1,23 @@
|
||||
|
||||
// Based on code 14-11 (p. 123)
|
||||
|
||||
|
||||
size(440, 666);
|
||||
//size(440, 666, PDF, "page_144.pdf");
|
||||
background(255);
|
||||
stroke(0);
|
||||
strokeWeight(0.25);
|
||||
fill(0);
|
||||
|
||||
float scaleVal = 16.0;
|
||||
float angleInc = PI/24.0;
|
||||
float angle = 0.0;
|
||||
|
||||
for (int offset = 20; offset < width-20; offset += 5) {
|
||||
for (int y = 0; y <= height; y += 2) {
|
||||
float x = offset + (sin(angle) * scaleVal);
|
||||
line(x, y, x+0.125, y+0.125);
|
||||
angle += angleInc;
|
||||
}
|
||||
angle += PI;
|
||||
}
|
||||
BIN
java/examples/Books/Processing Handbook/Illustrations/page_148/data/Thumbs.db
Executable file
304
java/examples/Books/Processing Handbook/Illustrations/page_148/page_148.pde
Executable file
@@ -0,0 +1,304 @@
|
||||
|
||||
// Based on the Collage Engine code on page 150
|
||||
|
||||
|
||||
PImage nyt01 = loadImage("nyt_01.jpg");
|
||||
PImage nyt02 = loadImage("nyt_02.jpg");
|
||||
PImage nyt03 = loadImage("nyt_03.jpg");
|
||||
PImage nyt04 = loadImage("nyt_04.jpg");
|
||||
PImage nyt05 = loadImage("nyt_05.jpg");
|
||||
PImage nyt06 = loadImage("nyt_06.jpg");
|
||||
PImage nyt07 = loadImage("nyt_07.jpg");
|
||||
PImage nyt08 = loadImage("nyt_08.jpg");
|
||||
PImage nyt09 = loadImage("nyt_09.jpg");
|
||||
PImage nyt10 = loadImage("nyt_10.jpg");
|
||||
PImage nyt11 = loadImage("nyt_11.jpg");
|
||||
PImage nyt12 = loadImage("nyt_12.jpg");
|
||||
PImage nyt13 = loadImage("nyt_13.jpg");
|
||||
PImage nyt14 = loadImage("nyt_14.jpg");
|
||||
PImage nyt15 = loadImage("nyt_15.jpg");
|
||||
PImage nyt16 = loadImage("nyt_16.jpg");
|
||||
PImage nyt17 = loadImage("nyt_17.jpg");
|
||||
PImage nyt18 = loadImage("nyt_18.jpg");
|
||||
PImage nyt19 = loadImage("nyt_19.jpg");
|
||||
PImage nyt20 = loadImage("nyt_20.jpg");
|
||||
PImage nyt21 = loadImage("nyt_21.jpg");
|
||||
PImage nyt22 = loadImage("nyt_22.jpg");
|
||||
PImage nyt23 = loadImage("nyt_23.jpg");
|
||||
PImage nyt24 = loadImage("nyt_24.jpg");
|
||||
PImage nyt25 = loadImage("nyt_25.jpg");
|
||||
PImage nyt26 = loadImage("nyt_26.jpg");
|
||||
PImage nyt27 = loadImage("nyt_27.jpg");
|
||||
PImage nyt28 = loadImage("nyt_28.jpg");
|
||||
PImage nyt29 = loadImage("nyt_29.jpg");
|
||||
|
||||
float x, y, r;
|
||||
|
||||
size(750, 2275);
|
||||
smooth();
|
||||
background(255);
|
||||
tint(255, 204);
|
||||
|
||||
x = random(width);
|
||||
y = random(height);
|
||||
r = random(0, TWO_PI);
|
||||
pushMatrix();
|
||||
translate(x, y);
|
||||
rotate(r);
|
||||
image(nyt01, -nyt01.width/2, -nyt01.height/2);
|
||||
popMatrix();
|
||||
|
||||
x = random(width);
|
||||
y = random(height);
|
||||
r = random(0, TWO_PI);
|
||||
pushMatrix();
|
||||
translate(x, y);
|
||||
rotate(r);
|
||||
image(nyt02, -nyt02.width/2, -nyt02.height/2);
|
||||
popMatrix();
|
||||
|
||||
x = random(width);
|
||||
y = random(height);
|
||||
r = random(0, TWO_PI);
|
||||
pushMatrix();
|
||||
translate(x, y);
|
||||
rotate(r);
|
||||
image(nyt03, -nyt03.width/2, -nyt03.height/2);
|
||||
popMatrix();
|
||||
|
||||
x = random(width);
|
||||
y = random(height);
|
||||
r = random(0, TWO_PI);
|
||||
pushMatrix();
|
||||
translate(x, y);
|
||||
rotate(r);
|
||||
image(nyt04, -nyt04.width/2, -nyt04.height/2);
|
||||
popMatrix();
|
||||
|
||||
x = random(width);
|
||||
y = random(height);
|
||||
r = random(0, TWO_PI);
|
||||
pushMatrix();
|
||||
translate(x, y);
|
||||
rotate(r);
|
||||
image(nyt05, -nyt05.width/2, -nyt05.height/2);
|
||||
popMatrix();
|
||||
|
||||
x = random(width);
|
||||
y = random(height);
|
||||
r = random(0, TWO_PI);
|
||||
pushMatrix();
|
||||
translate(x, y);
|
||||
rotate(r);
|
||||
image(nyt06, -nyt06.width/2, -nyt06.height/2);
|
||||
popMatrix();
|
||||
|
||||
x = random(width);
|
||||
y = random(height);
|
||||
r = random(0, TWO_PI);
|
||||
pushMatrix();
|
||||
translate(x, y);
|
||||
rotate(r);
|
||||
image(nyt07, -nyt07.width/2, -nyt07.height/2);
|
||||
popMatrix();
|
||||
|
||||
x = random(width);
|
||||
y = random(height);
|
||||
r = random(0, TWO_PI);
|
||||
pushMatrix();
|
||||
translate(x, y);
|
||||
rotate(r);
|
||||
image(nyt08, -nyt08.width/2, -nyt08.height/2);
|
||||
popMatrix();
|
||||
|
||||
x = random(width);
|
||||
y = random(height);
|
||||
r = random(0, TWO_PI);
|
||||
pushMatrix();
|
||||
translate(x, y);
|
||||
rotate(r);
|
||||
image(nyt09, -nyt09.width/2, -nyt09.height/2);
|
||||
popMatrix();
|
||||
|
||||
x = random(width);
|
||||
y = random(height);
|
||||
r = random(0, TWO_PI);
|
||||
pushMatrix();
|
||||
translate(x, y);
|
||||
rotate(r);
|
||||
image(nyt10, -nyt10.width/2, -nyt10.height/2);
|
||||
popMatrix();
|
||||
|
||||
x = random(width);
|
||||
y = random(height);
|
||||
r = random(0, TWO_PI);
|
||||
pushMatrix();
|
||||
translate(x, y);
|
||||
rotate(r);
|
||||
image(nyt11, -nyt11.width/2, -nyt11.height/2);
|
||||
popMatrix();
|
||||
|
||||
x = random(width);
|
||||
y = random(height);
|
||||
r = random(0, TWO_PI);
|
||||
pushMatrix();
|
||||
translate(x, y);
|
||||
rotate(r);
|
||||
image(nyt12, -nyt12.width/2, -nyt12.height/2);
|
||||
popMatrix();
|
||||
|
||||
x = random(width);
|
||||
y = random(height);
|
||||
r = random(0, TWO_PI);
|
||||
pushMatrix();
|
||||
translate(x, y);
|
||||
rotate(r);
|
||||
image(nyt13, -nyt13.width/2, -nyt13.height/2);
|
||||
popMatrix();
|
||||
|
||||
x = random(width);
|
||||
y = random(height);
|
||||
r = random(0, TWO_PI);
|
||||
pushMatrix();
|
||||
translate(x, y);
|
||||
rotate(r);
|
||||
image(nyt14, -nyt14.width/2, -nyt14.height/2);
|
||||
popMatrix();
|
||||
|
||||
x = random(width);
|
||||
y = random(height);
|
||||
r = random(0, TWO_PI);
|
||||
pushMatrix();
|
||||
translate(x, y);
|
||||
rotate(r);
|
||||
image(nyt15, -nyt15.width/2, -nyt15.height/2);
|
||||
popMatrix();
|
||||
|
||||
x = random(width);
|
||||
y = random(height);
|
||||
r = random(0, TWO_PI);
|
||||
pushMatrix();
|
||||
translate(x, y);
|
||||
rotate(r);
|
||||
image(nyt16, -nyt16.width/2, -nyt16.height/2);
|
||||
popMatrix();
|
||||
|
||||
x = random(width);
|
||||
y = random(height);
|
||||
r = random(0, TWO_PI);
|
||||
pushMatrix();
|
||||
translate(x, y);
|
||||
rotate(r);
|
||||
image(nyt17, -nyt17.width/2, -nyt17.height/2);
|
||||
popMatrix();
|
||||
|
||||
x = random(width);
|
||||
y = random(height);
|
||||
r = random(0, TWO_PI);
|
||||
pushMatrix();
|
||||
translate(x, y);
|
||||
rotate(r);
|
||||
image(nyt18, -nyt18.width/2, -nyt18.height/2);
|
||||
popMatrix();
|
||||
|
||||
x = random(width);
|
||||
y = random(height);
|
||||
r = random(0, TWO_PI);
|
||||
pushMatrix();
|
||||
translate(x, y);
|
||||
rotate(r);
|
||||
image(nyt19, -nyt19.width/2, -nyt19.height/2);
|
||||
popMatrix();
|
||||
|
||||
x = random(width);
|
||||
y = random(height);
|
||||
r = random(0, TWO_PI);
|
||||
pushMatrix();
|
||||
translate(x, y);
|
||||
rotate(r);
|
||||
image(nyt20, -nyt20.width/2, -nyt20.height/2);
|
||||
popMatrix();
|
||||
|
||||
x = random(width);
|
||||
y = random(height);
|
||||
r = random(0, TWO_PI);
|
||||
pushMatrix();
|
||||
translate(x, y);
|
||||
rotate(r);
|
||||
image(nyt21, -nyt21.width/2, -nyt21.height/2);
|
||||
popMatrix();
|
||||
|
||||
x = random(width);
|
||||
y = random(height);
|
||||
r = random(0, TWO_PI);
|
||||
pushMatrix();
|
||||
translate(x, y);
|
||||
rotate(r);
|
||||
image(nyt22, -nyt22.width/2, -nyt22.height/2);
|
||||
popMatrix();
|
||||
|
||||
x = random(width);
|
||||
y = random(height);
|
||||
r = random(0, TWO_PI);
|
||||
pushMatrix();
|
||||
translate(x, y);
|
||||
rotate(r);
|
||||
image(nyt23, -nyt23.width/2, -nyt23.height/2);
|
||||
popMatrix();
|
||||
|
||||
x = random(width);
|
||||
y = random(height);
|
||||
r = random(0, TWO_PI);
|
||||
pushMatrix();
|
||||
translate(x, y);
|
||||
rotate(r);
|
||||
image(nyt24, -nyt24.width/2, -nyt24.height/2);
|
||||
popMatrix();
|
||||
|
||||
x = random(width);
|
||||
y = random(height);
|
||||
r = random(0, TWO_PI);
|
||||
pushMatrix();
|
||||
translate(x, y);
|
||||
rotate(r);
|
||||
image(nyt25, -nyt25.width/2, -nyt25.height/2);
|
||||
popMatrix();
|
||||
|
||||
x = random(width);
|
||||
y = random(height);
|
||||
r = random(0, TWO_PI);
|
||||
pushMatrix();
|
||||
translate(x, y);
|
||||
rotate(r);
|
||||
image(nyt26, -nyt26.width/2, -nyt26.height/2);
|
||||
popMatrix();
|
||||
|
||||
x = random(width);
|
||||
y = random(height);
|
||||
r = random(0, TWO_PI);
|
||||
pushMatrix();
|
||||
translate(x, y);
|
||||
rotate(r);
|
||||
image(nyt27, -nyt27.width/2, -nyt27.height/2);
|
||||
popMatrix();
|
||||
|
||||
x = random(width);
|
||||
y = random(height);
|
||||
r = random(0, TWO_PI);
|
||||
pushMatrix();
|
||||
translate(x, y);
|
||||
rotate(r);
|
||||
image(nyt28, -nyt28.width/2, -nyt28.height/2);
|
||||
popMatrix();
|
||||
|
||||
x = random(width);
|
||||
y = random(height);
|
||||
r = random(0, TWO_PI);
|
||||
pushMatrix();
|
||||
translate(x, y);
|
||||
rotate(r);
|
||||
image(nyt29, -nyt29.width/2, -nyt29.height/2);
|
||||
popMatrix();
|
||||
|
||||
|
||||
//saveFrame("page-148--" + int(random(0, 1000)) + ".tif");
|
||||
48
java/examples/Books/Processing Handbook/Illustrations/page_172/page_172.pde
Executable file
@@ -0,0 +1,48 @@
|
||||
|
||||
// Based on code 21-14 (p. 192)
|
||||
|
||||
|
||||
void setup() {
|
||||
size(200, 666);
|
||||
//size(200, 666, PDF, "page_172.pdf");
|
||||
background(255);
|
||||
smooth();
|
||||
noStroke();
|
||||
noLoop();
|
||||
fill(100);
|
||||
randomSeed(0);
|
||||
strokeWeight(0.25);
|
||||
}
|
||||
|
||||
void draw() {
|
||||
for(int i=0; i<12; i++) {
|
||||
vine(10+int(random(width-20)), int(random(10, 50)), int(random(2, 6)));
|
||||
}
|
||||
//exit();
|
||||
}
|
||||
|
||||
void vine(int x, int numLeaves, int leafSize ) {
|
||||
stroke(0);
|
||||
line(x, 0, x, height);
|
||||
noStroke();
|
||||
int gap = (height)/numLeaves;
|
||||
int direction = 1;
|
||||
for (int i = 0; i < numLeaves; i++) {
|
||||
int r = int(random(gap));
|
||||
leaf( x, gap*i + r, leafSize, direction);
|
||||
direction = direction * -1;
|
||||
}
|
||||
}
|
||||
|
||||
void leaf(int x, int y, int size, int d) {
|
||||
pushMatrix();
|
||||
translate(x, y); // Move to position
|
||||
scale(size); // Scale to size
|
||||
beginShape(); // Draw the shape
|
||||
vertex(1.0*d, -0.7);
|
||||
bezierVertex(1.0*d, -0.7, 0.4*d, -1.0, 0.0, 0.0);
|
||||
bezierVertex(0.0, 0.0, 1.0*d, 0.4, 1.0*d, -0.7);
|
||||
endShape();
|
||||
popMatrix();
|
||||
}
|
||||
|
||||
25
java/examples/Books/Processing Handbook/Illustrations/page_216/page_216.pde
Executable file
@@ -0,0 +1,25 @@
|
||||
|
||||
// Based on code 24-08 (p. 221)
|
||||
|
||||
|
||||
PImage lineImage;
|
||||
int x;
|
||||
|
||||
void setup() {
|
||||
size(750/2, 2292/2);
|
||||
background(255);
|
||||
lineImage = loadImage("paris-line.jpg");
|
||||
}
|
||||
|
||||
void draw() {
|
||||
if(mousePressed) {
|
||||
x = mouseX;
|
||||
}
|
||||
|
||||
image(lineImage, x-lineImage.width/2, mouseY);
|
||||
image(lineImage, x-lineImage.width/2, height-mouseY);
|
||||
}
|
||||
|
||||
void keyPressed() {
|
||||
saveFrame("page-216-####.tif");
|
||||
}
|
||||
52
java/examples/Books/Processing Handbook/Illustrations/page_222/page_222.pde
Executable file
@@ -0,0 +1,52 @@
|
||||
|
||||
// Based on code 13-01 (p. 113) and code 33-11 (p. 305)
|
||||
|
||||
|
||||
size(460, 225);
|
||||
//size(460, 225, PDF, "page_222.pdf");
|
||||
background(255);
|
||||
fill(204);
|
||||
|
||||
println(PFont.list()); // Select a font from this list
|
||||
String s = "Ziggurat-HTF-Black";
|
||||
PFont font = createFont(s, 34);
|
||||
textFont(font);
|
||||
|
||||
char[] c1 = {'1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '-', '='};
|
||||
char[] c2 = {'Q', 'W', 'E', 'R', 'T', 'Y', 'U', 'I', 'O', 'P', '[', ']'};
|
||||
char[] c3 = {'A', 'S', 'D', 'F', 'G', 'H', 'J', 'K', 'L', ';'};
|
||||
char[] c4 = {'Z', 'X', 'C', 'V', 'B', 'N', 'M', ',', '.', '/'};
|
||||
|
||||
textAlign(CENTER);
|
||||
textSize(50);
|
||||
stroke(204);
|
||||
|
||||
strokeWeight(0.5);
|
||||
|
||||
for (int i = 0; i < c1.length; i++) {
|
||||
text(c1[i], 20+i*78, 50);
|
||||
if(i%2 != 0) {
|
||||
line(20+i*39, 40, 20+i*39, 20);
|
||||
}
|
||||
}
|
||||
|
||||
for (int i = 0; i < c2.length; i++) {
|
||||
text(c2[i], 46+i*78, 105);
|
||||
if(i%2 != 0) {
|
||||
line(46+i*39, 95, 46+i*39, 75);
|
||||
}
|
||||
}
|
||||
|
||||
for (int i = 0; i < c3.length; i++) {
|
||||
text(c3[i], 65+i*78, 160);
|
||||
if(i%2 != 0) {
|
||||
line(65+i*39, 150, 65+i*39, 130);
|
||||
}
|
||||
}
|
||||
|
||||
for (int i = 0; i < c4.length; i++) {
|
||||
text(c4[i], 90+i*78, 215);
|
||||
if(i%2 != 0) {
|
||||
line(90+i*39, 205, 90+i*39, 185);
|
||||
}
|
||||
}
|
||||
45
java/examples/Books/Processing Handbook/Illustrations/page_228/page_228.pde
Executable file
@@ -0,0 +1,45 @@
|
||||
|
||||
// Based on code 26-04 (p. 231)
|
||||
|
||||
|
||||
int dragX, dragY, moveX, moveY;
|
||||
boolean record = false;
|
||||
|
||||
void setup() {
|
||||
size(360, 666);
|
||||
}
|
||||
|
||||
void draw() {
|
||||
if (record) {
|
||||
beginRecord(PDF, "page_228.pdf");
|
||||
}
|
||||
|
||||
background(255);
|
||||
noFill();
|
||||
stroke(0);
|
||||
ellipse(dragX, dragY, 200, 200); // Black circle
|
||||
fill(153);
|
||||
noStroke();
|
||||
ellipse(moveX, moveY, 200, 200); // Gray circle
|
||||
|
||||
if (record) {
|
||||
endRecord();
|
||||
record = false;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void mouseMoved() { // Move gray circle
|
||||
moveX = mouseX;
|
||||
moveY = mouseY;
|
||||
}
|
||||
|
||||
void mouseDragged() { // Move black circle
|
||||
dragX = mouseX;
|
||||
dragY = mouseY;
|
||||
}
|
||||
|
||||
void keyReleased() {
|
||||
record = true;
|
||||
}
|
||||
|
||||
40
java/examples/Books/Processing Handbook/Illustrations/page_250/page_250.pde
Executable file
@@ -0,0 +1,40 @@
|
||||
|
||||
size(442, 500);
|
||||
//size(442, 500, PDF, "page_250.pdf");
|
||||
background(255);
|
||||
fill(0);
|
||||
|
||||
println(PFont.list());
|
||||
String s = "TheSansMono Light Italic";
|
||||
PFont font = createFont(s, 24);
|
||||
textFont(font);
|
||||
|
||||
String s1 = "void draw() {";
|
||||
String s2 = " background(126);";
|
||||
String s3 = " ellipse(mouseX, mouseY, 33, 33);";
|
||||
String s4 = "}";
|
||||
|
||||
String s5 = "void Draw() (";
|
||||
String s6 = " background(126)";
|
||||
String s7 = " ellipse(mouseX. mousey, 33, 33);";
|
||||
String s8 = "}";
|
||||
|
||||
textAlign(LEFT);
|
||||
textSize(26);
|
||||
stroke(204);
|
||||
|
||||
text(s5, 0, 30);
|
||||
text(s6, 0, 70);
|
||||
text(s7, 0, 110);
|
||||
text(s8, 0, 150);
|
||||
|
||||
text(s1, 0, 250);
|
||||
text(s2, 0, 290);
|
||||
text(s3, 0, 320);
|
||||
text(s4, 0, 370);
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
108
java/examples/Books/Processing Handbook/Illustrations/page_254/page_254.pde
Executable file
@@ -0,0 +1,108 @@
|
||||
|
||||
// Based on the Cursor example on page 257
|
||||
|
||||
|
||||
import processing.pdf.*;
|
||||
|
||||
boolean record = false;
|
||||
|
||||
int gx, gy;
|
||||
int mode, nextmode;
|
||||
int nummodes;
|
||||
boolean forapplet = false;
|
||||
|
||||
float mx, my, lastmx, lastmy;
|
||||
float lastrot, lastsc;
|
||||
|
||||
float bgx, bgy;
|
||||
|
||||
float p_x, p_y;
|
||||
float p_fx, p_fy;
|
||||
float p_v2, p_vx, p_vy;
|
||||
float p_a2, p_ax, p_ay;
|
||||
float p_mass, p_drag;
|
||||
|
||||
void setup() {
|
||||
size(442, 550);
|
||||
gx = width;
|
||||
gy = height;
|
||||
colorMode(RGB, 1.0);
|
||||
strokeWeight(0.5);
|
||||
strokeJoin(ROUND);
|
||||
|
||||
nummodes = 4;
|
||||
mode = 4;
|
||||
|
||||
bgx = 0;
|
||||
bgy = 0;
|
||||
mx = gx/2;
|
||||
my = gy/2;
|
||||
}
|
||||
|
||||
|
||||
void draw() {
|
||||
|
||||
if(record) {
|
||||
beginRecord(PDF, "page_254.pdf"); // Start writing to PDF
|
||||
}
|
||||
|
||||
lastmx = mx;
|
||||
lastmy = my;
|
||||
|
||||
mx = mouseX;
|
||||
my = mouseY;
|
||||
|
||||
background(1.0);
|
||||
|
||||
// Grid
|
||||
fill(1,1,1);
|
||||
stroke(.2,.2,.2);
|
||||
|
||||
float rot;
|
||||
for (int i=16; i<gx+2; i+=32) {
|
||||
for (int j=11; j<gy; j+=30) {
|
||||
rot = -PI/4. + atan2(j-my, i-mx);
|
||||
drawCursor(i, j, .5, rot);
|
||||
}
|
||||
}
|
||||
|
||||
if(record) {
|
||||
endRecord();
|
||||
record = false;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void drawCursor(float x, float y, float myscale, float myrot) {
|
||||
// Draw generic arrow cursor
|
||||
if (forapplet) y -= gy/2;
|
||||
pushMatrix();
|
||||
translate(x, y);
|
||||
rotate(myrot);
|
||||
scale(myscale, myscale);
|
||||
beginShape(POLYGON);
|
||||
vertex(7, 21);
|
||||
vertex(4, 13);
|
||||
vertex(1, 16);
|
||||
vertex(0, 16);
|
||||
vertex(0, 0); // Tip of cursor shape
|
||||
vertex(1, 0);
|
||||
vertex(12, 11);
|
||||
vertex(12, 12);
|
||||
vertex(7, 12);
|
||||
vertex(10, 20);
|
||||
vertex(9, 21);
|
||||
vertex(7, 21);
|
||||
endShape();
|
||||
popMatrix();
|
||||
}
|
||||
|
||||
|
||||
void keyPressed() {
|
||||
record = true;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
78
java/examples/Books/Processing Handbook/Illustrations/page_278/page_278.pde
Executable file
@@ -0,0 +1,78 @@
|
||||
|
||||
// Based on code 31-03 (p. 281)
|
||||
|
||||
|
||||
Spot[] sps = new Spot[36];
|
||||
|
||||
void setup() {
|
||||
size(750, 2775);
|
||||
//size(200, 200);
|
||||
smooth();
|
||||
noStroke();
|
||||
ellipseMode(CENTER_RADIUS);
|
||||
randomSeed(0);
|
||||
for(int i=0; i<sps.length; i++) {
|
||||
sps[i] = new Spot();
|
||||
}
|
||||
}
|
||||
|
||||
void draw() {
|
||||
fill(255, 12);
|
||||
rect(0, 0, width, height);
|
||||
fill(0);
|
||||
for(int i=0; i<sps.length; i++) {
|
||||
sps[i].updateDisplay();
|
||||
}
|
||||
}
|
||||
|
||||
void mousePressed() {
|
||||
save("page_278.tif");
|
||||
}
|
||||
|
||||
|
||||
class Spot {
|
||||
|
||||
float radius = 15.0; // Radius of the circle
|
||||
float x = random(radius, width-radius); // X-coordinate
|
||||
float y = random(radius, height-radius); // Y-coordinate
|
||||
float speedX = random(0.4, 1.0); // Speed of motion on the x-axis
|
||||
float speedY = random(0.4, 1.0); // Speed of motion on the y-axis
|
||||
|
||||
int directionX = 1; // Direction of motion on the x-axis
|
||||
int directionY = -1; // Direction of motion on the y-axis
|
||||
|
||||
Spot() {
|
||||
float r = random(1.0);
|
||||
if (r < .25) {
|
||||
directionX = 1;
|
||||
directionY = 1;
|
||||
}
|
||||
else if (r < .5) {
|
||||
directionX = -1;
|
||||
directionY = 1;
|
||||
}
|
||||
else if (r < .75) {
|
||||
directionX = 1;
|
||||
directionY = -1;
|
||||
}
|
||||
else {
|
||||
directionX = -1;
|
||||
directionY = -1;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void updateDisplay() {
|
||||
|
||||
ellipse(x, y, radius, radius);
|
||||
x += speedX * directionX;
|
||||
if ((x > width-radius) || (x < radius)) {
|
||||
directionX = -directionX; // Change direction
|
||||
}
|
||||
y += speedY * directionY;
|
||||
if ((y > height-radius) || (y < radius)) {
|
||||
directionY = -directionY; // Change direction
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
59
java/examples/Books/Processing Handbook/Illustrations/page_290/page_290.pde
Executable file
@@ -0,0 +1,59 @@
|
||||
|
||||
// Based on code 32-07 (p. 297)
|
||||
|
||||
|
||||
float inc = 0.0;
|
||||
|
||||
boolean record = false;
|
||||
|
||||
void setup() {
|
||||
size(513, 462);
|
||||
smooth();
|
||||
}
|
||||
|
||||
void draw() {
|
||||
|
||||
if(record) {
|
||||
beginRecord(PDF, "page_290.pdf");
|
||||
}
|
||||
|
||||
stroke(0, 143);
|
||||
randomSeed(0);
|
||||
background(255);
|
||||
inc += 0.01;
|
||||
float angle = sin(inc)/10.0 + sin(inc*1.2)/20.0;
|
||||
//tail(18, 9, angle/1.3);
|
||||
//tail(33, 12, angle);
|
||||
//tail(44, 10, angle/1.3);
|
||||
//tail(62, 5, angle);
|
||||
//tail(88, 7, angle*2);
|
||||
for(int i=-20; i<width+20; i+=20) {
|
||||
tail(i, 100, int(random(5, 12)), angle*random(1.0, 1.3));
|
||||
tail(i, 200, int(random(5, 12)), -angle*random(1.0, 1.3));
|
||||
tail(i, 300, int(random(5, 12)), angle*random(1.0, 1.3));
|
||||
tail(i, 400, int(random(5, 12)), -angle*random(1.0, 1.3));
|
||||
}
|
||||
|
||||
if(record) {
|
||||
endRecord();
|
||||
record = false;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void mousePressed() {
|
||||
record = true;
|
||||
}
|
||||
|
||||
|
||||
void tail(int x, int y, int units, float angle) {
|
||||
pushMatrix();
|
||||
translate(x, y);
|
||||
for (int i = units; i > 0; i--) {
|
||||
strokeWeight(i/2.0);
|
||||
line(0, 0, 0, -8);
|
||||
translate(0.0, -8);
|
||||
rotate(angle);
|
||||
}
|
||||
popMatrix();
|
||||
}
|
||||
75
java/examples/Books/Processing Handbook/Illustrations/page_300/page_300.pde
Executable file
@@ -0,0 +1,75 @@
|
||||
|
||||
// Based on code 33-14 (p. 307)
|
||||
|
||||
|
||||
float inc = 0.0;
|
||||
|
||||
boolean record = false;
|
||||
|
||||
int numLines = 170;
|
||||
float[] y;
|
||||
float[] x;
|
||||
|
||||
float mx;
|
||||
float my;
|
||||
|
||||
void setup() {
|
||||
size(180, 666);
|
||||
smooth();
|
||||
noFill();
|
||||
strokeWeight(0.25);
|
||||
y = new float[height];
|
||||
x = new float[height];
|
||||
|
||||
}
|
||||
|
||||
void draw() {
|
||||
|
||||
if(record) {
|
||||
beginRecord(PDF, "page_300.pdf");
|
||||
}
|
||||
|
||||
background(255);
|
||||
|
||||
// Shift the values to the right
|
||||
for (int i = y.length-1; i > 0; i--) {
|
||||
y[i] = y[i-1];
|
||||
}
|
||||
// Add new values to the beginning
|
||||
my += (mouseX-my) * 0.1;
|
||||
y[0] = my;
|
||||
|
||||
beginShape();
|
||||
for (int i = 1; i < y.length; i++) {
|
||||
vertex(y[i], i);
|
||||
}
|
||||
endShape();
|
||||
|
||||
// Shift the values to the right
|
||||
for (int i = x.length-1; i > 0; i--) {
|
||||
x[i] = x[i-1];
|
||||
}
|
||||
// Add new values to the beginning
|
||||
mx += (mouseY-mx) * 0.1;
|
||||
x[0] = mx;
|
||||
|
||||
|
||||
beginShape();
|
||||
for (int i = 1; i < x.length; i++) {
|
||||
vertex(x[i] * float(width)/height, i);
|
||||
}
|
||||
endShape();
|
||||
|
||||
if(record) {
|
||||
endRecord();
|
||||
record = false;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void mousePressed() {
|
||||
record = true;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
After Width: | Height: | Size: 314 B |
|
After Width: | Height: | Size: 314 B |
|
After Width: | Height: | Size: 318 B |
|
After Width: | Height: | Size: 328 B |
|
After Width: | Height: | Size: 343 B |
|
After Width: | Height: | Size: 379 B |
|
After Width: | Height: | Size: 420 B |
|
After Width: | Height: | Size: 501 B |
|
After Width: | Height: | Size: 615 B |
|
After Width: | Height: | Size: 733 B |