blue book examples

This commit is contained in:
benfry
2011-01-26 20:26:16 +00:00
parent a5fb67810a
commit cd888bc239
927 changed files with 36898 additions and 0 deletions

View File

@@ -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;

View File

@@ -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;

View File

@@ -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;

View File

@@ -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

View File

@@ -0,0 +1,2 @@
float int = 50; // ERROR! Unexpected token: float
line(int, 0, int, 100);

View File

@@ -0,0 +1,2 @@
int line = 50; // This does not create a program error
line(line, 0, line, 100); // but it's very confusing

View File

@@ -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

View File

@@ -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);

View File

@@ -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;

View File

@@ -0,0 +1,5 @@
// AVOID loading an image within draw(), it is slow
void draw() {
PImage img = loadImage("tower.jpg");
image(img, 0, 0);
}

View File

@@ -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
}

View File

@@ -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();

View File

@@ -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();

View File

@@ -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();

View File

@@ -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();

View File

@@ -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();

View File

@@ -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
}
}