All code was written in Processing 2. Get it here!
Red Rectangle 20x40:
size(500, 500);
fill(255, 0, 0);
rect(250, 250, 20.0, 40.0);
Three Diagonal Lines:
size(500, 500);
stroke(255, 0, 0);
strokeWeight(2);
line(130, 130, 215, 235);
strokeCap(SQUARE);
stroke(255, 255, 255);
strokeWeight(8);
line(170, 130, 250, 235);
strokeCap(ROUND);
stroke(0, 0, 0);
strokeWeight(16.0);
line(210, 130, 285, 235);
Two Ellipses noSmooth(); and smooth(); :
The noSmooth() method draws curved objects with aliased (jagged) edges while the smooth() method slightly improves the quality of the image. The difference is just the quality of the image and the computer's ability to be able to draw said smooth object.
size(500, 500);
noSmooth();
ellipse(175, 248, 100, 100);
smooth();
ellipse(325, 248, 100, 100);
Variables:
Both x and y are 10, but in temp y is still 5.
int x = 10;
int y = 5;
int temp = x;
x = y;
y = temp;
Shapes:
Looking at the code (below) one can identify the shape of a star. The beginShape() method starts the drawing and executes the code. endShape(CLOSE) ends the execution and stops drawing.
size(400,200);
beginShape();
vertex(200,20);
vertex(220,50);
vertex(250,50);
vertex(220,80);
vertex(230,120);
vertex(200,100);
vertex(170,120);
vertex(180,80);
vertex(150,50);
vertex(180,50);
endShape(CLOSE);
Random:
float num = random(20); takes a random float and assigns it to the variable num
Drawing with random:
size(400,200);
float num = random(20);
if (num < 10) {
fill(0,255,0);
triangle(20,20,20,100,120,100);
} else {
fill(0,0,255);
ellipse(width/2, height/2, 50,50);
}
The code above draws a canvas with a height of 400px by 200px width. A float variable num is created that takes a float between 0 and 20 inclusive. If num is less than 10 draw a green, 100px triangle. Else, draw a blue ellipse that is half the height and width of the canvas. To change the color of the triangle to red, the fill method would have to be fill(255, 0, 0); .
Multiple Ellipses:
size(400,400);
float x;
float y;
for (int i = 0; i < 20; i = i+1) {
x = random(360);
y = random(360);
ellipse(x+20, y+20, 20,20);
}
To add 100 ellipses to this drawing, another if statement that checks if the repetition of the ellipse function is equal to 100. To randomize the colors, three different random variables can be created and used in the fill() function.
Both x and y are 10, but in temp y is still 5.
int x = 10;
int y = 5;
int temp = x;
x = y;
y = temp;
Shapes:
Looking at the code (below) one can identify the shape of a star. The beginShape() method starts the drawing and executes the code. endShape(CLOSE) ends the execution and stops drawing.
size(400,200);
beginShape();
vertex(200,20);
vertex(220,50);
vertex(250,50);
vertex(220,80);
vertex(230,120);
vertex(200,100);
vertex(170,120);
vertex(180,80);
vertex(150,50);
vertex(180,50);
endShape(CLOSE);
Random:
float num = random(20); takes a random float and assigns it to the variable num
Drawing with random:
size(400,200);
float num = random(20);
if (num < 10) {
fill(0,255,0);
triangle(20,20,20,100,120,100);
} else {
fill(0,0,255);
ellipse(width/2, height/2, 50,50);
}
The code above draws a canvas with a height of 400px by 200px width. A float variable num is created that takes a float between 0 and 20 inclusive. If num is less than 10 draw a green, 100px triangle. Else, draw a blue ellipse that is half the height and width of the canvas. To change the color of the triangle to red, the fill method would have to be fill(255, 0, 0); .
Multiple Ellipses:
size(400,400);
float x;
float y;
for (int i = 0; i < 20; i = i+1) {
x = random(360);
y = random(360);
ellipse(x+20, y+20, 20,20);
}
To add 100 ellipses to this drawing, another if statement that checks if the repetition of the ellipse function is equal to 100. To randomize the colors, three different random variables can be created and used in the fill() function.