@@ -0,0 +1,35 @@
|
||||
/**
|
||||
* Array.
|
||||
*
|
||||
* An array is a list of data. Each piece of data in an array
|
||||
* is identified by an index number representing its position in
|
||||
* the array. Arrays are zero based, which means that the first
|
||||
* element in the array is [0], the second element is [1], and so on.
|
||||
* In this example, an array named "coswav" is created and
|
||||
* filled with the cosine values. This data is displayed three
|
||||
* separate ways on the screen.
|
||||
*/
|
||||
|
||||
size(200, 200);
|
||||
|
||||
float[] coswave = new float[width];
|
||||
|
||||
for (int i = 0; i < width; i++) {
|
||||
float amount = map(i, 0, width, 0, PI);
|
||||
coswave[i] = abs(cos(amount));
|
||||
}
|
||||
|
||||
for (int i = 0; i < width; i++) {
|
||||
stroke(coswave[i]*255);
|
||||
line(i, 0, i, height/3);
|
||||
}
|
||||
|
||||
for (int i = 0; i < width; i++) {
|
||||
stroke(coswave[i]*255 / 4);
|
||||
line(i, height/3, i, height/3*2);
|
||||
}
|
||||
|
||||
for (int i = 0; i < width; i++) {
|
||||
stroke(255 - coswave[i]*255);
|
||||
line(i, height/3*2, i, height);
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
import processing.core.*;
|
||||
|
||||
import java.applet.*;
|
||||
import java.awt.*;
|
||||
import java.awt.image.*;
|
||||
import java.awt.event.*;
|
||||
import java.io.*;
|
||||
import java.net.*;
|
||||
import java.text.*;
|
||||
import java.util.*;
|
||||
import java.util.zip.*;
|
||||
import java.util.regex.*;
|
||||
|
||||
public class Array extends PApplet {
|
||||
public void setup() {/**
|
||||
* Array.
|
||||
*
|
||||
* An array is a list of data. Each piece of data in an array
|
||||
* is identified by an index number representing its position in
|
||||
* the array. Arrays are zero based, which means that the first
|
||||
* element in the array is [0], the second element is [1], and so on.
|
||||
* In this example, an array named "coswav" is created and
|
||||
* filled with the cosine values. This data is displayed three
|
||||
* separate ways on the screen.
|
||||
*/
|
||||
|
||||
size(200, 200);
|
||||
|
||||
float[] coswave = new float[width];
|
||||
|
||||
for (int i = 0; i < width; i++) {
|
||||
float amount = map(i, 0, width, 0, PI);
|
||||
coswave[i] = abs(cos(amount));
|
||||
}
|
||||
|
||||
for (int i = 0; i < width; i++) {
|
||||
stroke(coswave[i]*255);
|
||||
line(i, 0, i, height/3);
|
||||
}
|
||||
|
||||
for (int i = 0; i < width; i++) {
|
||||
stroke(coswave[i]*255 / 4);
|
||||
line(i, height/3, i, height/3*2);
|
||||
}
|
||||
|
||||
for (int i = 0; i < width; i++) {
|
||||
stroke(255 - coswave[i]*255);
|
||||
line(i, height/3*2, i, height);
|
||||
}
|
||||
|
||||
noLoop();
|
||||
}
|
||||
static public void main(String args[]) {
|
||||
PApplet.main(new String[] { "Array" });
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
/**
|
||||
* Array.
|
||||
*
|
||||
* An array is a list of data. Each piece of data in an array
|
||||
* is identified by an index number representing its position in
|
||||
* the array. Arrays are zero based, which means that the first
|
||||
* element in the array is [0], the second element is [1], and so on.
|
||||
* In this example, an array named "coswav" is created and
|
||||
* filled with the cosine values. This data is displayed three
|
||||
* separate ways on the screen.
|
||||
*/
|
||||
|
||||
size(200, 200);
|
||||
|
||||
float[] coswave = new float[width];
|
||||
|
||||
for (int i = 0; i < width; i++) {
|
||||
float amount = map(i, 0, width, 0, PI);
|
||||
coswave[i] = abs(cos(amount));
|
||||
}
|
||||
|
||||
for (int i = 0; i < width; i++) {
|
||||
stroke(coswave[i]*255);
|
||||
line(i, 0, i, height/3);
|
||||
}
|
||||
|
||||
for (int i = 0; i < width; i++) {
|
||||
stroke(coswave[i]*255 / 4);
|
||||
line(i, height/3, i, height/3*2);
|
||||
}
|
||||
|
||||
for (int i = 0; i < width; i++) {
|
||||
stroke(255 - coswave[i]*255);
|
||||
line(i, height/3*2, i, height);
|
||||
}
|
||||
|
After Width: | Height: | Size: 2.2 KiB |
@@ -0,0 +1,32 @@
|
||||
/**
|
||||
* Array 2D.
|
||||
*
|
||||
* Demonstrates the syntax for creating a two-dimensional (2D) array.
|
||||
* Values in a 2D array are accessed through two index values.
|
||||
* 2D arrays are useful for storing images. In this example, each dot
|
||||
* is colored in relation to its distance from the center of the image.
|
||||
*/
|
||||
|
||||
float[][] distances;
|
||||
float maxDistance;
|
||||
|
||||
size(200, 200);
|
||||
background(0);
|
||||
maxDistance = dist(width/2, height/2, width, height);
|
||||
distances = new float[width][height];
|
||||
for(int i=0; i<height; i++) {
|
||||
for(int j=0; j<width; j++) {
|
||||
float dist = dist(width/2, height/2, j, i);
|
||||
distances[j][i] = dist/maxDistance * 255;
|
||||
}
|
||||
}
|
||||
|
||||
for(int i=0; i<height; i+=2) {
|
||||
for(int j=0; j<width; j+=2) {
|
||||
stroke(distances[j][i]);
|
||||
point(j, i);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,53 @@
|
||||
import processing.core.*;
|
||||
|
||||
import java.applet.*;
|
||||
import java.awt.*;
|
||||
import java.awt.image.*;
|
||||
import java.awt.event.*;
|
||||
import java.io.*;
|
||||
import java.net.*;
|
||||
import java.text.*;
|
||||
import java.util.*;
|
||||
import java.util.zip.*;
|
||||
import java.util.regex.*;
|
||||
|
||||
public class Array2D extends PApplet {
|
||||
public void setup() {/**
|
||||
* Array 2D.
|
||||
*
|
||||
* Demonstrates the syntax for creating a two-dimensional (2D) array.
|
||||
* Values in a 2D array are accessed through two index values.
|
||||
* 2D arrays are useful for storing images. In this example, each dot
|
||||
* is colored in relation to its distance from the center of the image.
|
||||
*/
|
||||
|
||||
float[][] distances;
|
||||
float maxDistance;
|
||||
|
||||
size(200, 200);
|
||||
background(0);
|
||||
maxDistance = dist(width/2, height/2, width, height);
|
||||
distances = new float[width][height];
|
||||
for(int i=0; i<height; i++) {
|
||||
for(int j=0; j<width; j++) {
|
||||
float dist = dist(width/2, height/2, j, i);
|
||||
distances[j][i] = dist/maxDistance * 255;
|
||||
}
|
||||
}
|
||||
|
||||
for(int i=0; i<height; i+=2) {
|
||||
for(int j=0; j<width; j+=2) {
|
||||
stroke(distances[j][i]);
|
||||
point(j, i);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
noLoop();
|
||||
}
|
||||
static public void main(String args[]) {
|
||||
PApplet.main(new String[] { "Array2D" });
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
/**
|
||||
* Array 2D.
|
||||
*
|
||||
* Demonstrates the syntax for creating a two-dimensional (2D) array.
|
||||
* Values in a 2D array are accessed through two index values.
|
||||
* 2D arrays are useful for storing images. In this example, each dot
|
||||
* is colored in relation to its distance from the center of the image.
|
||||
*/
|
||||
|
||||
float[][] distances;
|
||||
float maxDistance;
|
||||
|
||||
size(200, 200);
|
||||
background(0);
|
||||
maxDistance = dist(width/2, height/2, width, height);
|
||||
distances = new float[width][height];
|
||||
for(int i=0; i<height; i++) {
|
||||
for(int j=0; j<width; j++) {
|
||||
float dist = dist(width/2, height/2, j, i);
|
||||
distances[j][i] = dist/maxDistance * 255;
|
||||
}
|
||||
}
|
||||
|
||||
for(int i=0; i<height; i+=2) {
|
||||
for(int j=0; j<width; j+=2) {
|
||||
stroke(distances[j][i]);
|
||||
point(j, i);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
After Width: | Height: | Size: 2.2 KiB |
@@ -0,0 +1,39 @@
|
||||
/**
|
||||
* Array Objects.
|
||||
*
|
||||
* Demonstrates the syntax for creating an array of custom objects.
|
||||
*
|
||||
* Updated 26 February 2010.
|
||||
*/
|
||||
|
||||
int unit = 40;
|
||||
int count;
|
||||
Module[] mods;
|
||||
|
||||
|
||||
void setup() {
|
||||
size(320, 240);
|
||||
background(176);
|
||||
noStroke();
|
||||
|
||||
int wideCount = width / unit;
|
||||
int highCount = height / unit;
|
||||
count = wideCount * highCount;
|
||||
mods = new Module[count];
|
||||
|
||||
int index = 0;
|
||||
for (int y = 0; y < highCount; y++) {
|
||||
for (int x = 0; x < wideCount; x++) {
|
||||
mods[index++] = new Module(x*unit, y*unit, unit/2, unit/2, random(0.05, 0.8));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void draw() {
|
||||
for (int i = 0; i < count; i++) {
|
||||
mods[i].update();
|
||||
mods[i].draw();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,38 @@
|
||||
class Module {
|
||||
int mx, my;
|
||||
int big;
|
||||
float x, y;
|
||||
int xdir = 1;
|
||||
int ydir = 1;
|
||||
float speed;
|
||||
|
||||
// Contructor (required)
|
||||
Module(int imx, int imy, int ix, int iy, float ispeed) {
|
||||
mx = imx;
|
||||
my = imy;
|
||||
x = ix;
|
||||
y = iy;
|
||||
speed = ispeed;
|
||||
big = unit;
|
||||
}
|
||||
|
||||
// Custom method for updating the variables
|
||||
void update() {
|
||||
x = x + (speed * xdir);
|
||||
if (x >= big || x <= 0) {
|
||||
xdir *= -1;
|
||||
x = x + (1 * xdir);
|
||||
y = y + (1 * ydir);
|
||||
}
|
||||
if (y >= big || y <= 0) {
|
||||
ydir *= -1;
|
||||
y = y + (1 * ydir);
|
||||
}
|
||||
}
|
||||
|
||||
// Custom method for drawing the object
|
||||
void draw() {
|
||||
stroke(second() * 4);
|
||||
point(mx+x-1, my+y-1);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,94 @@
|
||||
import processing.core.*;
|
||||
|
||||
import java.applet.*;
|
||||
import java.awt.*;
|
||||
import java.awt.image.*;
|
||||
import java.awt.event.*;
|
||||
import java.io.*;
|
||||
import java.net.*;
|
||||
import java.text.*;
|
||||
import java.util.*;
|
||||
import java.util.zip.*;
|
||||
import java.util.regex.*;
|
||||
|
||||
public class ArrayObjects extends PApplet {
|
||||
|
||||
/**
|
||||
* Array Objects.
|
||||
*
|
||||
* Demonstrates the syntax for creating an array of custom objects.
|
||||
*/
|
||||
|
||||
int unit = 40;
|
||||
int num;
|
||||
Module[] mods;
|
||||
|
||||
public void setup()
|
||||
{
|
||||
size(200, 200);
|
||||
background(176);
|
||||
noStroke();
|
||||
|
||||
num = width/unit * width/unit;
|
||||
mods = new Module[num];
|
||||
|
||||
for (int i=0; i<height/unit; i++) {
|
||||
for(int j=0; j<height/unit; j++) {
|
||||
int index = i*height/unit + j;
|
||||
mods[index] = new Module(j*unit, i*unit, unit/2, unit/2, random(0.05f, 0.8f));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void draw()
|
||||
{
|
||||
for(int i=0; i<num; i++) {
|
||||
mods[i].update();
|
||||
mods[i].draw();
|
||||
}
|
||||
}
|
||||
|
||||
class Module {
|
||||
float mx, my;
|
||||
int size = unit;
|
||||
float x, y = 0;
|
||||
int xdir = 1;
|
||||
int ydir = 1;
|
||||
float speed;
|
||||
|
||||
// Contructor (required)
|
||||
Module(float imx, float imy, float ix, float iy, float ispeed) {
|
||||
mx = imy;
|
||||
my = imx;
|
||||
x = PApplet.parseInt(ix);
|
||||
y = PApplet.parseInt(iy);
|
||||
speed = ispeed;
|
||||
}
|
||||
|
||||
// Custom method for updating the variables
|
||||
public void update() {
|
||||
x = x + (speed * xdir);
|
||||
if (x >= size || x <= 0) {
|
||||
xdir *= -1;
|
||||
x = x + (1 * xdir);
|
||||
y = y + (1 * ydir);
|
||||
}
|
||||
if (y >= size || y <= 0) {
|
||||
ydir *= -1;
|
||||
y = y + (1 * ydir);
|
||||
}
|
||||
}
|
||||
|
||||
// Custom method for drawing the object
|
||||
public void draw() {
|
||||
stroke(second()*4);
|
||||
point(mx+x-1, my+y-1);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
static public void main(String args[]) {
|
||||
PApplet.main(new String[] { "ArrayObjects" });
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,74 @@
|
||||
/**
|
||||
* Array Objects.
|
||||
*
|
||||
* Demonstrates the syntax for creating an array of custom objects.
|
||||
*/
|
||||
|
||||
int unit = 40;
|
||||
int num;
|
||||
Module[] mods;
|
||||
|
||||
void setup()
|
||||
{
|
||||
size(200, 200);
|
||||
background(176);
|
||||
noStroke();
|
||||
|
||||
num = width/unit * width/unit;
|
||||
mods = new Module[num];
|
||||
|
||||
for (int i=0; i<height/unit; i++) {
|
||||
for(int j=0; j<height/unit; j++) {
|
||||
int index = i*height/unit + j;
|
||||
mods[index] = new Module(j*unit, i*unit, unit/2, unit/2, random(0.05, 0.8));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void draw()
|
||||
{
|
||||
for(int i=0; i<num; i++) {
|
||||
mods[i].update();
|
||||
mods[i].draw();
|
||||
}
|
||||
}
|
||||
|
||||
class Module {
|
||||
float mx, my;
|
||||
int size = unit;
|
||||
float x, y = 0;
|
||||
int xdir = 1;
|
||||
int ydir = 1;
|
||||
float speed;
|
||||
|
||||
// Contructor (required)
|
||||
Module(float imx, float imy, float ix, float iy, float ispeed) {
|
||||
mx = imy;
|
||||
my = imx;
|
||||
x = int(ix);
|
||||
y = int(iy);
|
||||
speed = ispeed;
|
||||
}
|
||||
|
||||
// Custom method for updating the variables
|
||||
void update() {
|
||||
x = x + (speed * xdir);
|
||||
if (x >= size || x <= 0) {
|
||||
xdir *= -1;
|
||||
x = x + (1 * xdir);
|
||||
y = y + (1 * ydir);
|
||||
}
|
||||
if (y >= size || y <= 0) {
|
||||
ydir *= -1;
|
||||
y = y + (1 * ydir);
|
||||
}
|
||||
}
|
||||
|
||||
// Custom method for drawing the object
|
||||
void draw() {
|
||||
stroke(second()*4);
|
||||
point(mx+x-1, my+y-1);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
After Width: | Height: | Size: 2.2 KiB |
@@ -0,0 +1,29 @@
|
||||
/**
|
||||
* Brightness
|
||||
* by Rusty Robison.
|
||||
*
|
||||
* Brightness is the relative lightness or darkness of a color.
|
||||
* Move the cursor vertically over each bar to alter its brightness.
|
||||
*
|
||||
* Updated 28 February 2010.
|
||||
*/
|
||||
|
||||
int barWidth = 5;
|
||||
int lastBar = -1;
|
||||
|
||||
void setup() {
|
||||
size(200, 200);
|
||||
colorMode(HSB, 360, 100, height);
|
||||
noStroke();
|
||||
background(0);
|
||||
}
|
||||
|
||||
void draw() {
|
||||
int whichBar = mouseX / barWidth;
|
||||
if (whichBar != lastBar) {
|
||||
int barX = whichBar * barWidth;
|
||||
fill(barX, 100, mouseY);
|
||||
rect(barX, 0, barWidth, height);
|
||||
lastBar = whichBar;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
import processing.core.*;
|
||||
|
||||
import java.applet.*;
|
||||
import java.awt.*;
|
||||
import java.awt.image.*;
|
||||
import java.awt.event.*;
|
||||
import java.io.*;
|
||||
import java.net.*;
|
||||
import java.text.*;
|
||||
import java.util.*;
|
||||
import java.util.zip.*;
|
||||
import java.util.regex.*;
|
||||
|
||||
public class Brightness extends PApplet {
|
||||
|
||||
/**
|
||||
* Brightness
|
||||
* by Rusty Robison.
|
||||
*
|
||||
* Brightness is the relative lightness or darkness of a color.
|
||||
* Move the cursor vertically over each bar to alter its brightness.
|
||||
*/
|
||||
|
||||
int barWidth = 5;
|
||||
int[] brightness;
|
||||
|
||||
public void setup()
|
||||
{
|
||||
size(200, 200);
|
||||
colorMode(HSB, 360, height, height);
|
||||
brightness = new int[width/barWidth];
|
||||
}
|
||||
|
||||
public void draw()
|
||||
{
|
||||
int j = 0;
|
||||
for (int i = 0; i <= (width-barWidth); i += barWidth) {
|
||||
noStroke();
|
||||
if ((mouseX > i) && (mouseX < i+barWidth)) {
|
||||
brightness[j] = mouseY;
|
||||
}
|
||||
fill(i, height, brightness[j]);
|
||||
rect(i, 0, barWidth, height);
|
||||
j++;
|
||||
}
|
||||
}
|
||||
|
||||
static public void main(String args[]) {
|
||||
PApplet.main(new String[] { "Brightness" });
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
/**
|
||||
* Brightness
|
||||
* by Rusty Robison.
|
||||
*
|
||||
* Brightness is the relative lightness or darkness of a color.
|
||||
* Move the cursor vertically over each bar to alter its brightness.
|
||||
*/
|
||||
|
||||
int barWidth = 5;
|
||||
int[] brightness;
|
||||
|
||||
void setup()
|
||||
{
|
||||
size(200, 200);
|
||||
colorMode(HSB, 360, height, height);
|
||||
brightness = new int[width/barWidth];
|
||||
}
|
||||
|
||||
void draw()
|
||||
{
|
||||
int j = 0;
|
||||
for (int i = 0; i <= (width-barWidth); i += barWidth) {
|
||||
noStroke();
|
||||
if ((mouseX > i) && (mouseX < i+barWidth)) {
|
||||
brightness[j] = mouseY;
|
||||
}
|
||||
fill(i, height, brightness[j]);
|
||||
rect(i, 0, barWidth, height);
|
||||
j++;
|
||||
}
|
||||
}
|
||||
|
After Width: | Height: | Size: 2.2 KiB |
@@ -0,0 +1,88 @@
|
||||
/**
|
||||
* Subtractive Color Wheel
|
||||
* by Ira Greenberg.
|
||||
*
|
||||
* The primaries are red, yellow, and blue. The secondaries are green,
|
||||
* purple, and orange. The tertiaries are yellow-orange, red-orange,
|
||||
* red-purple, blue-purple, blue-green, and yellow-green.
|
||||
*
|
||||
* Create a shade or tint of the subtractive color wheel using
|
||||
* SHADE or TINT parameters.
|
||||
*
|
||||
* Updated 26 February 2010.
|
||||
*/
|
||||
|
||||
int segs = 12;
|
||||
int steps = 6;
|
||||
float rotAdjust = TWO_PI / segs / 2;
|
||||
float radius;
|
||||
float segWidth;
|
||||
float interval = TWO_PI / segs;
|
||||
|
||||
|
||||
void setup() {
|
||||
size(200, 200);
|
||||
background(127);
|
||||
smooth();
|
||||
ellipseMode(RADIUS);
|
||||
noStroke();
|
||||
// make the diameter 90% of the sketch area
|
||||
radius = min(width, height) * 0.45;
|
||||
segWidth = radius / steps;
|
||||
|
||||
// swap which line is commented out to draw the other version
|
||||
//drawTintWheel();
|
||||
drawShadeWheel();
|
||||
}
|
||||
|
||||
|
||||
void drawShadeWheel() {
|
||||
for (int j = 0; j < steps; j++) {
|
||||
color[] cols = {
|
||||
color(255-(255/steps)*j, 255-(255/steps)*j, 0),
|
||||
color(255-(255/steps)*j, (255/1.5)-((255/1.5)/steps)*j, 0),
|
||||
color(255-(255/steps)*j, (255/2)-((255/2)/steps)*j, 0),
|
||||
color(255-(255/steps)*j, (255/2.5)-((255/2.5)/steps)*j, 0),
|
||||
color(255-(255/steps)*j, 0, 0),
|
||||
color(255-(255/steps)*j, 0, (255/2)-((255/2)/steps)*j),
|
||||
color(255-(255/steps)*j, 0, 255-(255/steps)*j),
|
||||
color((255/2)-((255/2)/steps)*j, 0, 255-(255/steps)*j),
|
||||
color(0, 0, 255-(255/steps)*j),
|
||||
color(0, 255-(255/steps)*j, (255/2.5)-((255/2.5)/steps)*j),
|
||||
color(0, 255-(255/steps)*j, 0),
|
||||
color((255/2)-((255/2)/steps)*j, 255-(255/steps)*j, 0)
|
||||
};
|
||||
for (int i = 0; i < segs; i++) {
|
||||
fill(cols[i]);
|
||||
arc(width/2, height/2, radius, radius,
|
||||
interval*i+rotAdjust, interval*(i+1)+rotAdjust);
|
||||
}
|
||||
radius -= segWidth;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void drawTintWheel() {
|
||||
for (int j = 0; j < steps; j++) {
|
||||
color[] cols = {
|
||||
color((255/steps)*j, (255/steps)*j, 0),
|
||||
color((255/steps)*j, ((255/1.5)/steps)*j, 0),
|
||||
color((255/steps)*j, ((255/2)/steps)*j, 0),
|
||||
color((255/steps)*j, ((255/2.5)/steps)*j, 0),
|
||||
color((255/steps)*j, 0, 0),
|
||||
color((255/steps)*j, 0, ((255/2)/steps)*j),
|
||||
color((255/steps)*j, 0, (255/steps)*j),
|
||||
color(((255/2)/steps)*j, 0, (255/steps)*j),
|
||||
color(0, 0, (255/steps)*j),
|
||||
color(0, (255/steps)*j, ((255/2.5)/steps)*j),
|
||||
color(0, (255/steps)*j, 0),
|
||||
color(((255/2)/steps)*j, (255/steps)*j, 0)
|
||||
};
|
||||
for (int i = 0; i < segs; i++) {
|
||||
fill(cols[i]);
|
||||
arc(width/2, height/2, radius, radius,
|
||||
interval*i+rotAdjust, interval*(i+1)+rotAdjust);
|
||||
}
|
||||
radius -= segWidth;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,99 @@
|
||||
import processing.core.*;
|
||||
|
||||
import java.applet.*;
|
||||
import java.awt.*;
|
||||
import java.awt.image.*;
|
||||
import java.awt.event.*;
|
||||
import java.io.*;
|
||||
import java.net.*;
|
||||
import java.text.*;
|
||||
import java.util.*;
|
||||
import java.util.zip.*;
|
||||
import java.util.regex.*;
|
||||
|
||||
public class ColorWheel extends PApplet {
|
||||
|
||||
/**
|
||||
* Subtractive Color Wheel
|
||||
* by Ira Greenberg.
|
||||
*
|
||||
* The primaries are red, yellow, and blue. The
|
||||
* secondaries are green, purple, and orange. The
|
||||
* tertiaries are yellow-orange, red-orange, red-purple,
|
||||
* blue-purple, blue-green, and yellow-green.
|
||||
*
|
||||
* Create a shade or tint of the
|
||||
* subtractive color wheel using
|
||||
* SHADE or TINT parameters.
|
||||
*/
|
||||
|
||||
int segs = 12;
|
||||
int steps = 6;
|
||||
float rotAdjust = radians(360.0f/segs/2.0f);
|
||||
float radius = 95.0f;
|
||||
float segWidth = radius/steps;
|
||||
float interval = TWO_PI/segs;
|
||||
int SHADE = 0;
|
||||
int TINT = 1;
|
||||
|
||||
public void setup(){
|
||||
size(200, 200);
|
||||
background(127);
|
||||
smooth();
|
||||
ellipseMode(CENTER_RADIUS);
|
||||
noStroke();
|
||||
// you can substitue TINT for SHADE argument
|
||||
createWheel(width/2, height/2, SHADE);
|
||||
}
|
||||
|
||||
public void createWheel(int x, int y, int valueShift){
|
||||
if (valueShift == SHADE){
|
||||
for (int j=0; j<steps; j++){
|
||||
int[]cols = {
|
||||
color(255-(255/steps)*j, 255-(255/steps)*j, 0),
|
||||
color(255-(255/steps)*j, (255/1.5f)-((255/1.5f)/steps)*j, 0),
|
||||
color(255-(255/steps)*j, (255/2)-((255/2)/steps)*j, 0),
|
||||
color(255-(255/steps)*j, (255/2.5f)-((255/2.5f)/steps)*j, 0),
|
||||
color(255-(255/steps)*j, 0, 0),
|
||||
color(255-(255/steps)*j, 0, (255/2)-((255/2)/steps)*j),
|
||||
color(255-(255/steps)*j, 0, 255-(255/steps)*j),
|
||||
color((255/2)-((255/2)/steps)*j, 0, 255-(255/steps)*j),
|
||||
color(0, 0, 255-(255/steps)*j),
|
||||
color(0, 255-(255/steps)*j, (255/2.5f)-((255/2.5f)/steps)*j),
|
||||
color(0, 255-(255/steps)*j, 0),
|
||||
color((255/2)-((255/2)/steps)*j, 255-(255/steps)*j, 0) };
|
||||
for (int i=0; i< segs; i++){
|
||||
fill(cols[i]);
|
||||
arc(x, y, radius, radius, interval*i+rotAdjust, interval*(i+1)+rotAdjust);
|
||||
}
|
||||
radius -= segWidth;
|
||||
}
|
||||
} else if (valueShift == TINT){
|
||||
for (int j=0; j<steps; j++){
|
||||
int[]cols = {
|
||||
color((255/steps)*j, (255/steps)*j, 0),
|
||||
color((255/steps)*j, ((255/1.5f)/steps)*j, 0),
|
||||
color((255/steps)*j, ((255/2)/steps)*j, 0),
|
||||
color((255/steps)*j, ((255/2.5f)/steps)*j, 0),
|
||||
color((255/steps)*j, 0, 0),
|
||||
color((255/steps)*j, 0, ((255/2)/steps)*j),
|
||||
color((255/steps)*j, 0, (255/steps)*j),
|
||||
color(((255/2)/steps)*j, 0, (255/steps)*j),
|
||||
color(0, 0, (255/steps)*j),
|
||||
color(0, (255/steps)*j, ((255/2.5f)/steps)*j),
|
||||
color(0, (255/steps)*j, 0),
|
||||
color(((255/2)/steps)*j, (255/steps)*j, 0) };
|
||||
for (int i=0; i< segs; i++){
|
||||
fill(cols[i]);
|
||||
arc(x, y, radius, radius, interval*i+rotAdjust, interval*(i+1)+rotAdjust);
|
||||
}
|
||||
radius -= segWidth;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static public void main(String args[]) {
|
||||
PApplet.main(new String[] { "ColorWheel" });
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,79 @@
|
||||
/**
|
||||
* Subtractive Color Wheel
|
||||
* by Ira Greenberg.
|
||||
*
|
||||
* The primaries are red, yellow, and blue. The
|
||||
* secondaries are green, purple, and orange. The
|
||||
* tertiaries are yellow-orange, red-orange, red-purple,
|
||||
* blue-purple, blue-green, and yellow-green.
|
||||
*
|
||||
* Create a shade or tint of the
|
||||
* subtractive color wheel using
|
||||
* SHADE or TINT parameters.
|
||||
*/
|
||||
|
||||
int segs = 12;
|
||||
int steps = 6;
|
||||
float rotAdjust = radians(360.0/segs/2.0);
|
||||
float radius = 95.0;
|
||||
float segWidth = radius/steps;
|
||||
float interval = TWO_PI/segs;
|
||||
int SHADE = 0;
|
||||
int TINT = 1;
|
||||
|
||||
void setup(){
|
||||
size(200, 200);
|
||||
background(127);
|
||||
smooth();
|
||||
ellipseMode(CENTER_RADIUS);
|
||||
noStroke();
|
||||
// you can substitue TINT for SHADE argument
|
||||
createWheel(width/2, height/2, SHADE);
|
||||
}
|
||||
|
||||
void createWheel(int x, int y, int valueShift){
|
||||
if (valueShift == SHADE){
|
||||
for (int j=0; j<steps; j++){
|
||||
color[]cols = {
|
||||
color(255-(255/steps)*j, 255-(255/steps)*j, 0),
|
||||
color(255-(255/steps)*j, (255/1.5)-((255/1.5)/steps)*j, 0),
|
||||
color(255-(255/steps)*j, (255/2)-((255/2)/steps)*j, 0),
|
||||
color(255-(255/steps)*j, (255/2.5)-((255/2.5)/steps)*j, 0),
|
||||
color(255-(255/steps)*j, 0, 0),
|
||||
color(255-(255/steps)*j, 0, (255/2)-((255/2)/steps)*j),
|
||||
color(255-(255/steps)*j, 0, 255-(255/steps)*j),
|
||||
color((255/2)-((255/2)/steps)*j, 0, 255-(255/steps)*j),
|
||||
color(0, 0, 255-(255/steps)*j),
|
||||
color(0, 255-(255/steps)*j, (255/2.5)-((255/2.5)/steps)*j),
|
||||
color(0, 255-(255/steps)*j, 0),
|
||||
color((255/2)-((255/2)/steps)*j, 255-(255/steps)*j, 0) };
|
||||
for (int i=0; i< segs; i++){
|
||||
fill(cols[i]);
|
||||
arc(x, y, radius, radius, interval*i+rotAdjust, interval*(i+1)+rotAdjust);
|
||||
}
|
||||
radius -= segWidth;
|
||||
}
|
||||
} else if (valueShift == TINT){
|
||||
for (int j=0; j<steps; j++){
|
||||
color[]cols = {
|
||||
color((255/steps)*j, (255/steps)*j, 0),
|
||||
color((255/steps)*j, ((255/1.5)/steps)*j, 0),
|
||||
color((255/steps)*j, ((255/2)/steps)*j, 0),
|
||||
color((255/steps)*j, ((255/2.5)/steps)*j, 0),
|
||||
color((255/steps)*j, 0, 0),
|
||||
color((255/steps)*j, 0, ((255/2)/steps)*j),
|
||||
color((255/steps)*j, 0, (255/steps)*j),
|
||||
color(((255/2)/steps)*j, 0, (255/steps)*j),
|
||||
color(0, 0, (255/steps)*j),
|
||||
color(0, (255/steps)*j, ((255/2.5)/steps)*j),
|
||||
color(0, (255/steps)*j, 0),
|
||||
color(((255/2)/steps)*j, (255/steps)*j, 0) };
|
||||
for (int i=0; i< segs; i++){
|
||||
fill(cols[i]);
|
||||
arc(x, y, radius, radius, interval*i+rotAdjust, interval*(i+1)+rotAdjust);
|
||||
}
|
||||
radius -= segWidth;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
After Width: | Height: | Size: 2.2 KiB |
@@ -0,0 +1,26 @@
|
||||
/**
|
||||
* Creating Colors (Homage to Albers).
|
||||
*
|
||||
* Creating variables for colors that may be referred to
|
||||
* in the program by their name, rather than a number.
|
||||
*/
|
||||
|
||||
size(200, 200);
|
||||
noStroke();
|
||||
|
||||
color inside = color(204, 102, 0);
|
||||
color middle = color(204, 153, 0);
|
||||
color outside = color(153, 51, 0);
|
||||
|
||||
// These statements are equivalent to the statements above.
|
||||
// Programmers may use the format they prefer.
|
||||
//color inside = #CC6600;
|
||||
//color middle = #CC9900;
|
||||
//color outside = #993300;
|
||||
|
||||
fill(outside);
|
||||
rect(0, 0, 200, 200);
|
||||
fill(middle);
|
||||
rect(40, 60, 120, 120);
|
||||
fill(inside);
|
||||
rect(60, 90, 80, 80);
|
||||
@@ -0,0 +1,47 @@
|
||||
import processing.core.*;
|
||||
|
||||
import java.applet.*;
|
||||
import java.awt.*;
|
||||
import java.awt.image.*;
|
||||
import java.awt.event.*;
|
||||
import java.io.*;
|
||||
import java.net.*;
|
||||
import java.text.*;
|
||||
import java.util.*;
|
||||
import java.util.zip.*;
|
||||
import java.util.regex.*;
|
||||
|
||||
public class Creating extends PApplet {
|
||||
public void setup() {/**
|
||||
* Creating Colors (Homage to Albers).
|
||||
*
|
||||
* Creating variables for colors that may be referred to
|
||||
* in the program by their name, rather than a number.
|
||||
*/
|
||||
|
||||
size(200, 200);
|
||||
noStroke();
|
||||
|
||||
int inside = color(204, 102, 0);
|
||||
int middle = color(204, 153, 0);
|
||||
int outside = color(153, 51, 0);
|
||||
|
||||
// These statements are equivalent to the statements above.
|
||||
// Programmers may use the format they prefer.
|
||||
//color inside = #CC6600;
|
||||
//color middle = #CC9900;
|
||||
//color outside = #993300;
|
||||
|
||||
fill(outside);
|
||||
rect(0, 0, 200, 200);
|
||||
fill(middle);
|
||||
rect(40, 60, 120, 120);
|
||||
fill(inside);
|
||||
rect(60, 90, 80, 80);
|
||||
|
||||
noLoop();
|
||||
}
|
||||
static public void main(String args[]) {
|
||||
PApplet.main(new String[] { "Creating" });
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
/**
|
||||
* Creating Colors (Homage to Albers).
|
||||
*
|
||||
* Creating variables for colors that may be referred to
|
||||
* in the program by their name, rather than a number.
|
||||
*/
|
||||
|
||||
size(200, 200);
|
||||
noStroke();
|
||||
|
||||
color inside = color(204, 102, 0);
|
||||
color middle = color(204, 153, 0);
|
||||
color outside = color(153, 51, 0);
|
||||
|
||||
// These statements are equivalent to the statements above.
|
||||
// Programmers may use the format they prefer.
|
||||
//color inside = #CC6600;
|
||||
//color middle = #CC9900;
|
||||
//color outside = #993300;
|
||||
|
||||
fill(outside);
|
||||
rect(0, 0, 200, 200);
|
||||
fill(middle);
|
||||
rect(40, 60, 120, 120);
|
||||
fill(inside);
|
||||
rect(60, 90, 80, 80);
|
||||
|
After Width: | Height: | Size: 2.2 KiB |
@@ -0,0 +1,31 @@
|
||||
/**
|
||||
* Hue.
|
||||
*
|
||||
* Hue is the color reflected from or transmitted through an object
|
||||
* and is typically referred to as the name of the color (red, blue, yellow, etc.)
|
||||
* Move the cursor vertically over each bar to alter its hue.
|
||||
*/
|
||||
|
||||
int barWidth = 5;
|
||||
int[] hue;
|
||||
|
||||
void setup()
|
||||
{
|
||||
size(200, 200);
|
||||
colorMode(HSB, 360, height, height);
|
||||
hue = new int[width/barWidth];
|
||||
noStroke();
|
||||
}
|
||||
|
||||
void draw()
|
||||
{
|
||||
int j = 0;
|
||||
for (int i=0; i<=(width-barWidth); i+=barWidth) {
|
||||
if ((mouseX > i) && (mouseX < i+barWidth)) {
|
||||
hue[j] = mouseY;
|
||||
}
|
||||
fill(hue[j], height/1.2, height/1.2);
|
||||
rect(i, 0, barWidth, height);
|
||||
j++;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
import processing.core.*;
|
||||
|
||||
import java.applet.*;
|
||||
import java.awt.*;
|
||||
import java.awt.image.*;
|
||||
import java.awt.event.*;
|
||||
import java.io.*;
|
||||
import java.net.*;
|
||||
import java.text.*;
|
||||
import java.util.*;
|
||||
import java.util.zip.*;
|
||||
import java.util.regex.*;
|
||||
|
||||
public class Hue extends PApplet {
|
||||
|
||||
/**
|
||||
* Hue.
|
||||
*
|
||||
* Hue is the color reflected from or transmitted through an object
|
||||
* and is typically referred to as the name of the color (red, blue, yellow, etc.)
|
||||
* Move the cursor vertically over each bar to alter its hue.
|
||||
*/
|
||||
|
||||
int barWidth = 5;
|
||||
int[] hue;
|
||||
|
||||
public void setup()
|
||||
{
|
||||
size(200, 200);
|
||||
colorMode(HSB, 360, height, height);
|
||||
hue = new int[width/barWidth];
|
||||
noStroke();
|
||||
}
|
||||
|
||||
public void draw()
|
||||
{
|
||||
int j = 0;
|
||||
for (int i=0; i<=(width-barWidth); i+=barWidth) {
|
||||
if ((mouseX > i) && (mouseX < i+barWidth)) {
|
||||
hue[j] = mouseY;
|
||||
}
|
||||
fill(hue[j], height/1.2f, height/1.2f);
|
||||
rect(i, 0, barWidth, height);
|
||||
j++;
|
||||
}
|
||||
}
|
||||
|
||||
static public void main(String args[]) {
|
||||
PApplet.main(new String[] { "Hue" });
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
/**
|
||||
* Hue.
|
||||
*
|
||||
* Hue is the color reflected from or transmitted through an object
|
||||
* and is typically referred to as the name of the color (red, blue, yellow, etc.)
|
||||
* Move the cursor vertically over each bar to alter its hue.
|
||||
*/
|
||||
|
||||
int barWidth = 5;
|
||||
int[] hue;
|
||||
|
||||
void setup()
|
||||
{
|
||||
size(200, 200);
|
||||
colorMode(HSB, 360, height, height);
|
||||
hue = new int[width/barWidth];
|
||||
noStroke();
|
||||
}
|
||||
|
||||
void draw()
|
||||
{
|
||||
int j = 0;
|
||||
for (int i=0; i<=(width-barWidth); i+=barWidth) {
|
||||
if ((mouseX > i) && (mouseX < i+barWidth)) {
|
||||
hue[j] = mouseY;
|
||||
}
|
||||
fill(hue[j], height/1.2, height/1.2);
|
||||
rect(i, 0, barWidth, height);
|
||||
j++;
|
||||
}
|
||||
}
|
||||
|
After Width: | Height: | Size: 2.2 KiB |
@@ -0,0 +1,73 @@
|
||||
/**
|
||||
* Simple Linear Gradient
|
||||
* by Ira Greenberg.
|
||||
*
|
||||
* Using the convenient red(), green()
|
||||
* and blue() component functions,
|
||||
* generate some linear gradients.
|
||||
*/
|
||||
|
||||
// constants
|
||||
int Y_AXIS = 1;
|
||||
int X_AXIS = 2;
|
||||
|
||||
void setup(){
|
||||
size(200, 200);
|
||||
|
||||
// create some gradients
|
||||
// background
|
||||
color b1 = color(190, 190, 190);
|
||||
color b2 = color(20, 20, 20);
|
||||
setGradient(0, 0, width, height, b1, b2, Y_AXIS);
|
||||
//center squares
|
||||
color c1 = color(255, 120, 0);
|
||||
color c2 = color(10, 45, 255);
|
||||
color c3 = color(10, 255, 15);
|
||||
color c4 = color(125, 2, 140);
|
||||
color c5 = color(255, 255, 0);
|
||||
color c6 = color(25, 255, 200);
|
||||
setGradient(25, 25, 75, 75, c1, c2, Y_AXIS);
|
||||
setGradient(100, 25, 75, 75, c3, c4, X_AXIS);
|
||||
setGradient(25, 100, 75, 75, c2, c5, X_AXIS);
|
||||
setGradient(100, 100, 75, 75, c4, c6, Y_AXIS);
|
||||
}
|
||||
|
||||
void setGradient(int x, int y, float w, float h, color c1, color c2, int axis ){
|
||||
// calculate differences between color components
|
||||
float deltaR = red(c2)-red(c1);
|
||||
float deltaG = green(c2)-green(c1);
|
||||
float deltaB = blue(c2)-blue(c1);
|
||||
|
||||
// choose axis
|
||||
if(axis == Y_AXIS){
|
||||
/*nested for loops set pixels
|
||||
in a basic table structure */
|
||||
// column
|
||||
for (int i=x; i<=(x+w); i++){
|
||||
// row
|
||||
for (int j = y; j<=(y+h); j++){
|
||||
color c = color(
|
||||
(red(c1)+(j-y)*(deltaR/h)),
|
||||
(green(c1)+(j-y)*(deltaG/h)),
|
||||
(blue(c1)+(j-y)*(deltaB/h))
|
||||
);
|
||||
set(i, j, c);
|
||||
}
|
||||
}
|
||||
}
|
||||
else if(axis == X_AXIS){
|
||||
// column
|
||||
for (int i=y; i<=(y+h); i++){
|
||||
// row
|
||||
for (int j = x; j<=(x+w); j++){
|
||||
color c = color(
|
||||
(red(c1)+(j-x)*(deltaR/h)),
|
||||
(green(c1)+(j-x)*(deltaG/h)),
|
||||
(blue(c1)+(j-x)*(deltaB/h))
|
||||
);
|
||||
set(j, i, c);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,93 @@
|
||||
import processing.core.*;
|
||||
|
||||
import java.applet.*;
|
||||
import java.awt.*;
|
||||
import java.awt.image.*;
|
||||
import java.awt.event.*;
|
||||
import java.io.*;
|
||||
import java.net.*;
|
||||
import java.text.*;
|
||||
import java.util.*;
|
||||
import java.util.zip.*;
|
||||
import java.util.regex.*;
|
||||
|
||||
public class LinearGradient extends PApplet {
|
||||
|
||||
/**
|
||||
* Simple Linear Gradient
|
||||
* by Ira Greenberg.
|
||||
*
|
||||
* Using the convenient red(), green()
|
||||
* and blue() component functions,
|
||||
* generate some linear gradients.
|
||||
*/
|
||||
|
||||
// constants
|
||||
int Y_AXIS = 1;
|
||||
int X_AXIS = 2;
|
||||
|
||||
public void setup(){
|
||||
size(200, 200);
|
||||
|
||||
// create some gradients
|
||||
// background
|
||||
int b1 = color(190, 190, 190);
|
||||
int b2 = color(20, 20, 20);
|
||||
setGradient(0, 0, width, height, b1, b2, Y_AXIS);
|
||||
//center squares
|
||||
int c1 = color(255, 120, 0);
|
||||
int c2 = color(10, 45, 255);
|
||||
int c3 = color(10, 255, 15);
|
||||
int c4 = color(125, 2, 140);
|
||||
int c5 = color(255, 255, 0);
|
||||
int c6 = color(25, 255, 200);
|
||||
setGradient(25, 25, 75, 75, c1, c2, Y_AXIS);
|
||||
setGradient(100, 25, 75, 75, c3, c4, X_AXIS);
|
||||
setGradient(25, 100, 75, 75, c2, c5, X_AXIS);
|
||||
setGradient(100, 100, 75, 75, c4, c6, Y_AXIS);
|
||||
}
|
||||
|
||||
public void setGradient(int x, int y, float w, float h, int c1, int c2, int axis ){
|
||||
// calculate differences between color components
|
||||
float deltaR = red(c2)-red(c1);
|
||||
float deltaG = green(c2)-green(c1);
|
||||
float deltaB = blue(c2)-blue(c1);
|
||||
|
||||
// choose axis
|
||||
if(axis == Y_AXIS){
|
||||
/*nested for loops set pixels
|
||||
in a basic table structure */
|
||||
// column
|
||||
for (int i=x; i<=(x+w); i++){
|
||||
// row
|
||||
for (int j = y; j<=(y+h); j++){
|
||||
int c = color(
|
||||
(red(c1)+(j-y)*(deltaR/h)),
|
||||
(green(c1)+(j-y)*(deltaG/h)),
|
||||
(blue(c1)+(j-y)*(deltaB/h))
|
||||
);
|
||||
set(i, j, c);
|
||||
}
|
||||
}
|
||||
}
|
||||
else if(axis == X_AXIS){
|
||||
// column
|
||||
for (int i=y; i<=(y+h); i++){
|
||||
// row
|
||||
for (int j = x; j<=(x+w); j++){
|
||||
int c = color(
|
||||
(red(c1)+(j-x)*(deltaR/h)),
|
||||
(green(c1)+(j-x)*(deltaG/h)),
|
||||
(blue(c1)+(j-x)*(deltaB/h))
|
||||
);
|
||||
set(j, i, c);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static public void main(String args[]) {
|
||||
PApplet.main(new String[] { "LinearGradient" });
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,73 @@
|
||||
/**
|
||||
* Simple Linear Gradient
|
||||
* by Ira Greenberg.
|
||||
*
|
||||
* Using the convenient red(), green()
|
||||
* and blue() component functions,
|
||||
* generate some linear gradients.
|
||||
*/
|
||||
|
||||
// constants
|
||||
int Y_AXIS = 1;
|
||||
int X_AXIS = 2;
|
||||
|
||||
void setup(){
|
||||
size(200, 200);
|
||||
|
||||
// create some gradients
|
||||
// background
|
||||
color b1 = color(190, 190, 190);
|
||||
color b2 = color(20, 20, 20);
|
||||
setGradient(0, 0, width, height, b1, b2, Y_AXIS);
|
||||
//center squares
|
||||
color c1 = color(255, 120, 0);
|
||||
color c2 = color(10, 45, 255);
|
||||
color c3 = color(10, 255, 15);
|
||||
color c4 = color(125, 2, 140);
|
||||
color c5 = color(255, 255, 0);
|
||||
color c6 = color(25, 255, 200);
|
||||
setGradient(25, 25, 75, 75, c1, c2, Y_AXIS);
|
||||
setGradient(100, 25, 75, 75, c3, c4, X_AXIS);
|
||||
setGradient(25, 100, 75, 75, c2, c5, X_AXIS);
|
||||
setGradient(100, 100, 75, 75, c4, c6, Y_AXIS);
|
||||
}
|
||||
|
||||
void setGradient(int x, int y, float w, float h, color c1, color c2, int axis ){
|
||||
// calculate differences between color components
|
||||
float deltaR = red(c2)-red(c1);
|
||||
float deltaG = green(c2)-green(c1);
|
||||
float deltaB = blue(c2)-blue(c1);
|
||||
|
||||
// choose axis
|
||||
if(axis == Y_AXIS){
|
||||
/*nested for loops set pixels
|
||||
in a basic table structure */
|
||||
// column
|
||||
for (int i=x; i<=(x+w); i++){
|
||||
// row
|
||||
for (int j = y; j<=(y+h); j++){
|
||||
color c = color(
|
||||
(red(c1)+(j-y)*(deltaR/h)),
|
||||
(green(c1)+(j-y)*(deltaG/h)),
|
||||
(blue(c1)+(j-y)*(deltaB/h))
|
||||
);
|
||||
set(i, j, c);
|
||||
}
|
||||
}
|
||||
}
|
||||
else if(axis == X_AXIS){
|
||||
// column
|
||||
for (int i=y; i<=(y+h); i++){
|
||||
// row
|
||||
for (int j = x; j<=(x+w); j++){
|
||||
color c = color(
|
||||
(red(c1)+(j-x)*(deltaR/h)),
|
||||
(green(c1)+(j-x)*(deltaG/h)),
|
||||
(blue(c1)+(j-x)*(deltaB/h))
|
||||
);
|
||||
set(j, i, c);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
After Width: | Height: | Size: 2.2 KiB |
@@ -0,0 +1,58 @@
|
||||
/**
|
||||
* Simple Radial Gradient
|
||||
* by Ira Greenberg.
|
||||
*
|
||||
* Using the convenient red(), green()
|
||||
* and blue() component functions,
|
||||
* generate an array of radial gradients.
|
||||
*/
|
||||
|
||||
void setup(){
|
||||
size(200, 200);
|
||||
background(0);
|
||||
smooth();
|
||||
|
||||
// create a simple table of gradients
|
||||
int columns = 4;
|
||||
int radius = (width/columns)/2;
|
||||
// create some gradients
|
||||
for (int i=radius; i< width; i+=radius*2){
|
||||
for (int j =radius; j< height; j+=radius*2){
|
||||
createGradient(i, j, radius,
|
||||
color(int(random(255)), int(random(255)), int(random(255))),
|
||||
color(int(random(255)), int(random(255)), int(random(255))));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void createGradient (float x, float y, float radius, color c1, color c2){
|
||||
float px = 0, py = 0, angle = 0;
|
||||
|
||||
// calculate differences between color components
|
||||
float deltaR = red(c2)-red(c1);
|
||||
float deltaG = green(c2)-green(c1);
|
||||
float deltaB = blue(c2)-blue(c1);
|
||||
// hack to ensure there are no holes in gradient
|
||||
// needs to be increased, as radius increases
|
||||
float gapFiller = 8.0;
|
||||
|
||||
for (int i=0; i< radius; i++){
|
||||
for (float j=0; j<360; j+=1.0/gapFiller){
|
||||
px = x+cos(radians(angle))*i;
|
||||
py = y+sin(radians(angle))*i;
|
||||
angle+=1.0/gapFiller;
|
||||
color c = color(
|
||||
(red(c1)+(i)*(deltaR/radius)),
|
||||
(green(c1)+(i)*(deltaG/radius)),
|
||||
(blue(c1)+(i)*(deltaB/radius))
|
||||
);
|
||||
set(int(px), int(py), c);
|
||||
}
|
||||
}
|
||||
// adds smooth edge
|
||||
// hack anti-aliasing
|
||||
noFill();
|
||||
strokeWeight(3);
|
||||
ellipse(x, y, radius*2, radius*2);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,78 @@
|
||||
import processing.core.*;
|
||||
|
||||
import java.applet.*;
|
||||
import java.awt.*;
|
||||
import java.awt.image.*;
|
||||
import java.awt.event.*;
|
||||
import java.io.*;
|
||||
import java.net.*;
|
||||
import java.text.*;
|
||||
import java.util.*;
|
||||
import java.util.zip.*;
|
||||
import java.util.regex.*;
|
||||
|
||||
public class RadialGradient extends PApplet {
|
||||
|
||||
/**
|
||||
* Simple Radial Gradient
|
||||
* by Ira Greenberg.
|
||||
*
|
||||
* Using the convenient red(), green()
|
||||
* and blue() component functions,
|
||||
* generate an array of radial gradients.
|
||||
*/
|
||||
|
||||
public void setup(){
|
||||
size(200, 200);
|
||||
background(0);
|
||||
smooth();
|
||||
|
||||
// create a simple table of gradients
|
||||
int columns = 4;
|
||||
int radius = (width/columns)/2;
|
||||
// create some gradients
|
||||
for (int i=radius; i< width; i+=radius*2){
|
||||
for (int j =radius; j< height; j+=radius*2){
|
||||
createGradient(i, j, radius,
|
||||
color(PApplet.parseInt(random(255)), PApplet.parseInt(random(255)), PApplet.parseInt(random(255))),
|
||||
color(PApplet.parseInt(random(255)), PApplet.parseInt(random(255)), PApplet.parseInt(random(255))));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void createGradient (float x, float y, float radius, int c1, int c2){
|
||||
float px = 0, py = 0, angle = 0;
|
||||
|
||||
// calculate differences between color components
|
||||
float deltaR = red(c2)-red(c1);
|
||||
float deltaG = green(c2)-green(c1);
|
||||
float deltaB = blue(c2)-blue(c1);
|
||||
// hack to ensure there are no holes in gradient
|
||||
// needs to be increased, as radius increases
|
||||
float gapFiller = 8.0f;
|
||||
|
||||
for (int i=0; i< radius; i++){
|
||||
for (float j=0; j<360; j+=1.0f/gapFiller){
|
||||
px = x+cos(radians(angle))*i;
|
||||
py = y+sin(radians(angle))*i;
|
||||
angle+=1.0f/gapFiller;
|
||||
int c = color(
|
||||
(red(c1)+(i)*(deltaR/radius)),
|
||||
(green(c1)+(i)*(deltaG/radius)),
|
||||
(blue(c1)+(i)*(deltaB/radius))
|
||||
);
|
||||
set(PApplet.parseInt(px), PApplet.parseInt(py), c);
|
||||
}
|
||||
}
|
||||
// adds smooth edge
|
||||
// hack anti-aliasing
|
||||
noFill();
|
||||
strokeWeight(3);
|
||||
ellipse(x, y, radius*2, radius*2);
|
||||
}
|
||||
|
||||
|
||||
static public void main(String args[]) {
|
||||
PApplet.main(new String[] { "RadialGradient" });
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
/**
|
||||
* Simple Radial Gradient
|
||||
* by Ira Greenberg.
|
||||
*
|
||||
* Using the convenient red(), green()
|
||||
* and blue() component functions,
|
||||
* generate an array of radial gradients.
|
||||
*/
|
||||
|
||||
void setup(){
|
||||
size(200, 200);
|
||||
background(0);
|
||||
smooth();
|
||||
|
||||
// create a simple table of gradients
|
||||
int columns = 4;
|
||||
int radius = (width/columns)/2;
|
||||
// create some gradients
|
||||
for (int i=radius; i< width; i+=radius*2){
|
||||
for (int j =radius; j< height; j+=radius*2){
|
||||
createGradient(i, j, radius,
|
||||
color(int(random(255)), int(random(255)), int(random(255))),
|
||||
color(int(random(255)), int(random(255)), int(random(255))));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void createGradient (float x, float y, float radius, color c1, color c2){
|
||||
float px = 0, py = 0, angle = 0;
|
||||
|
||||
// calculate differences between color components
|
||||
float deltaR = red(c2)-red(c1);
|
||||
float deltaG = green(c2)-green(c1);
|
||||
float deltaB = blue(c2)-blue(c1);
|
||||
// hack to ensure there are no holes in gradient
|
||||
// needs to be increased, as radius increases
|
||||
float gapFiller = 8.0;
|
||||
|
||||
for (int i=0; i< radius; i++){
|
||||
for (float j=0; j<360; j+=1.0/gapFiller){
|
||||
px = x+cos(radians(angle))*i;
|
||||
py = y+sin(radians(angle))*i;
|
||||
angle+=1.0/gapFiller;
|
||||
color c = color(
|
||||
(red(c1)+(i)*(deltaR/radius)),
|
||||
(green(c1)+(i)*(deltaG/radius)),
|
||||
(blue(c1)+(i)*(deltaB/radius))
|
||||
);
|
||||
set(int(px), int(py), c);
|
||||
}
|
||||
}
|
||||
// adds smooth edge
|
||||
// hack anti-aliasing
|
||||
noFill();
|
||||
strokeWeight(3);
|
||||
ellipse(x, y, radius*2, radius*2);
|
||||
}
|
||||
|
||||
|
After Width: | Height: | Size: 2.2 KiB |
@@ -0,0 +1,43 @@
|
||||
/**
|
||||
* Inspired by Ira Greenberg's RadialGradient sketch,
|
||||
* but uses a different method for the gradients.
|
||||
*/
|
||||
|
||||
int dim = 40;
|
||||
|
||||
void setup() {
|
||||
size(200, 200);
|
||||
background(0);
|
||||
smooth();
|
||||
noStroke();
|
||||
ellipseMode(RADIUS);
|
||||
|
||||
// create a simple table of gradients
|
||||
int rows = height / dim;
|
||||
int cols = width / dim;
|
||||
|
||||
for (int row = 0; row < rows; row++) {
|
||||
for (int col = 0; col < cols; col++) {
|
||||
drawGradient(col*dim + dim/2, row*dim + dim/2);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void drawGradient(float x, float y) {
|
||||
int radius = dim/2 - 2;
|
||||
float r1 = random(255);
|
||||
float g1 = random(255);
|
||||
float b1 = random(255);
|
||||
float dr = (random(255) - r1) / radius;
|
||||
float dg = (random(255) - g1) / radius;
|
||||
float db = (random(255) - b1) / radius;
|
||||
|
||||
for (int r = radius; r > 0; --r) {
|
||||
fill(r1, g1, b1);
|
||||
ellipse(x, y, r, r);
|
||||
r1 += dr;
|
||||
g1 += dg;
|
||||
b1 += db;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,46 @@
|
||||
/**
|
||||
* Reading.
|
||||
*
|
||||
* An image is recreated from its individual component colors.
|
||||
* The many colors of the image are created through modulating the
|
||||
* red, green, and blue values. This is an exageration of an LCD display.
|
||||
*/
|
||||
|
||||
size(200, 200);
|
||||
noStroke();
|
||||
background(0);
|
||||
|
||||
// Load an image from the data directory
|
||||
PImage img = loadImage("cait.jpg");
|
||||
img.loadPixels();
|
||||
|
||||
// figure out how big to make each block based on
|
||||
// the sketch area and the size of the input image
|
||||
int eachW = width / img.width;
|
||||
int eachH = height / img.height;
|
||||
int each = min(eachW, eachH);
|
||||
// vertical stripes will be a third as wide
|
||||
int stripeW = each / 3;
|
||||
// make sure the block size is a multiple of 3
|
||||
each = 3 * stripeW;
|
||||
|
||||
int left = (width - (img.width * each)) / 2;
|
||||
int top = (height - (img.height * each)) / 2;
|
||||
|
||||
for (int y = 0; y < img.height; y++) {
|
||||
int y1 = top + y*each;
|
||||
|
||||
for (int x = 0; x < img.width; x++) {
|
||||
int pixel = img.get(x, y);
|
||||
int x1 = left + x*each;
|
||||
|
||||
fill(red(pixel), 0, 0);
|
||||
rect(x1 + stripeW*0, y1, stripeW, each);
|
||||
|
||||
fill(0, green(pixel), 0);
|
||||
rect(x1 + stripeW*1, y1, stripeW, each);
|
||||
|
||||
fill(0, 0, blue(pixel));
|
||||
rect(x1 + stripeW*2, y1, stripeW, each);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
import processing.core.*;
|
||||
|
||||
import java.applet.*;
|
||||
import java.awt.*;
|
||||
import java.awt.image.*;
|
||||
import java.awt.event.*;
|
||||
import java.io.*;
|
||||
import java.net.*;
|
||||
import java.text.*;
|
||||
import java.util.*;
|
||||
import java.util.zip.*;
|
||||
import java.util.regex.*;
|
||||
|
||||
public class Reading extends PApplet {
|
||||
public void setup() {/**
|
||||
* Reading.
|
||||
*
|
||||
* An image is recreated from its individual component colors.
|
||||
* The many colors of the image are created through modulating the
|
||||
* red, green, and blue values. This is an exageration of an LCD display.
|
||||
*/
|
||||
|
||||
size(200, 200);
|
||||
noStroke();
|
||||
background(0);
|
||||
|
||||
// Load an image from the data directory
|
||||
PImage c;
|
||||
c = loadImage("cait.jpg");
|
||||
|
||||
int xoff = 0;
|
||||
int yoff = 0;
|
||||
int p = 2;
|
||||
int pix = p*3;
|
||||
|
||||
|
||||
for(int i = 0; i < c.width*c.height; i += 1)
|
||||
{
|
||||
int here = c.pixels[i];
|
||||
|
||||
fill(red(here), 0, 0);
|
||||
rect(xoff, yoff, p, pix);
|
||||
|
||||
fill(0, green(here), 0);
|
||||
rect(xoff+p, yoff, p, pix);
|
||||
|
||||
fill(0, 0, blue(here));
|
||||
rect(xoff+p*2, yoff, p, pix);
|
||||
|
||||
xoff+=pix;
|
||||
if(xoff >= width-pix) {
|
||||
xoff = 0;
|
||||
yoff += pix;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
noLoop();
|
||||
}
|
||||
static public void main(String args[]) {
|
||||
PApplet.main(new String[] { "Reading" });
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
/**
|
||||
* Reading.
|
||||
*
|
||||
* An image is recreated from its individual component colors.
|
||||
* The many colors of the image are created through modulating the
|
||||
* red, green, and blue values. This is an exageration of an LCD display.
|
||||
*/
|
||||
|
||||
size(200, 200);
|
||||
noStroke();
|
||||
background(0);
|
||||
|
||||
// Load an image from the data directory
|
||||
PImage c;
|
||||
c = loadImage("cait.jpg");
|
||||
|
||||
int xoff = 0;
|
||||
int yoff = 0;
|
||||
int p = 2;
|
||||
int pix = p*3;
|
||||
|
||||
|
||||
for(int i = 0; i < c.width*c.height; i += 1)
|
||||
{
|
||||
int here = c.pixels[i];
|
||||
|
||||
fill(red(here), 0, 0);
|
||||
rect(xoff, yoff, p, pix);
|
||||
|
||||
fill(0, green(here), 0);
|
||||
rect(xoff+p, yoff, p, pix);
|
||||
|
||||
fill(0, 0, blue(here));
|
||||
rect(xoff+p*2, yoff, p, pix);
|
||||
|
||||
xoff+=pix;
|
||||
if(xoff >= width-pix) {
|
||||
xoff = 0;
|
||||
yoff += pix;
|
||||
}
|
||||
}
|
||||
|
||||
|
After Width: | Height: | Size: 2.2 KiB |
@@ -0,0 +1,42 @@
|
||||
/**
|
||||
* Relativity.
|
||||
*
|
||||
* Each color is perceived in relation to other colors.
|
||||
* The top and bottom bars each contain the same component colors,
|
||||
* but a different display order causes individual colors to appear differently.
|
||||
*/
|
||||
|
||||
color a, b, c, d, e;
|
||||
|
||||
void setup() {
|
||||
size(200, 200);
|
||||
noStroke();
|
||||
a = color(165, 167, 20);
|
||||
b = color(77, 86, 59);
|
||||
c = color(42, 106, 105);
|
||||
d = color(165, 89, 20);
|
||||
e = color(146, 150, 127);
|
||||
noLoop();
|
||||
}
|
||||
|
||||
void draw() {
|
||||
drawBand(a, b, c, d, e, 0, width/50);
|
||||
drawBand(c, a, d, b, e, height/2, width/50);
|
||||
}
|
||||
|
||||
void drawBand(color v, color w, color x, color y, color z, int ypos, int barWidth) {
|
||||
int num = 5;
|
||||
color[] colorOrder = { v, w, x, y, z };
|
||||
for(int i = 0; i < width; i += barWidth*num) {
|
||||
for(int j = 0; j < num; j++) {
|
||||
fill(colorOrder[j]);
|
||||
rect(i+j*barWidth, ypos, barWidth, height/2);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,62 @@
|
||||
import processing.core.*;
|
||||
|
||||
import java.applet.*;
|
||||
import java.awt.*;
|
||||
import java.awt.image.*;
|
||||
import java.awt.event.*;
|
||||
import java.io.*;
|
||||
import java.net.*;
|
||||
import java.text.*;
|
||||
import java.util.*;
|
||||
import java.util.zip.*;
|
||||
import java.util.regex.*;
|
||||
|
||||
public class Relativity extends PApplet {
|
||||
|
||||
/**
|
||||
* Relativity.
|
||||
*
|
||||
* Each color is perceived in relation to other colors.
|
||||
* The top and bottom bars each contain the same component colors,
|
||||
* but a different display order causes individual colors to appear differently.
|
||||
*/
|
||||
|
||||
int a, b, c, d, e;
|
||||
|
||||
public void setup() {
|
||||
size(200, 200);
|
||||
noStroke();
|
||||
a = color(165, 167, 20);
|
||||
b = color(77, 86, 59);
|
||||
c = color(42, 106, 105);
|
||||
d = color(165, 89, 20);
|
||||
e = color(146, 150, 127);
|
||||
noLoop();
|
||||
}
|
||||
|
||||
public void draw() {
|
||||
drawBand(a, b, c, d, e, 0, 4);
|
||||
drawBand(c, a, d, b, e, height/2, 4);
|
||||
}
|
||||
|
||||
public void drawBand(int v, int w, int x, int y, int z, int ypos, int barWidth) {
|
||||
int num = 5;
|
||||
int[] colorOrder = { v, w, x, y, z };
|
||||
for(int i = 0; i < width; i += barWidth*num) {
|
||||
for(int j = 0; j < num; j++) {
|
||||
fill(colorOrder[j]);
|
||||
rect(i+j*barWidth, ypos, barWidth, height/2);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
static public void main(String args[]) {
|
||||
PApplet.main(new String[] { "Relativity" });
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
/**
|
||||
* Relativity.
|
||||
*
|
||||
* Each color is perceived in relation to other colors.
|
||||
* The top and bottom bars each contain the same component colors,
|
||||
* but a different display order causes individual colors to appear differently.
|
||||
*/
|
||||
|
||||
color a, b, c, d, e;
|
||||
|
||||
void setup() {
|
||||
size(200, 200);
|
||||
noStroke();
|
||||
a = color(165, 167, 20);
|
||||
b = color(77, 86, 59);
|
||||
c = color(42, 106, 105);
|
||||
d = color(165, 89, 20);
|
||||
e = color(146, 150, 127);
|
||||
noLoop();
|
||||
}
|
||||
|
||||
void draw() {
|
||||
drawBand(a, b, c, d, e, 0, 4);
|
||||
drawBand(c, a, d, b, e, height/2, 4);
|
||||
}
|
||||
|
||||
void drawBand(color v, color w, color x, color y, color z, int ypos, int barWidth) {
|
||||
int num = 5;
|
||||
color[] colorOrder = { v, w, x, y, z };
|
||||
for(int i = 0; i < width; i += barWidth*num) {
|
||||
for(int j = 0; j < num; j++) {
|
||||
fill(colorOrder[j]);
|
||||
rect(i+j*barWidth, ypos, barWidth, height/2);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
After Width: | Height: | Size: 2.2 KiB |
@@ -0,0 +1,29 @@
|
||||
/**
|
||||
* Saturation.
|
||||
*
|
||||
* Saturation is the strength or purity of the color and represents the
|
||||
* amount of gray in proportion to the hue. A "saturated" color is pure
|
||||
* and an "unsaturated" color has a large percentage of gray.
|
||||
* Move the cursor vertically over each bar to alter its saturation.
|
||||
*/
|
||||
|
||||
int barWidth = 5;
|
||||
int lastBar = -1;
|
||||
|
||||
|
||||
void setup() {
|
||||
size(200, 200);
|
||||
colorMode(HSB, width, height, 100);
|
||||
noStroke();
|
||||
}
|
||||
|
||||
|
||||
void draw() {
|
||||
int whichBar = mouseX / barWidth;
|
||||
if (whichBar != lastBar) {
|
||||
int barX = whichBar * barWidth;
|
||||
fill(barX, mouseY, 66);
|
||||
rect(barX, 0, barWidth, height);
|
||||
lastBar = whichBar;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
import processing.core.*;
|
||||
|
||||
import java.applet.*;
|
||||
import java.awt.*;
|
||||
import java.awt.image.*;
|
||||
import java.awt.event.*;
|
||||
import java.io.*;
|
||||
import java.net.*;
|
||||
import java.text.*;
|
||||
import java.util.*;
|
||||
import java.util.zip.*;
|
||||
import java.util.regex.*;
|
||||
|
||||
public class Saturation extends PApplet {
|
||||
|
||||
/**
|
||||
* Saturation.
|
||||
*
|
||||
* Saturation is the strength or purity of the color and represents the
|
||||
* amount of gray in proportion to the hue. A "saturated" color is pure
|
||||
* and an "unsaturated" color has a large percentage of gray.
|
||||
* Move the cursor vertically over each bar to alter its saturation.
|
||||
*/
|
||||
|
||||
int barWidth = 5;
|
||||
int[] saturation;
|
||||
|
||||
public void setup()
|
||||
{
|
||||
size(200, 200);
|
||||
colorMode(HSB, 360, height, height);
|
||||
saturation = new int[width/barWidth];
|
||||
}
|
||||
|
||||
public void draw()
|
||||
{
|
||||
int j = 0;
|
||||
for (int i=0; i<=(width-barWidth); i+=barWidth) {
|
||||
noStroke();
|
||||
if ((mouseX > i) && (mouseX < i+barWidth)) {
|
||||
saturation[j] = mouseY;
|
||||
}
|
||||
fill(i, saturation[j], height/1.5f);
|
||||
rect(i, 0, barWidth, height);
|
||||
j++;
|
||||
}
|
||||
}
|
||||
|
||||
static public void main(String args[]) {
|
||||
PApplet.main(new String[] { "Saturation" });
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
/**
|
||||
* Saturation.
|
||||
*
|
||||
* Saturation is the strength or purity of the color and represents the
|
||||
* amount of gray in proportion to the hue. A "saturated" color is pure
|
||||
* and an "unsaturated" color has a large percentage of gray.
|
||||
* Move the cursor vertically over each bar to alter its saturation.
|
||||
*/
|
||||
|
||||
int barWidth = 5;
|
||||
int[] saturation;
|
||||
|
||||
void setup()
|
||||
{
|
||||
size(200, 200);
|
||||
colorMode(HSB, 360, height, height);
|
||||
saturation = new int[width/barWidth];
|
||||
}
|
||||
|
||||
void draw()
|
||||
{
|
||||
int j = 0;
|
||||
for (int i=0; i<=(width-barWidth); i+=barWidth) {
|
||||
noStroke();
|
||||
if ((mouseX > i) && (mouseX < i+barWidth)) {
|
||||
saturation[j] = mouseY;
|
||||
}
|
||||
fill(i, saturation[j], height/1.5);
|
||||
rect(i, 0, barWidth, height);
|
||||
j++;
|
||||
}
|
||||
}
|
||||
|
After Width: | Height: | Size: 2.2 KiB |
@@ -0,0 +1,39 @@
|
||||
/**
|
||||
* Wave Gradient
|
||||
* by Ira Greenberg.
|
||||
*
|
||||
* Generate a gradient along a sin() wave.
|
||||
*/
|
||||
|
||||
float angle = 0;
|
||||
float px = 0, py = 0;
|
||||
float amplitude = 30;
|
||||
float frequency = 0;
|
||||
float fillGap = 2.5;
|
||||
color c;
|
||||
|
||||
void setup() {
|
||||
size(200, 200);
|
||||
background(200,200,200);
|
||||
noLoop();
|
||||
}
|
||||
|
||||
void draw() {
|
||||
for (int i =- 75; i < height+75; i++){
|
||||
// Reset angle to 0, so waves stack properly
|
||||
angle = 0;
|
||||
// Increasing frequency causes more gaps
|
||||
frequency+=.006;
|
||||
for (float j=0; j<width+75; j++){
|
||||
py = i+sin(radians(angle))*amplitude;
|
||||
angle+=frequency;
|
||||
c = color(abs(py-i)*255/amplitude, 255-abs(py-i)*255/amplitude, j*(255.0/(width+50)));
|
||||
// Hack to fill gaps. Raise value of fillGap if you increase frequency
|
||||
for (int filler = 0; filler<fillGap; filler++){
|
||||
set(int(j-filler), int(py)-filler, c);
|
||||
set(int(j), int(py), c);
|
||||
set(int(j+filler), int(py)+filler, c);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
import processing.core.*;
|
||||
|
||||
import java.applet.*;
|
||||
import java.awt.*;
|
||||
import java.awt.image.*;
|
||||
import java.awt.event.*;
|
||||
import java.io.*;
|
||||
import java.net.*;
|
||||
import java.text.*;
|
||||
import java.util.*;
|
||||
import java.util.zip.*;
|
||||
import java.util.regex.*;
|
||||
|
||||
public class WaveGradient extends PApplet {
|
||||
|
||||
/**
|
||||
* Wave Gradient
|
||||
* by Ira Greenberg.
|
||||
*
|
||||
* Generate a gradient along a sin() wave.
|
||||
*/
|
||||
|
||||
float angle = 0;
|
||||
float px = 0, py = 0;
|
||||
float amplitude = 30;
|
||||
float frequency = 0;
|
||||
float fillGap = 2.5f;
|
||||
int c;
|
||||
|
||||
public void setup() {
|
||||
size(200, 200);
|
||||
background(200,200,200);
|
||||
noLoop();
|
||||
}
|
||||
|
||||
public void draw() {
|
||||
for (int i =- 75; i < height+75; i++){
|
||||
// Reset angle to 0, so waves stack properly
|
||||
angle = 0;
|
||||
// Increasing frequency causes more gaps
|
||||
frequency+=.006f;
|
||||
for (float j=0; j<width+75; j++){
|
||||
py = i+sin(radians(angle))*amplitude;
|
||||
angle+=frequency;
|
||||
c = color(abs(py-i)*255/amplitude, 255-abs(py-i)*255/amplitude, j*(255.0f/(width+50)));
|
||||
// Hack to fill gaps. Raise value of fillGap if you increase frequency
|
||||
for (int filler = 0; filler<fillGap; filler++){
|
||||
set(PApplet.parseInt(j-filler), PApplet.parseInt(py)-filler, c);
|
||||
set(PApplet.parseInt(j), PApplet.parseInt(py), c);
|
||||
set(PApplet.parseInt(j+filler), PApplet.parseInt(py)+filler, c);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static public void main(String args[]) {
|
||||
PApplet.main(new String[] { "WaveGradient" });
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
/**
|
||||
* Wave Gradient
|
||||
* by Ira Greenberg.
|
||||
*
|
||||
* Generate a gradient along a sin() wave.
|
||||
*/
|
||||
|
||||
float angle = 0;
|
||||
float px = 0, py = 0;
|
||||
float amplitude = 30;
|
||||
float frequency = 0;
|
||||
float fillGap = 2.5;
|
||||
color c;
|
||||
|
||||
void setup() {
|
||||
size(200, 200);
|
||||
background(200,200,200);
|
||||
noLoop();
|
||||
}
|
||||
|
||||
void draw() {
|
||||
for (int i =- 75; i < height+75; i++){
|
||||
// Reset angle to 0, so waves stack properly
|
||||
angle = 0;
|
||||
// Increasing frequency causes more gaps
|
||||
frequency+=.006;
|
||||
for (float j=0; j<width+75; j++){
|
||||
py = i+sin(radians(angle))*amplitude;
|
||||
angle+=frequency;
|
||||
c = color(abs(py-i)*255/amplitude, 255-abs(py-i)*255/amplitude, j*(255.0/(width+50)));
|
||||
// Hack to fill gaps. Raise value of fillGap if you increase frequency
|
||||
for (int filler = 0; filler<fillGap; filler++){
|
||||
set(int(j-filler), int(py)-filler, c);
|
||||
set(int(j), int(py), c);
|
||||
set(int(j+filler), int(py)+filler, c);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
After Width: | Height: | Size: 2.2 KiB |
@@ -0,0 +1,26 @@
|
||||
/**
|
||||
* Conditionals 1.
|
||||
*
|
||||
* Conditions are like questions.
|
||||
* They allow a program to decide to take one action if
|
||||
* the answer to a question is true or to do another action
|
||||
* if the answer to the question is false.
|
||||
* The questions asked within a program are always logical
|
||||
* or relational statements. For example, if the variable 'i' is
|
||||
* equal to zero then draw a line.
|
||||
*/
|
||||
|
||||
size(200, 200);
|
||||
background(0);
|
||||
|
||||
for(int i=10; i<width; i+=10) {
|
||||
// If 'i' divides by 20 with no remainder draw the first line
|
||||
// else draw the second line
|
||||
if(i%20 == 0) {
|
||||
stroke(153);
|
||||
line(i, 40, i, height/2);
|
||||
} else {
|
||||
stroke(102);
|
||||
line(i, 20, i, 180);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
import processing.core.*;
|
||||
|
||||
import java.applet.*;
|
||||
import java.awt.*;
|
||||
import java.awt.image.*;
|
||||
import java.awt.event.*;
|
||||
import java.io.*;
|
||||
import java.net.*;
|
||||
import java.text.*;
|
||||
import java.util.*;
|
||||
import java.util.zip.*;
|
||||
import java.util.regex.*;
|
||||
|
||||
public class Conditionals1 extends PApplet {
|
||||
public void setup() {/**
|
||||
* Conditionals 1.
|
||||
*
|
||||
* Conditions are like questions.
|
||||
* They allow a program to decide to take one action if
|
||||
* the answer to a question is true or to do another action
|
||||
* if the answer to the question is false.
|
||||
* The questions asked within a program are always logical
|
||||
* or relational statements. For example, if the variable 'i' is
|
||||
* equal to zero then draw a line.
|
||||
*/
|
||||
|
||||
size(200, 200);
|
||||
background(0);
|
||||
|
||||
for(int i=10; i<width; i+=10) {
|
||||
// If 'i' divides by 20 with no remainder draw the first line
|
||||
// else draw the second line
|
||||
if(i%20 == 0) {
|
||||
stroke(153);
|
||||
line(i, 40, i, height/2);
|
||||
} else {
|
||||
stroke(102);
|
||||
line(i, 20, i, 180);
|
||||
}
|
||||
}
|
||||
|
||||
noLoop();
|
||||
}
|
||||
static public void main(String args[]) {
|
||||
PApplet.main(new String[] { "Conditionals1" });
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
/**
|
||||
* Conditionals 1.
|
||||
*
|
||||
* Conditions are like questions.
|
||||
* They allow a program to decide to take one action if
|
||||
* the answer to a question is true or to do another action
|
||||
* if the answer to the question is false.
|
||||
* The questions asked within a program are always logical
|
||||
* or relational statements. For example, if the variable 'i' is
|
||||
* equal to zero then draw a line.
|
||||
*/
|
||||
|
||||
size(200, 200);
|
||||
background(0);
|
||||
|
||||
for(int i=10; i<width; i+=10) {
|
||||
// If 'i' divides by 20 with no remainder draw the first line
|
||||
// else draw the second line
|
||||
if(i%20 == 0) {
|
||||
stroke(153);
|
||||
line(i, 40, i, height/2);
|
||||
} else {
|
||||
stroke(102);
|
||||
line(i, 20, i, 180);
|
||||
}
|
||||
}
|
||||
|
After Width: | Height: | Size: 2.2 KiB |
@@ -0,0 +1,26 @@
|
||||
/**
|
||||
* Conditionals 2.
|
||||
*
|
||||
* We extend the language of conditionals by adding the
|
||||
* keyword "else". This allows conditionals to ask
|
||||
* two or more sequential questions, each with a different
|
||||
* action.
|
||||
*/
|
||||
|
||||
size(200, 200);
|
||||
background(0);
|
||||
|
||||
for(int i=2; i<width-2; i+=2) {
|
||||
// If 'i' divides by 20 with no remainder
|
||||
// draw the first line else draw the second line
|
||||
if(i%20 == 0) {
|
||||
stroke(255);
|
||||
line(i, 40, i, height/2);
|
||||
} else if (i%10 == 0) {
|
||||
stroke(153);
|
||||
line(i, 20, i, 180);
|
||||
} else {
|
||||
stroke(102);
|
||||
line(i, height/2, i, height-40);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
import processing.core.*;
|
||||
|
||||
import java.applet.*;
|
||||
import java.awt.*;
|
||||
import java.awt.image.*;
|
||||
import java.awt.event.*;
|
||||
import java.io.*;
|
||||
import java.net.*;
|
||||
import java.text.*;
|
||||
import java.util.*;
|
||||
import java.util.zip.*;
|
||||
import java.util.regex.*;
|
||||
|
||||
public class Conditionals2 extends PApplet {
|
||||
public void setup() {/**
|
||||
* Conditionals 2.
|
||||
*
|
||||
* We extend the language of conditionals by adding the
|
||||
* keyword "else". This allows conditionals to ask
|
||||
* two or more sequential questions, each with a different
|
||||
* action.
|
||||
*/
|
||||
|
||||
size(200, 200);
|
||||
background(0);
|
||||
|
||||
for(int i=2; i<width-2; i+=2) {
|
||||
// If 'i' divides by 20 with no remainder
|
||||
// draw the first line else draw the second line
|
||||
if(i%20 == 0) {
|
||||
stroke(255);
|
||||
line(i, 40, i, height/2);
|
||||
} else if (i%10 == 0) {
|
||||
stroke(153);
|
||||
line(i, 20, i, 180);
|
||||
} else {
|
||||
stroke(102);
|
||||
line(i, height/2, i, height-40);
|
||||
}
|
||||
}
|
||||
|
||||
noLoop();
|
||||
}
|
||||
static public void main(String args[]) {
|
||||
PApplet.main(new String[] { "Conditionals2" });
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
/**
|
||||
* Conditionals 2.
|
||||
*
|
||||
* We extend the language of conditionals by adding the
|
||||
* keyword "else". This allows conditionals to ask
|
||||
* two or more sequential questions, each with a different
|
||||
* action.
|
||||
*/
|
||||
|
||||
size(200, 200);
|
||||
background(0);
|
||||
|
||||
for(int i=2; i<width-2; i+=2) {
|
||||
// If 'i' divides by 20 with no remainder
|
||||
// draw the first line else draw the second line
|
||||
if(i%20 == 0) {
|
||||
stroke(255);
|
||||
line(i, 40, i, height/2);
|
||||
} else if (i%10 == 0) {
|
||||
stroke(153);
|
||||
line(i, 20, i, 180);
|
||||
} else {
|
||||
stroke(102);
|
||||
line(i, height/2, i, height-40);
|
||||
}
|
||||
}
|
||||
|
After Width: | Height: | Size: 2.2 KiB |
@@ -0,0 +1,27 @@
|
||||
/**
|
||||
* Embedding Iteration.
|
||||
*
|
||||
* Embedding "for" structures allows repetition in two dimensions.
|
||||
*/
|
||||
|
||||
float box_size = 11;
|
||||
float box_space = 12;
|
||||
int margin = 7;
|
||||
|
||||
size(200, 200);
|
||||
background(0);
|
||||
noStroke();
|
||||
|
||||
// Draw gray boxes
|
||||
|
||||
for (int i = margin; i < height-margin; i += box_space){
|
||||
if(box_size > 0){
|
||||
for(int j = margin; j < width-margin; j+= box_space){
|
||||
fill(255-box_size*10);
|
||||
rect(j, i, box_size, box_size);
|
||||
}
|
||||
box_size = box_size - 0.6;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,48 @@
|
||||
import processing.core.*;
|
||||
|
||||
import java.applet.*;
|
||||
import java.awt.*;
|
||||
import java.awt.image.*;
|
||||
import java.awt.event.*;
|
||||
import java.io.*;
|
||||
import java.net.*;
|
||||
import java.text.*;
|
||||
import java.util.*;
|
||||
import java.util.zip.*;
|
||||
import java.util.regex.*;
|
||||
|
||||
public class EmbeddedIteration extends PApplet {
|
||||
public void setup() {/**
|
||||
* Embedding Iteration.
|
||||
*
|
||||
* Embedding "for" structures allows repetition in two dimensions.
|
||||
*/
|
||||
|
||||
float box_size = 11;
|
||||
float box_space = 12;
|
||||
int margin = 7;
|
||||
|
||||
size(200, 200);
|
||||
background(0);
|
||||
noStroke();
|
||||
|
||||
// Draw gray boxes
|
||||
|
||||
for (int i = margin; i < height-margin; i += box_space){
|
||||
if(box_size > 0){
|
||||
for(int j = margin; j < width-margin; j+= box_space){
|
||||
fill(255-box_size*10);
|
||||
rect(j, i, box_size, box_size);
|
||||
}
|
||||
box_size = box_size - 0.6f;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
noLoop();
|
||||
}
|
||||
static public void main(String args[]) {
|
||||
PApplet.main(new String[] { "EmbeddedIteration" });
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
/**
|
||||
* Embedding Iteration.
|
||||
*
|
||||
* Embedding "for" structures allows repetition in two dimensions.
|
||||
*/
|
||||
|
||||
float box_size = 11;
|
||||
float box_space = 12;
|
||||
int margin = 7;
|
||||
|
||||
size(200, 200);
|
||||
background(0);
|
||||
noStroke();
|
||||
|
||||
// Draw gray boxes
|
||||
|
||||
for (int i = margin; i < height-margin; i += box_space){
|
||||
if(box_size > 0){
|
||||
for(int j = margin; j < width-margin; j+= box_space){
|
||||
fill(255-box_size*10);
|
||||
rect(j, i, box_size, box_size);
|
||||
}
|
||||
box_size = box_size - 0.6;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
After Width: | Height: | Size: 2.2 KiB |
@@ -0,0 +1,45 @@
|
||||
/**
|
||||
* Iteration.
|
||||
*
|
||||
* Iteration with a "for" structure constructs repetitive forms.
|
||||
*/
|
||||
|
||||
int k;
|
||||
int xpos1 = 100;
|
||||
int xpos2 = 118;
|
||||
int count = 0;
|
||||
int timey = 0;
|
||||
int num = 12;
|
||||
|
||||
size(200, 200);
|
||||
background(102);
|
||||
noStroke();
|
||||
|
||||
// Draw gray bars
|
||||
fill(255);
|
||||
k=60;
|
||||
for(int i=0; i < num/3; i++) {
|
||||
rect(25, k, 155, 5);
|
||||
k+=10;
|
||||
}
|
||||
|
||||
// Black bars
|
||||
fill(51);
|
||||
k = 40;
|
||||
for(int i=0; i < num; i++) {
|
||||
rect(105, k, 30, 5);
|
||||
k += 10;
|
||||
}
|
||||
k = 15;
|
||||
for(int i = 0; i < num; i++) {
|
||||
rect(125, k, 30, 5);
|
||||
k +=10;
|
||||
}
|
||||
|
||||
// Thin lines
|
||||
k = 42;
|
||||
fill(0);
|
||||
for(int i=0; i < num-1; i++) {
|
||||
rect(36, k, 20, 1);
|
||||
k+=10;
|
||||
}
|
||||
@@ -0,0 +1,66 @@
|
||||
import processing.core.*;
|
||||
|
||||
import java.applet.*;
|
||||
import java.awt.*;
|
||||
import java.awt.image.*;
|
||||
import java.awt.event.*;
|
||||
import java.io.*;
|
||||
import java.net.*;
|
||||
import java.text.*;
|
||||
import java.util.*;
|
||||
import java.util.zip.*;
|
||||
import java.util.regex.*;
|
||||
|
||||
public class Iteration extends PApplet {
|
||||
public void setup() {/**
|
||||
* Iteration.
|
||||
*
|
||||
* Iteration with a "for" structure constructs repetitive forms.
|
||||
*/
|
||||
|
||||
int k;
|
||||
int xpos1 = 100;
|
||||
int xpos2 = 118;
|
||||
int count = 0;
|
||||
int timey = 0;
|
||||
int num = 12;
|
||||
|
||||
size(200, 200);
|
||||
background(102);
|
||||
noStroke();
|
||||
|
||||
// Draw gray bars
|
||||
fill(255);
|
||||
k=60;
|
||||
for(int i=0; i < num/3; i++) {
|
||||
rect(25, k, 155, 5);
|
||||
k+=10;
|
||||
}
|
||||
|
||||
// Black bars
|
||||
fill(51);
|
||||
k = 40;
|
||||
for(int i=0; i < num; i++) {
|
||||
rect(105, k, 30, 5);
|
||||
k += 10;
|
||||
}
|
||||
k = 15;
|
||||
for(int i = 0; i < num; i++) {
|
||||
rect(125, k, 30, 5);
|
||||
k +=10;
|
||||
}
|
||||
|
||||
// Thin lines
|
||||
k = 42;
|
||||
fill(0);
|
||||
for(int i=0; i < num-1; i++) {
|
||||
rect(36, k, 20, 1);
|
||||
k+=10;
|
||||
}
|
||||
|
||||
noLoop();
|
||||
}
|
||||
static public void main(String args[]) {
|
||||
PApplet.main(new String[] { "Iteration" });
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
/**
|
||||
* Iteration.
|
||||
*
|
||||
* Iteration with a "for" structure constructs repetitive forms.
|
||||
*/
|
||||
|
||||
int k;
|
||||
int xpos1 = 100;
|
||||
int xpos2 = 118;
|
||||
int count = 0;
|
||||
int timey = 0;
|
||||
int num = 12;
|
||||
|
||||
size(200, 200);
|
||||
background(102);
|
||||
noStroke();
|
||||
|
||||
// Draw gray bars
|
||||
fill(255);
|
||||
k=60;
|
||||
for(int i=0; i < num/3; i++) {
|
||||
rect(25, k, 155, 5);
|
||||
k+=10;
|
||||
}
|
||||
|
||||
// Black bars
|
||||
fill(51);
|
||||
k = 40;
|
||||
for(int i=0; i < num; i++) {
|
||||
rect(105, k, 30, 5);
|
||||
k += 10;
|
||||
}
|
||||
k = 15;
|
||||
for(int i = 0; i < num; i++) {
|
||||
rect(125, k, 30, 5);
|
||||
k +=10;
|
||||
}
|
||||
|
||||
// Thin lines
|
||||
k = 42;
|
||||
fill(0);
|
||||
for(int i=0; i < num-1; i++) {
|
||||
rect(36, k, 20, 1);
|
||||
k+=10;
|
||||
}
|
||||
|
After Width: | Height: | Size: 2.2 KiB |
@@ -0,0 +1,45 @@
|
||||
/**
|
||||
* Logical Operators.
|
||||
*
|
||||
* The logical operators for AND (&&) and OR (||) are used to
|
||||
* combine simple relational statements into more complex expressions.
|
||||
* The NOT (!) operator is used to negate a boolean statement.
|
||||
*/
|
||||
|
||||
size(200, 200);
|
||||
background(126);
|
||||
|
||||
boolean op = false;
|
||||
|
||||
for(int i=5; i<=195; i+=5) {
|
||||
// Logical AND
|
||||
stroke(0);
|
||||
if((i > 35) && (i < 100)) {
|
||||
line(5, i, 95, i);
|
||||
op = false;
|
||||
}
|
||||
|
||||
// Logical OR
|
||||
stroke(76);
|
||||
if((i <= 35) || (i >= 100)) {
|
||||
line(105, i, 195, i);
|
||||
op = true;
|
||||
}
|
||||
|
||||
// Testing if a boolean value is "true"
|
||||
// The expression "if(op)" is equivalent to "if(op == true)"
|
||||
if(op) {
|
||||
stroke(0);
|
||||
point(width/2, i);
|
||||
}
|
||||
|
||||
// Testing if a boolean value is "false"
|
||||
// The expression "if(!op)" is equivalent to "if(op == false)"
|
||||
if(!op) {
|
||||
stroke(255);
|
||||
point(width/4, i);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,66 @@
|
||||
import processing.core.*;
|
||||
|
||||
import java.applet.*;
|
||||
import java.awt.*;
|
||||
import java.awt.image.*;
|
||||
import java.awt.event.*;
|
||||
import java.io.*;
|
||||
import java.net.*;
|
||||
import java.text.*;
|
||||
import java.util.*;
|
||||
import java.util.zip.*;
|
||||
import java.util.regex.*;
|
||||
|
||||
public class LogicalOperators extends PApplet {
|
||||
public void setup() {/**
|
||||
* Logical Operators.
|
||||
*
|
||||
* The logical operators for AND (&&) and OR (||) are used to
|
||||
* combine simple relational statements into more complex expressions.
|
||||
* The NOT (!) operator is used to negate a boolean statement.
|
||||
*/
|
||||
|
||||
size(200, 200);
|
||||
background(126);
|
||||
|
||||
boolean op = false;
|
||||
|
||||
for(int i=5; i<=195; i+=5) {
|
||||
// Logical AND
|
||||
stroke(0);
|
||||
if((i > 35) && (i < 100)) {
|
||||
line(5, i, 95, i);
|
||||
op = false;
|
||||
}
|
||||
|
||||
// Logical OR
|
||||
stroke(76);
|
||||
if((i <= 35) || (i >= 100)) {
|
||||
line(105, i, 195, i);
|
||||
op = true;
|
||||
}
|
||||
|
||||
// Testing if a boolean value is "true"
|
||||
// The expression "if(op)" is equivalent to "if(op == true)"
|
||||
if(op) {
|
||||
stroke(0);
|
||||
point(width/2, i);
|
||||
}
|
||||
|
||||
// Testing if a boolean value is "false"
|
||||
// The expression "if(!op)" is equivalent to "if(op == false)"
|
||||
if(!op) {
|
||||
stroke(255);
|
||||
point(width/4, i);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
noLoop();
|
||||
}
|
||||
static public void main(String args[]) {
|
||||
PApplet.main(new String[] { "LogicalOperators" });
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
/**
|
||||
* Logical Operators.
|
||||
*
|
||||
* The logical operators for AND (&&) and OR (||) are used to
|
||||
* combine simple relational statements into more complex expressions.
|
||||
* The NOT (!) operator is used to negate a boolean statement.
|
||||
*/
|
||||
|
||||
size(200, 200);
|
||||
background(126);
|
||||
|
||||
boolean op = false;
|
||||
|
||||
for(int i=5; i<=195; i+=5) {
|
||||
// Logical AND
|
||||
stroke(0);
|
||||
if((i > 35) && (i < 100)) {
|
||||
line(5, i, 95, i);
|
||||
op = false;
|
||||
}
|
||||
|
||||
// Logical OR
|
||||
stroke(76);
|
||||
if((i <= 35) || (i >= 100)) {
|
||||
line(105, i, 195, i);
|
||||
op = true;
|
||||
}
|
||||
|
||||
// Testing if a boolean value is "true"
|
||||
// The expression "if(op)" is equivalent to "if(op == true)"
|
||||
if(op) {
|
||||
stroke(0);
|
||||
point(width/2, i);
|
||||
}
|
||||
|
||||
// Testing if a boolean value is "false"
|
||||
// The expression "if(!op)" is equivalent to "if(op == false)"
|
||||
if(!op) {
|
||||
stroke(255);
|
||||
point(width/4, i);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
After Width: | Height: | Size: 2.2 KiB |
@@ -0,0 +1,82 @@
|
||||
/**
|
||||
* Characters Strings.
|
||||
*
|
||||
* Click on the image to give it focus and then type letters to
|
||||
* shift the location of the image.
|
||||
* Characters are typographic symbols such as A, d, and %.
|
||||
* The character datatype, abbreviated as char, stores letters and
|
||||
* symbols in the Unicode format, a coding system developed to support
|
||||
* a variety of world languages. Characters are distinguished from other
|
||||
* symbols by putting them between single quotes ('P').
|
||||
* A string is a sequence of characters. A string is noted by surrounding
|
||||
* a group of letters with double quotes ("Processing").
|
||||
* Chars and strings are most often used with the keyboard methods,
|
||||
* to display text to the screen, and to load images or files.
|
||||
*/
|
||||
|
||||
PImage frog;
|
||||
PFont font;
|
||||
int xoffset;
|
||||
char letter;
|
||||
|
||||
void setup()
|
||||
{
|
||||
size(200, 200, P2D);
|
||||
|
||||
font = loadFont("Eureka-90.vlw");
|
||||
textFont(font);
|
||||
// Draw text more accurately and efficiently.
|
||||
textMode(SCREEN);
|
||||
textAlign(CENTER);
|
||||
|
||||
// The String datatype must be capitalized because it is a complex datatype.
|
||||
// A String is actually a class with its own methods, some of which are
|
||||
// featured below.
|
||||
String name = "rathausFrog";
|
||||
String extension = ".jpg";
|
||||
int nameLength = name.length();
|
||||
println("The length of " + name + " is " + nameLength + ".");
|
||||
name = name.concat(extension);
|
||||
nameLength = name.length();
|
||||
println("The length of " + name + " is " + nameLength + ".");
|
||||
|
||||
// The parameter for the loadImage() method must be a string
|
||||
// This line could also be written "frog = loadImage("rathausFrog.jpg");
|
||||
frog = loadImage(name);
|
||||
}
|
||||
|
||||
void draw()
|
||||
{
|
||||
background(51); // Set background to dark gray
|
||||
|
||||
// Same as image(frog, xoffset, 0), but more efficient
|
||||
// because no transformations or tint() or smooth() are used.
|
||||
set(xoffset, 0, frog);
|
||||
|
||||
// Draw an X
|
||||
line(0, 0, width, height);
|
||||
line(0, height, width, 0);
|
||||
|
||||
// // Get the width of the letter
|
||||
// float letterWidth = textWidth(letter);
|
||||
//
|
||||
// Draw the letter to the center of the screen
|
||||
text(letter, width/2, height/2);
|
||||
}
|
||||
|
||||
void keyPressed()
|
||||
{
|
||||
// The variable "key" always contains the value of the most recent key pressed.
|
||||
// If the key is an upper or lowercase letter between 'A' and 'z'
|
||||
// the image is shifted to the corresponding value of that key
|
||||
if (key >= 'A' && key <= 'z') {
|
||||
// Map the index of the key pressed from the range between 'A' and 'z',
|
||||
// into a position for the left edge of the image. The maximum xoffset
|
||||
// is the width of the drawing area minus the size of the image.
|
||||
xoffset = int(map(key, 'A', 'z', 0, width - frog.width));
|
||||
// Update the letter shown to the screen
|
||||
letter = key;
|
||||
// Write the letter to the console
|
||||
println(key);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,101 @@
|
||||
import processing.core.*;
|
||||
|
||||
import java.applet.*;
|
||||
import java.awt.*;
|
||||
import java.awt.image.*;
|
||||
import java.awt.event.*;
|
||||
import java.io.*;
|
||||
import java.net.*;
|
||||
import java.text.*;
|
||||
import java.util.*;
|
||||
import java.util.zip.*;
|
||||
import java.util.regex.*;
|
||||
|
||||
public class CharactersStrings extends PApplet {
|
||||
|
||||
/**
|
||||
* Characters Strings.
|
||||
*
|
||||
* Click on the image to give it focus and then type letters to
|
||||
* shift the location of the image.
|
||||
* Characters are typographic symbols such as A, d, and %.
|
||||
* The character datatype, abbreviated as char, stores letters and
|
||||
* symbols in the Unicode format, a coding system developed to support
|
||||
* a variety of world languages. Characters are distinguished from other
|
||||
* symbols by putting them between single quotes ('P').
|
||||
* A string is a sequence of characters. A string is noted by surrounding
|
||||
* a group of letters with double quotes ("Processing").
|
||||
* Chars and strings are most often used with the keyboard methods,
|
||||
* to display text to the screen, and to load images or files.
|
||||
*/
|
||||
|
||||
PImage frog;
|
||||
PFont fontA;
|
||||
int lettersize = 90;
|
||||
int xoffset;
|
||||
char letter;
|
||||
|
||||
public void setup()
|
||||
{
|
||||
size(200, 200);
|
||||
fontA = loadFont("Eureka90.vlw");
|
||||
textFont(fontA);
|
||||
textSize(lettersize);
|
||||
|
||||
// The String datatype must be capitalized because it is a complex datatype.
|
||||
// A String is actually a class with its own methods, some of which are
|
||||
// featured below.
|
||||
String name= "rathausFrog";
|
||||
String extension = ".jpg";
|
||||
int nameLength = name.length();
|
||||
println("The length of " + name + " is " + nameLength + ".");
|
||||
name = name.concat(extension);
|
||||
nameLength = name.length();
|
||||
println("The length of " + name + " is " + nameLength + ".");
|
||||
|
||||
// The parameter for the loadImage() method must be a string
|
||||
// This line could also be written "frog = loadImage("rathausFrog.jpg");
|
||||
frog = loadImage(name);
|
||||
}
|
||||
|
||||
public void draw()
|
||||
{
|
||||
background(51); // Set background to dark gray
|
||||
|
||||
image(frog, xoffset, 0);
|
||||
|
||||
// Draw an X
|
||||
line(0, 0, width, height);
|
||||
line(0, height, width, 0);
|
||||
|
||||
// Get the width of the letter
|
||||
int letterWidth = PApplet.parseInt(fontA.width(letter) * lettersize);
|
||||
|
||||
// Draw the letter to the center of the screen
|
||||
text(letter, width/2-letterWidth/2, height/2);
|
||||
}
|
||||
|
||||
public void keyPressed()
|
||||
{
|
||||
// The variable "key" always contains the value of the most recent key pressed.
|
||||
// If the key is an upper or lowercase letter between 'A' and 'z'
|
||||
// the image is shifted to the corresponding value of that key
|
||||
if(key >= 'A' && key <= 'z') {
|
||||
letter = PApplet.parseChar(key);
|
||||
// Scale the values to numbers between 0 and 100
|
||||
float scale = 100.0f/57.0f;
|
||||
int temp = PApplet.parseInt((key - 'A') * scale);
|
||||
// Set the offset for the image
|
||||
xoffset = temp;
|
||||
println(key);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
static public void main(String args[]) {
|
||||
PApplet.main(new String[] { "CharactersStrings" });
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,81 @@
|
||||
/**
|
||||
* Characters Strings.
|
||||
*
|
||||
* Click on the image to give it focus and then type letters to
|
||||
* shift the location of the image.
|
||||
* Characters are typographic symbols such as A, d, and %.
|
||||
* The character datatype, abbreviated as char, stores letters and
|
||||
* symbols in the Unicode format, a coding system developed to support
|
||||
* a variety of world languages. Characters are distinguished from other
|
||||
* symbols by putting them between single quotes ('P').
|
||||
* A string is a sequence of characters. A string is noted by surrounding
|
||||
* a group of letters with double quotes ("Processing").
|
||||
* Chars and strings are most often used with the keyboard methods,
|
||||
* to display text to the screen, and to load images or files.
|
||||
*/
|
||||
|
||||
PImage frog;
|
||||
PFont fontA;
|
||||
int lettersize = 90;
|
||||
int xoffset;
|
||||
char letter;
|
||||
|
||||
void setup()
|
||||
{
|
||||
size(200, 200);
|
||||
fontA = loadFont("Eureka90.vlw");
|
||||
textFont(fontA);
|
||||
textSize(lettersize);
|
||||
|
||||
// The String datatype must be capitalized because it is a complex datatype.
|
||||
// A String is actually a class with its own methods, some of which are
|
||||
// featured below.
|
||||
String name= "rathausFrog";
|
||||
String extension = ".jpg";
|
||||
int nameLength = name.length();
|
||||
println("The length of " + name + " is " + nameLength + ".");
|
||||
name = name.concat(extension);
|
||||
nameLength = name.length();
|
||||
println("The length of " + name + " is " + nameLength + ".");
|
||||
|
||||
// The parameter for the loadImage() method must be a string
|
||||
// This line could also be written "frog = loadImage("rathausFrog.jpg");
|
||||
frog = loadImage(name);
|
||||
}
|
||||
|
||||
void draw()
|
||||
{
|
||||
background(51); // Set background to dark gray
|
||||
|
||||
image(frog, xoffset, 0);
|
||||
|
||||
// Draw an X
|
||||
line(0, 0, width, height);
|
||||
line(0, height, width, 0);
|
||||
|
||||
// Get the width of the letter
|
||||
int letterWidth = int(fontA.width(letter) * lettersize);
|
||||
|
||||
// Draw the letter to the center of the screen
|
||||
text(letter, width/2-letterWidth/2, height/2);
|
||||
}
|
||||
|
||||
void keyPressed()
|
||||
{
|
||||
// The variable "key" always contains the value of the most recent key pressed.
|
||||
// If the key is an upper or lowercase letter between 'A' and 'z'
|
||||
// the image is shifted to the corresponding value of that key
|
||||
if(key >= 'A' && key <= 'z') {
|
||||
letter = char(key);
|
||||
// Scale the values to numbers between 0 and 100
|
||||
float scale = 100.0/57.0;
|
||||
int temp = int((key - 'A') * scale);
|
||||
// Set the offset for the image
|
||||
xoffset = temp;
|
||||
println(key);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
After Width: | Height: | Size: 2.2 KiB |
@@ -0,0 +1,28 @@
|
||||
/**
|
||||
* Datatype Conversion.
|
||||
*
|
||||
* It is sometimes beneficial to convert a value from one type of
|
||||
* data to another. Each of the conversion functions converts its parameter
|
||||
* to an equivalent representation within its datatype.
|
||||
* The conversion functions include int(), float(), char(), byte(), and others.
|
||||
*/
|
||||
|
||||
size(200, 200);
|
||||
background(51);
|
||||
noStroke();
|
||||
|
||||
char c; // Chars are used for storing typographic symbols
|
||||
float f; // Floats are decimal numbers
|
||||
int i; // Ints are values between 2,147,483,647 and -2147483648
|
||||
byte b; // Bytes are values between -128 and 128
|
||||
|
||||
c = 'A';
|
||||
f = float(c); // Sets f = 65.0
|
||||
i = int(f * 1.4); // Sets i to 91
|
||||
b = byte(c / 2); // Sets b to 32
|
||||
|
||||
rect(f, 0, 40, 66);
|
||||
fill(204);
|
||||
rect(i, 67, 40, 66);
|
||||
fill(255);
|
||||
rect(b, 134, 40, 66);
|
||||
@@ -0,0 +1,49 @@
|
||||
import processing.core.*;
|
||||
|
||||
import java.applet.*;
|
||||
import java.awt.*;
|
||||
import java.awt.image.*;
|
||||
import java.awt.event.*;
|
||||
import java.io.*;
|
||||
import java.net.*;
|
||||
import java.text.*;
|
||||
import java.util.*;
|
||||
import java.util.zip.*;
|
||||
import java.util.regex.*;
|
||||
|
||||
public class DatatypeConversion extends PApplet {
|
||||
public void setup() {/**
|
||||
* Datatype Conversion.
|
||||
*
|
||||
* It is sometimes beneficial to convert a value from one type of
|
||||
* data to another. Each of the conversion functions converts its parameter
|
||||
* to an equivalent representation within its datatype.
|
||||
* The conversion functions include int(), float(), char(), byte(), and others.
|
||||
*/
|
||||
|
||||
size(200, 200);
|
||||
background(51);
|
||||
noStroke();
|
||||
|
||||
char c; // Chars are used for storing typographic symbols
|
||||
float f; // Floats are decimal numbers
|
||||
int i; // Ints are values between 2,147,483,647 and -2147483648
|
||||
byte b; // Bytes are values between -128 and 128
|
||||
|
||||
c = 'A';
|
||||
f = PApplet.parseFloat(c); // Sets f = 65.0
|
||||
i = PApplet.parseInt(f * 1.4f); // Sets i to 91
|
||||
b = PApplet.parseByte(c / 2); // Sets b to 32
|
||||
|
||||
rect(f, 0, 40, 66);
|
||||
fill(204);
|
||||
rect(i, 67, 40, 66);
|
||||
fill(255);
|
||||
rect(b, 134, 40, 66);
|
||||
|
||||
noLoop();
|
||||
}
|
||||
static public void main(String args[]) {
|
||||
PApplet.main(new String[] { "DatatypeConversion" });
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
/**
|
||||
* Datatype Conversion.
|
||||
*
|
||||
* It is sometimes beneficial to convert a value from one type of
|
||||
* data to another. Each of the conversion functions converts its parameter
|
||||
* to an equivalent representation within its datatype.
|
||||
* The conversion functions include int(), float(), char(), byte(), and others.
|
||||
*/
|
||||
|
||||
size(200, 200);
|
||||
background(51);
|
||||
noStroke();
|
||||
|
||||
char c; // Chars are used for storing typographic symbols
|
||||
float f; // Floats are decimal numbers
|
||||
int i; // Ints are values between 2,147,483,647 and -2147483648
|
||||
byte b; // Bytes are values between -128 and 128
|
||||
|
||||
c = 'A';
|
||||
f = float(c); // Sets f = 65.0
|
||||
i = int(f * 1.4); // Sets i to 91
|
||||
b = byte(c / 2); // Sets b to 32
|
||||
|
||||
rect(f, 0, 40, 66);
|
||||
fill(204);
|
||||
rect(i, 67, 40, 66);
|
||||
fill(255);
|
||||
rect(b, 134, 40, 66);
|
||||
|
After Width: | Height: | Size: 2.2 KiB |
@@ -0,0 +1,36 @@
|
||||
/**
|
||||
* Integers Floats.
|
||||
*
|
||||
* Integers and floats are two different kinds of numerical data.
|
||||
* An integer (more commonly called an int) is a number without
|
||||
* a decimal point. A float is a floating-point number, which means
|
||||
* it is a number that has a decimal place. Floats are used when
|
||||
* more precision is needed.
|
||||
*/
|
||||
|
||||
int a = 0; // Create a variable "a" of the datatype "int"
|
||||
float b = 0.0; // Create a variable "b" of the datatype "float"
|
||||
|
||||
void setup()
|
||||
{
|
||||
size(200, 200);
|
||||
stroke(255);
|
||||
frameRate(30);
|
||||
}
|
||||
|
||||
void draw()
|
||||
{
|
||||
background(51);
|
||||
|
||||
a = a + 1;
|
||||
b = b + 0.2;
|
||||
line(a, 0, a, height/2);
|
||||
line(b, height/2, b, height);
|
||||
|
||||
if(a > width) {
|
||||
a = 0;
|
||||
}
|
||||
if(b > width) {
|
||||
b = 0;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
import processing.core.*;
|
||||
|
||||
import java.applet.*;
|
||||
import java.awt.*;
|
||||
import java.awt.image.*;
|
||||
import java.awt.event.*;
|
||||
import java.io.*;
|
||||
import java.net.*;
|
||||
import java.text.*;
|
||||
import java.util.*;
|
||||
import java.util.zip.*;
|
||||
import java.util.regex.*;
|
||||
|
||||
public class IntegersFloats extends PApplet {
|
||||
|
||||
/**
|
||||
* Integers Floats.
|
||||
*
|
||||
* Integers and floats are two different kinds of numerical data.
|
||||
* An integer (more commonly called an int) is a number without
|
||||
* a decimal point. A float is a floating-point number, which means
|
||||
* it is a number that has a decimal place. Floats are used when
|
||||
* more precision is needed.
|
||||
*/
|
||||
|
||||
int a = 0; // Create a variable "a" of the datatype "int"
|
||||
float b = 0.0f; // Create a variable "b" of the datatype "float"
|
||||
|
||||
public void setup()
|
||||
{
|
||||
size(200, 200);
|
||||
stroke(255);
|
||||
frameRate(30);
|
||||
}
|
||||
|
||||
public void draw()
|
||||
{
|
||||
background(51);
|
||||
|
||||
a = a + 1;
|
||||
b = b + 0.2f;
|
||||
line(a, 0, a, height/2);
|
||||
line(b, height/2, b, height);
|
||||
|
||||
if(a > width) {
|
||||
a = 0;
|
||||
}
|
||||
if(b > width) {
|
||||
b = 0;
|
||||
}
|
||||
}
|
||||
|
||||
static public void main(String args[]) {
|
||||
PApplet.main(new String[] { "IntegersFloats" });
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
/**
|
||||
* Integers Floats.
|
||||
*
|
||||
* Integers and floats are two different kinds of numerical data.
|
||||
* An integer (more commonly called an int) is a number without
|
||||
* a decimal point. A float is a floating-point number, which means
|
||||
* it is a number that has a decimal place. Floats are used when
|
||||
* more precision is needed.
|
||||
*/
|
||||
|
||||
int a = 0; // Create a variable "a" of the datatype "int"
|
||||
float b = 0.0; // Create a variable "b" of the datatype "float"
|
||||
|
||||
void setup()
|
||||
{
|
||||
size(200, 200);
|
||||
stroke(255);
|
||||
frameRate(30);
|
||||
}
|
||||
|
||||
void draw()
|
||||
{
|
||||
background(51);
|
||||
|
||||
a = a + 1;
|
||||
b = b + 0.2;
|
||||
line(a, 0, a, height/2);
|
||||
line(b, height/2, b, height);
|
||||
|
||||
if(a > width) {
|
||||
a = 0;
|
||||
}
|
||||
if(b > width) {
|
||||
b = 0;
|
||||
}
|
||||
}
|
||||
|
After Width: | Height: | Size: 2.2 KiB |
@@ -0,0 +1,34 @@
|
||||
/**
|
||||
* True/False.
|
||||
*
|
||||
* Boolean data is one bit of information. True or false.
|
||||
* It is common to use Booleans with control statements to
|
||||
* determine the flow of a program. In this example, when the
|
||||
* boolean value "x" is true, vertical black lines are drawn and when
|
||||
* the boolean value "x" is false, horizontal gray lines are drawn.
|
||||
*/
|
||||
|
||||
boolean x = false;
|
||||
|
||||
size(200, 200);
|
||||
background(0);
|
||||
stroke(0);
|
||||
|
||||
for (int i = 1; i < width; i += 2)
|
||||
{
|
||||
if (i < width/2) {
|
||||
x = true;
|
||||
} else {
|
||||
x = false;
|
||||
}
|
||||
|
||||
if (x) {
|
||||
stroke(255);
|
||||
line(i, 1, i, height-1);
|
||||
}
|
||||
|
||||
if (!x) {
|
||||
stroke(126);
|
||||
line(width/2 , i, width-2, i);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
import processing.core.*;
|
||||
|
||||
import java.applet.*;
|
||||
import java.awt.*;
|
||||
import java.awt.image.*;
|
||||
import java.awt.event.*;
|
||||
import java.io.*;
|
||||
import java.net.*;
|
||||
import java.text.*;
|
||||
import java.util.*;
|
||||
import java.util.zip.*;
|
||||
import java.util.regex.*;
|
||||
|
||||
public class TrueFalse extends PApplet {
|
||||
public void setup() {/**
|
||||
* True/False.
|
||||
*
|
||||
* Boolean data is one bit of information. True or false.
|
||||
* It is common to use Booleans with control statements to
|
||||
* determine the flow of a program. In this example, when the
|
||||
* boolean value "x" is true, vertical black lines are drawn and when
|
||||
* the boolean value "x" is false, horizontal gray lines are drawn.
|
||||
*/
|
||||
|
||||
boolean x = false;
|
||||
|
||||
size(200, 200);
|
||||
background(0);
|
||||
stroke(0);
|
||||
|
||||
for (int i = 1; i < width; i += 2)
|
||||
{
|
||||
if (i < width/2) {
|
||||
x = true;
|
||||
} else {
|
||||
x = false;
|
||||
}
|
||||
|
||||
if (x) {
|
||||
stroke(255);
|
||||
line(i, 1, i, height-1);
|
||||
}
|
||||
|
||||
if (!x) {
|
||||
stroke(126);
|
||||
line(width/2 , i, width-2, i);
|
||||
}
|
||||
}
|
||||
|
||||
noLoop();
|
||||
}
|
||||
static public void main(String args[]) {
|
||||
PApplet.main(new String[] { "TrueFalse" });
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
/**
|
||||
* True/False.
|
||||
*
|
||||
* Boolean data is one bit of information. True or false.
|
||||
* It is common to use Booleans with control statements to
|
||||
* determine the flow of a program. In this example, when the
|
||||
* boolean value "x" is true, vertical black lines are drawn and when
|
||||
* the boolean value "x" is false, horizontal gray lines are drawn.
|
||||
*/
|
||||
|
||||
boolean x = false;
|
||||
|
||||
size(200, 200);
|
||||
background(0);
|
||||
stroke(0);
|
||||
|
||||
for (int i = 1; i < width; i += 2)
|
||||
{
|
||||
if (i < width/2) {
|
||||
x = true;
|
||||
} else {
|
||||
x = false;
|
||||
}
|
||||
|
||||
if (x) {
|
||||
stroke(255);
|
||||
line(i, 1, i, height-1);
|
||||
}
|
||||
|
||||
if (!x) {
|
||||
stroke(126);
|
||||
line(width/2 , i, width-2, i);
|
||||
}
|
||||
}
|
||||
|
After Width: | Height: | Size: 2.2 KiB |
@@ -0,0 +1,61 @@
|
||||
/**
|
||||
* Variable Scope.
|
||||
*
|
||||
* Variables may either have a global or local "scope".
|
||||
* For example, variables declared within either the
|
||||
* setup() or loop() functions may be only used in these
|
||||
* functions. Global variables, variables declared outside
|
||||
* of setup() and loop(), may be used anywhere within the program.
|
||||
* If a local variable is declared with the same name as a
|
||||
* global variable, the program will use the local variable to make
|
||||
* its calculations within the current scope. Variables may be localized
|
||||
* within classes, functions, and iterative statements.
|
||||
*/
|
||||
|
||||
int a = 20; // Create a global variable "a"
|
||||
|
||||
void setup()
|
||||
{
|
||||
size(200, 200);
|
||||
background(51);
|
||||
stroke(255);
|
||||
noLoop();
|
||||
}
|
||||
|
||||
void draw()
|
||||
{
|
||||
// Draw a line using the global variable "a"
|
||||
line(a, 0, a, height);
|
||||
|
||||
// Create a new variable "a" local to the for() statement
|
||||
for(int a=50; a<80; a += 2) {
|
||||
line(a, 0, a, height);
|
||||
}
|
||||
|
||||
// Create a new variable "a" local to the loop() method
|
||||
int a = 100;
|
||||
// Draw a line using the new local variable "a"
|
||||
line(a, 0, a, height);
|
||||
|
||||
// Make a call to the custom function drawAnotherLine()
|
||||
drawAnotherLine();
|
||||
|
||||
// Make a call to the custom function setYetAnotherLine()
|
||||
drawYetAnotherLine();
|
||||
}
|
||||
|
||||
void drawAnotherLine()
|
||||
{
|
||||
// Create a new variable "a" local to this method
|
||||
int a = 185;
|
||||
// Draw a line using the local variable "a"
|
||||
line(a, 0, a, height);
|
||||
}
|
||||
|
||||
void drawYetAnotherLine()
|
||||
{
|
||||
// Because no new local variable "a" is set,
|
||||
// this lines draws using the original global
|
||||
// variable "a" which is set to the value 20.
|
||||
line(a+2, 0, a+2, height);
|
||||
}
|
||||
@@ -0,0 +1,81 @@
|
||||
import processing.core.*;
|
||||
|
||||
import java.applet.*;
|
||||
import java.awt.*;
|
||||
import java.awt.image.*;
|
||||
import java.awt.event.*;
|
||||
import java.io.*;
|
||||
import java.net.*;
|
||||
import java.text.*;
|
||||
import java.util.*;
|
||||
import java.util.zip.*;
|
||||
import java.util.regex.*;
|
||||
|
||||
public class VariableScope extends PApplet {
|
||||
|
||||
/**
|
||||
* Variable Scope.
|
||||
*
|
||||
* Variables may either have a global or local "scope".
|
||||
* For example, variables declared within either the
|
||||
* setup() or loop() functions may be only used in these
|
||||
* functions. Global variables, variables declared outside
|
||||
* of setup() and loop(), may be used anywhere within the program.
|
||||
* If a local variable is declared with the same name as a
|
||||
* global variable, the program will use the local variable to make
|
||||
* its calculations within the current scope. Variables may be localized
|
||||
* within classes, functions, and iterative statements.
|
||||
*/
|
||||
|
||||
int a = 20; // Create a global variable "a"
|
||||
|
||||
public void setup()
|
||||
{
|
||||
size(200, 200);
|
||||
background(51);
|
||||
stroke(255);
|
||||
noLoop();
|
||||
}
|
||||
|
||||
public void draw()
|
||||
{
|
||||
// Draw a line using the global variable "a"
|
||||
line(a, 0, a, height);
|
||||
|
||||
// Create a new variable "a" local to the for() statement
|
||||
for(int a=50; a<80; a += 2) {
|
||||
line(a, 0, a, height);
|
||||
}
|
||||
|
||||
// Create a new variable "a" local to the loop() method
|
||||
int a = 100;
|
||||
// Draw a line using the new local variable "a"
|
||||
line(a, 0, a, height);
|
||||
|
||||
// Make a call to the custom function drawAnotherLine()
|
||||
drawAnotherLine();
|
||||
|
||||
// Make a call to the custom function setYetAnotherLine()
|
||||
drawYetAnotherLine();
|
||||
}
|
||||
|
||||
public void drawAnotherLine()
|
||||
{
|
||||
// Create a new variable "a" local to this method
|
||||
int a = 185;
|
||||
// Draw a line using the local variable "a"
|
||||
line(a, 0, a, height);
|
||||
}
|
||||
|
||||
public void drawYetAnotherLine()
|
||||
{
|
||||
// Because no new local variable "a" is set,
|
||||
// this lines draws using the original global
|
||||
// variable "a" which is set to the value 20.
|
||||
line(a+2, 0, a+2, height);
|
||||
}
|
||||
|
||||
static public void main(String args[]) {
|
||||
PApplet.main(new String[] { "VariableScope" });
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
/**
|
||||
* Variable Scope.
|
||||
*
|
||||
* Variables may either have a global or local "scope".
|
||||
* For example, variables declared within either the
|
||||
* setup() or loop() functions may be only used in these
|
||||
* functions. Global variables, variables declared outside
|
||||
* of setup() and loop(), may be used anywhere within the program.
|
||||
* If a local variable is declared with the same name as a
|
||||
* global variable, the program will use the local variable to make
|
||||
* its calculations within the current scope. Variables may be localized
|
||||
* within classes, functions, and iterative statements.
|
||||
*/
|
||||
|
||||
int a = 20; // Create a global variable "a"
|
||||
|
||||
void setup()
|
||||
{
|
||||
size(200, 200);
|
||||
background(51);
|
||||
stroke(255);
|
||||
noLoop();
|
||||
}
|
||||
|
||||
void draw()
|
||||
{
|
||||
// Draw a line using the global variable "a"
|
||||
line(a, 0, a, height);
|
||||
|
||||
// Create a new variable "a" local to the for() statement
|
||||
for(int a=50; a<80; a += 2) {
|
||||
line(a, 0, a, height);
|
||||
}
|
||||
|
||||
// Create a new variable "a" local to the loop() method
|
||||
int a = 100;
|
||||
// Draw a line using the new local variable "a"
|
||||
line(a, 0, a, height);
|
||||
|
||||
// Make a call to the custom function drawAnotherLine()
|
||||
drawAnotherLine();
|
||||
|
||||
// Make a call to the custom function setYetAnotherLine()
|
||||
drawYetAnotherLine();
|
||||
}
|
||||
|
||||
void drawAnotherLine()
|
||||
{
|
||||
// Create a new variable "a" local to this method
|
||||
int a = 185;
|
||||
// Draw a line using the local variable "a"
|
||||
line(a, 0, a, height);
|
||||
}
|
||||
|
||||
void drawYetAnotherLine()
|
||||
{
|
||||
// Because no new local variable "a" is set,
|
||||
// this lines draws using the original global
|
||||
// variable "a" which is set to the value 20.
|
||||
line(a+2, 0, a+2, height);
|
||||
}
|
||||
|
After Width: | Height: | Size: 2.2 KiB |
@@ -0,0 +1,23 @@
|
||||
/**
|
||||
* Variables.
|
||||
*
|
||||
* Variables are used for storing values. In this example, changing
|
||||
* the values of variables 'a' and 'b' significantly change the composition.
|
||||
*/
|
||||
|
||||
size(200, 200);
|
||||
background(0);
|
||||
stroke(153);
|
||||
|
||||
int a = 20;
|
||||
int b = 50;
|
||||
int c = a*8;
|
||||
int d = a*9;
|
||||
int e = b-a;
|
||||
int f = b*2;
|
||||
int g = f+e;
|
||||
|
||||
line(a, f, b, g);
|
||||
line(b, e, b, g);
|
||||
line(b, e, d, c);
|
||||
line(a, e, d-e, c);
|
||||
@@ -0,0 +1,44 @@
|
||||
import processing.core.*;
|
||||
|
||||
import java.applet.*;
|
||||
import java.awt.*;
|
||||
import java.awt.image.*;
|
||||
import java.awt.event.*;
|
||||
import java.io.*;
|
||||
import java.net.*;
|
||||
import java.text.*;
|
||||
import java.util.*;
|
||||
import java.util.zip.*;
|
||||
import java.util.regex.*;
|
||||
|
||||
public class Variables extends PApplet {
|
||||
public void setup() {/**
|
||||
* Variables.
|
||||
*
|
||||
* Variables are used for storing values. In this example, changing
|
||||
* the values of variables 'a' and 'b' significantly change the composition.
|
||||
*/
|
||||
|
||||
size(200, 200);
|
||||
background(0);
|
||||
stroke(153);
|
||||
|
||||
int a = 20;
|
||||
int b = 50;
|
||||
int c = a*8;
|
||||
int d = a*9;
|
||||
int e = b-a;
|
||||
int f = b*2;
|
||||
int g = f+e;
|
||||
|
||||
line(a, f, b, g);
|
||||
line(b, e, b, g);
|
||||
line(b, e, d, c);
|
||||
line(a, e, d-e, c);
|
||||
|
||||
noLoop();
|
||||
}
|
||||
static public void main(String args[]) {
|
||||
PApplet.main(new String[] { "Variables" });
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
/**
|
||||
* Variables.
|
||||
*
|
||||
* Variables are used for storing values. In this example, changing
|
||||
* the values of variables 'a' and 'b' significantly change the composition.
|
||||
*/
|
||||
|
||||
size(200, 200);
|
||||
background(0);
|
||||
stroke(153);
|
||||
|
||||
int a = 20;
|
||||
int b = 50;
|
||||
int c = a*8;
|
||||
int d = a*9;
|
||||
int e = b-a;
|
||||
int f = b*2;
|
||||
int g = f+e;
|
||||
|
||||
line(a, f, b, g);
|
||||
line(b, e, b, g);
|
||||
line(b, e, d, c);
|
||||
line(a, e, d-e, c);
|
||||
|
After Width: | Height: | Size: 2.2 KiB |
@@ -0,0 +1,18 @@
|
||||
/**
|
||||
* Bezier.
|
||||
*
|
||||
* The first two parameters for the bezier() function specify the
|
||||
* first point in the curve and the last two parameters specify
|
||||
* the last point. The middle parameters set the control points
|
||||
* that define the shape of the curve.
|
||||
*/
|
||||
|
||||
size(200, 200);
|
||||
background(0);
|
||||
stroke(255);
|
||||
noFill();
|
||||
smooth();
|
||||
|
||||
for(int i = 0; i < 100; i += 20) {
|
||||
bezier(90-(i/2.0), 20+i, 210, 10, 220, 150, 120-(i/8.0), 150+(i/4.0));
|
||||
}
|
||||