Processing
15
File I /O +Functions +3D
+ +
File I/O
Format FPS DVD Resolution HDTV
NTSC 30 720 480 1280 720 or 1920 1080
PAL 25 720 576 1280 720 or 1920 1080
save(test.tif);
saveFrame(filename-####.tif);
// Page 370 of textbook
// Save 24 frames, from x-1000.tif to x-1023.tif
void draw() {
background(204);
Line(mouseX, mouseY, pmouseX, pmouseY);
if ((frameCount > 999) && (frameCount < 1024)) {
saveFrame("x-####.tif");
}
}
// Save every fifth frame (i.e., x-0005.tif, x-0005.tif, x-0010.tif)
void draw() {
background(204);
line(mouseX, mouseY, pmouseX, pmouseY);
if ((frameCount % 5) == 0) {
saveFrame("x-####.tif");
}
}
import processing.dxf.*
// Page 541 of textbook
beginRaw(DXF, output.dxf);
endRaw();
import saito.objloader.*
model.load (chair.obj);
model.drawMode(POLYGON);
Function
// Page 49 - 50 of textbook
int w = ceil(2.0); // Assign 2 to w
int x = ceil(2.1); // Assign 3 to x
int y = ceil(2.5); // Assign 3 to y
int z = ceil(2.9); // Assign 3 to z
int w = floor(2.0); // Assign 2 to w
int x = floor(2.1); // Assign 2 to x
int y = floor(2.5); // Assign 2 to y
int z = floor(2.9); // Assign 2 to z
int w = round(2.0); // Assign 2 to w
int x = round(2.1); // Assign 2 to x
int y = round(2.5); // Assign 3 to y
int z = round(2.9); // Assign 3 to z
int u = min(5, 9); // Assign 5 to u
int v = min(-4, -12, -9); // Assign -12 to v
float w = min(12.3, 230.24); // Assign 12.3 to w
int x = max(5, 9); // Assign 9 to x
int y = max(-4, -12, -9); // Assign -4 to y
float z = max(12.3, 230.24); // Assign 230.24 to z
// Page 79 - 80 of textbook
float a = sq(1); // Assign 1 to a: Equivalent to 1 * 1
float b = sq(-5); // Assign 25 to b: Equivalent to -5 * -5
float c = sq(9); // Assign 81 to c: Equivalent to 9 * 9
float a = sqrt(6561); // Assign 81 to a
float b = sqrt(625); // Assign 25 to b
float c = sqrt(1); // Assign 1 to c
float a = pow(1, 3); // Assign 1.0 to a: Equivalent to 1*1*1
float b = pow(3, 4); // Assign 81.0 to b: Equivalent to 3*3*3*3
float c = pow(3, -2); // Assign 0.11 to c: Equivalent to 1 / 3*3
float d = pow(-3, 3); // Assign -27.0 to d: Equivalent to -3*-3*-3
// Page 80 - 83 of textbook
norm(value, low, high)
float x = norm(0.0, 0.0, 255.0); // Assign 0.0 to x
float y = norm(102.0, 0.0, 255.0); // Assign 0.4 to y
float z = norm(255.0, 0.0, 255.0); // Assign 1.0 to z
lerp(value1, value2, amt)
float x = lerp(-20.0, 60.0, 0.0); // Assign -20.0 to x
float y = lerp(-20.0, 60.0, 0.5); // Assign 20.0 to y
float z = lerp(-20.0, 60.0, 1.0); // Assign 60.0 to z
map(value, low1, high1, low2, high2)
float x = map(20.0, 0.0, 255.0, -1.0, 1.0); // Assign -0.84 to x
float y = map(0.0, 0.0, 255.0, -1.0, 1.0); // Assign -1.0 to y
float z = map(255.0, 0.0, 255.0, -1.0, 1.0); // Assign 1.0 to z
void setup() {
size(100, 100);
float c = FtoC(451.0); // Assign 232.77779 to c
println(c);
}
float FtoC(float t) {
float f = (t-32.0) * (5.0/9.0);
return f;
}
f1 = x * x + 2 * x + 1
void setup() {
size(100, 100);
int f = fact(10);
println(f);
}
int fact(int n) {
if (n == 0) {return 1;}
else {return (n * (fact (n - 1)));}
}
3D
void setup(){
background(255);
size(600,600,P3D);
smooth();
}
void draw(){
background(0);
lights();
translate(width / 2, height / 2,-height / 2);
rotateY(map(mouseX, 0, width, 0, PI));
rotateZ(map(mouseY, 0, height, 0, -PI));
noStroke();
translate(0, -300, 0);
box(100);
translate(0, -300, 0);
sphere(100);
}
import processing.opengl.*;
void setup(){
background(255);
size(600,600,OPENGL);
smooth();
}
void draw(){
background(0);
lights();
translate(width / 2, height / 2,-height / 2);
rotateY(map(mouseX, 0, width, 0, PI));
rotateZ(map(mouseY, 0, height, 0, -PI));
noStroke();
translate(0, -100, 0);
fill(0,0,255);
box(100);
translate(0, -300, 0);
fill(0,255,0);
sphere(100);
}
import processing.opengl.*;
void setup() {
size(600,600,OPENGL);
}
void draw() {
background(0);
lights();
translate(width / 2, height / 2,-height / 2);
rotateY(map(mouseX, 0, width, PI, -PI));
rotateZ(map(mouseY, 0, height, PI, -PI));
noStroke();
drawBoxes(0,0,0,150,4);
}
void drawBoxes(float x, float y, float z, int len, int n) {
pushMatrix();
translate(x,y,z);
box(len);
float w = 0.75 * len;
if (n > 0) {
drawBoxes( w, w, w,len/2, n-1);}
popMatrix();
}
import processing.opengl.*;
void setup() {
size(600,600,OPENGL);
}
void draw() {
background(0);
lights();
translate(width / 2, height / 2,-height / 2);
rotateY(map(mouseX, 0, width, PI, -PI));
rotateZ(map(mouseY, 0, height, PI, -PI));
noStroke();
drawBoxes(0,0,0,150,4);
}
void drawBoxes(float x, float y, float z, int len, int n)
{
pushMatrix();
translate(x,y,z);
box(len);
float w = 0.75 * len;
if (n > 0) {
drawBoxes( w, w, w,len/2, n-1);
drawBoxes(-w, w, w,len/2, n-1);
drawBoxes( w,-w, w,len/2, n-1);
drawBoxes(-w,-w, w,len/2, n-1);
drawBoxes( w, w,-w,len/2, n-1);
drawBoxes(-w, w,-w,len/2, n-1);
drawBoxes( w,-w,-w,len/2, n-1);
drawBoxes(-w,-w,-w,len/2, n-1); }
popMatrix();
}
import processing.opengl.*;
void setup() {
size(600,600,OPENGL);
}
void draw() {
background(0);
lights();
translate(width / 2, height / 2,-height / 2);
rotateY(map(mouseX, 0, width, PI, -PI));
rotateZ(map(mouseY, 0, height, PI, -PI));
noStroke();
drawBoxes(0,0,0,150,4);
}
void drawBoxes(float x, float y, float z, int len, int n)
{
pushMatrix();
translate(x,y,z);
sphere(len);
float w = 0.75 * len;
if (n > 0) {
drawBoxes( w, w, w,len/2, n-1);
drawBoxes(-w, w, w,len/2, n-1);
drawBoxes( w,-w, w,len/2, n-1);
drawBoxes(-w,-w, w,len/2, n-1);
drawBoxes( w, w,-w,len/2, n-1);
drawBoxes(-w, w,-w,len/2, n-1);
drawBoxes( w,-w,-w,len/2, n-1);
drawBoxes(-w,-w,-w,len/2, n-1); }
popMatrix();
}
void setup()
{
size(400, 400);
background(25); // draw
frameRate(2);
noStroke();
smooth();
}
void draw() {
draw_target((int)random(400),(int)random(400),(int)random(400),15);
draw_target((int)random(400),(int)random(400),(int)random(400),25);
}
void draw_target(int xloc, int yloc, int sizing, int num) {
float grayvalues = 255/num;
float steps = sizing/num;
for(int i=0; i<num; i++) {
fill(i*grayvalues);
ellipse(xloc, yloc, sizing-i*steps, sizing-i*steps);
}
}
File I/O Output 1Images (Page 367)
Function StructureFunctions (Page 194)
3D Extension 23D (Page 525)
Code (Example 1-9) (Page 539)
05/25/2012