Initial Commit
This commit is contained in:
@@ -0,0 +1,50 @@
|
||||
/**
|
||||
* 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.
|
||||
*/
|
||||
|
||||
|
||||
float[] coswave;
|
||||
|
||||
void setup() {
|
||||
size(640, 360);
|
||||
coswave = new float[width];
|
||||
for (int i = 0; i < width; i++) {
|
||||
float amount = map(i, 0, width, 0, PI);
|
||||
coswave[i] = abs(cos(amount));
|
||||
}
|
||||
background(255);
|
||||
noLoop();
|
||||
}
|
||||
|
||||
void draw() {
|
||||
|
||||
int y1 = 0;
|
||||
int y2 = height/3;
|
||||
for (int i = 0; i < width; i++) {
|
||||
stroke(coswave[i]*255);
|
||||
line(i, y1, i, y2);
|
||||
}
|
||||
|
||||
y1 = y2;
|
||||
y2 = y1 + y1;
|
||||
for (int i = 0; i < width; i++) {
|
||||
stroke(coswave[i]*255 / 4);
|
||||
line(i, y1, i, y2);
|
||||
}
|
||||
|
||||
y1 = y2;
|
||||
y2 = height;
|
||||
for (int i = 0; i < width; i++) {
|
||||
stroke(255 - coswave[i]*255);
|
||||
line(i, y1, i, y2);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
/**
|
||||
* 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;
|
||||
int spacer;
|
||||
|
||||
void setup() {
|
||||
size(640, 360);
|
||||
maxDistance = dist(width/2, height/2, width, height);
|
||||
distances = new float[width][height];
|
||||
for (int y = 0; y < height; y++) {
|
||||
for (int x = 0; x < width; x++) {
|
||||
float distance = dist(width/2, height/2, x, y);
|
||||
distances[x][y] = distance/maxDistance * 255;
|
||||
}
|
||||
}
|
||||
spacer = 10;
|
||||
strokeWeight(6);
|
||||
noLoop(); // Run once and stop
|
||||
}
|
||||
|
||||
void draw() {
|
||||
background(0);
|
||||
// This embedded loop skips over values in the arrays based on
|
||||
// the spacer variable, so there are more values in the array
|
||||
// than are drawn here. Change the value of the spacer variable
|
||||
// to change the density of the points
|
||||
for (int y = 0; y < height; y += spacer) {
|
||||
for (int x = 0; x < width; x += spacer) {
|
||||
stroke(distances[x][y]);
|
||||
point(x + spacer/2, y + spacer/2);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
/**
|
||||
* Array Objects.
|
||||
*
|
||||
* Demonstrates the syntax for creating an array of custom objects.
|
||||
*/
|
||||
|
||||
int unit = 40;
|
||||
int count;
|
||||
Module[] mods;
|
||||
|
||||
void setup() {
|
||||
size(640, 360);
|
||||
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), unit);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void draw() {
|
||||
background(0);
|
||||
for (Module mod : mods) {
|
||||
mod.update();
|
||||
mod.display();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
class Module {
|
||||
int xOffset;
|
||||
int yOffset;
|
||||
float x, y;
|
||||
int unit;
|
||||
int xDirection = 1;
|
||||
int yDirection = 1;
|
||||
float speed;
|
||||
|
||||
// Contructor
|
||||
Module(int xOffsetTemp, int yOffsetTemp, int xTemp, int yTemp, float speedTemp, int tempUnit) {
|
||||
xOffset = xOffsetTemp;
|
||||
yOffset = yOffsetTemp;
|
||||
x = xTemp;
|
||||
y = yTemp;
|
||||
speed = speedTemp;
|
||||
unit = tempUnit;
|
||||
}
|
||||
|
||||
// Custom method for updating the variables
|
||||
void update() {
|
||||
x = x + (speed * xDirection);
|
||||
if (x >= unit || x <= 0) {
|
||||
xDirection *= -1;
|
||||
x = x + (1 * xDirection);
|
||||
y = y + (1 * yDirection);
|
||||
}
|
||||
if (y >= unit || y <= 0) {
|
||||
yDirection *= -1;
|
||||
y = y + (1 * yDirection);
|
||||
}
|
||||
}
|
||||
|
||||
// Custom method for drawing the object
|
||||
void display() {
|
||||
fill(255);
|
||||
ellipse(xOffset + x, yOffset + y, 6, 6);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user