Initial Commit
This commit is contained in:
@@ -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."<br />
|
||||
* 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(640, 360);
|
||||
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(255);
|
||||
line(i, 80, i, height/2);
|
||||
} else {
|
||||
stroke(153);
|
||||
line(i, 20, i, 180);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
/**
|
||||
* Conditionals 2.
|
||||
*
|
||||
* We extend the language of conditionals from the previous
|
||||
* example by adding the keyword "else". This allows conditionals
|
||||
* to ask two or more sequential questions, each with a different
|
||||
* action.
|
||||
*/
|
||||
|
||||
size(640, 360);
|
||||
background(0);
|
||||
|
||||
for (int i = 2; i < width-2; i += 2) {
|
||||
// If 'i' divides by 20 with no remainder
|
||||
if ((i % 20) == 0) {
|
||||
stroke(255);
|
||||
line(i, 80, i, height/2);
|
||||
// If 'i' divides by 10 with no remainder
|
||||
} else if ((i % 10) == 0) {
|
||||
stroke(153);
|
||||
line(i, 20, i, 180);
|
||||
// If neither of the above two conditions are met
|
||||
// then draw this line
|
||||
} else {
|
||||
stroke(102);
|
||||
line(i, height/2, i, height-20);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
/**
|
||||
* Embedding Iteration.
|
||||
*
|
||||
* Embedding "for" structures allows repetition in two dimensions.
|
||||
*
|
||||
*/
|
||||
|
||||
|
||||
size(640, 360);
|
||||
background(0);
|
||||
|
||||
int gridSize = 40;
|
||||
|
||||
for (int x = gridSize; x <= width - gridSize; x += gridSize) {
|
||||
for (int y = gridSize; y <= height - gridSize; y += gridSize) {
|
||||
noStroke();
|
||||
fill(255);
|
||||
rect(x-1, y-1, 3, 3);
|
||||
stroke(255, 100);
|
||||
line(x, y, width/2, height/2);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
/**
|
||||
* Iteration.
|
||||
*
|
||||
* Iteration with a "for" structure to construct repetitive forms.
|
||||
*/
|
||||
|
||||
int y;
|
||||
int num = 14;
|
||||
|
||||
size(640, 360);
|
||||
background(102);
|
||||
noStroke();
|
||||
|
||||
// White bars
|
||||
fill(255);
|
||||
y = 60;
|
||||
for(int i = 0; i < num/3; i++) {
|
||||
rect(50, y, 475, 10);
|
||||
y+=20;
|
||||
}
|
||||
|
||||
// Gray bars
|
||||
fill(51);
|
||||
y = 40;
|
||||
for(int i = 0; i < num; i++) {
|
||||
rect(405, y, 30, 10);
|
||||
y += 20;
|
||||
}
|
||||
y = 50;
|
||||
for(int i = 0; i < num; i++) {
|
||||
rect(425, y, 30, 10);
|
||||
y += 20;
|
||||
}
|
||||
|
||||
// Thin lines
|
||||
y = 45;
|
||||
fill(0);
|
||||
for(int i = 0; i < num-1; i++) {
|
||||
rect(120, y, 40, 1);
|
||||
y+= 20;
|
||||
}
|
||||
@@ -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(640, 360);
|
||||
background(126);
|
||||
|
||||
boolean test = false;
|
||||
|
||||
for (int i = 5; i <= height; i += 5) {
|
||||
// Logical AND
|
||||
stroke(0);
|
||||
if((i > 35) && (i < 100)) {
|
||||
line(width/4, i, width/2, i);
|
||||
test = false;
|
||||
}
|
||||
|
||||
// Logical OR
|
||||
stroke(76);
|
||||
if ((i <= 35) || (i >= 100)) {
|
||||
line(width/2, i, width, i);
|
||||
test = true;
|
||||
}
|
||||
|
||||
// Testing if a boolean value is "true"
|
||||
// The expression "if(test)" is equivalent to "if(test == true)"
|
||||
if (test) {
|
||||
stroke(0);
|
||||
point(width/3, i);
|
||||
}
|
||||
|
||||
// Testing if a boolean value is "false"
|
||||
// The expression "if(!test)" is equivalent to "if(test == false)"
|
||||
if (!test) {
|
||||
stroke(255);
|
||||
point(width/4, i);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user