WEEK 2 DAY 2
PROBLEM:
HOW TO ROTATE SHAPES?
In drawing software like Illustrator, moving, rotating and scaling objects is easy
Transformations affect individual shapes
With code you are drawing the entire frame at once
Transformations affect all the following shapes
Transformations are reset when frame is refreshed
translate(x,y);
rotate(rad); //default is radians
scale(x,y); //decimal percentage
translate(100,-50); //move origin point to (100,-50)
rotate(3); //rotate coordinates 3 radians (~180°)
scale(1,2); //scale y-axis to 200%
rect(20,20,40);
rect(80,100,40);
rect(20,20,40);
translate(60,80);
rect(20,20,40);
Translating is useful when drawing the same complicated shape in different locations
Define the coordinates in relation to the origin, then move the origin and repeat drawing
“Grouping shapes”
Translating is also necessary when rotating shapes!
Rotates the coordinate system around the point of origin
Default unit is radians
Use angleMode(DEGREES) for degrees
Scales the coordinate system in relation to the origin
X and Y axis can be scaled individually
Unit is decimal percentage
scale(2.5) increases the size 2.5 times larger than original (=250%)
rect(20,20,40);
scale(2);
rect(20,20,40);
Transformations are cumulative and affect all the following drawing commands
Drawing styles eg. fill() also affect all following drawing commands
We can save and restore transformations and styles with push and pop functions
Indenting your code makes it more legible!
push(); //start new drawing state
fill(0); //change fill to black
translate(100,100); //move origin
rect(0,0,50,50); //draw rectangle at new origin
pop(); //restore the original drawing state and style
function setup(){
createCanvas(500,500);
angleMode(DEGREES);
}
function draw(){
background(255,200,255);
strokeWeight(30);
stroke(255,100,0);
//translate the origin to center of canvas
//rotate around origin using the value of mouseX
//draw a line from the origin point to 100px on the right
//translate the origin to the end of the line
//rotate around the new origin point using the value of mouseY
//draw a line from the new origin point to 100px on the right
noStroke();
fill(255,100,0);
//draw a circle at the end of the line
}
Start from the solar system example, but don’t update the background in draw()!
Play with changing values of rotation and translation to get cool patterns!
MEMORY
Storing data and accessing it later
Variables, arrays, objects
SEQUENCE
Running instructions in order
Functions, algorithms
SELECTION
Making choices
Conditionals and logic (if, else, and, or)
REPETITION
Doing the same thing more than once
Loops (for, while)
ellipse(0, 250, 50);
ellipse(100, 250, 50);
ellipse(200, 250, 50);
ellipse(300, 250, 50);
ellipse(400, 250, 50);
ellipse(500, 250, 50);
PROBLEM:
HOW TO MAKE REPETITION EASIER?
AUTOMATION
Using computational power to complete tasks that are laborious for humans.
OPTIMISATION
Seeking solutions for complex problems that are beyond human cognition.
IDEATION
Reaching unexpected outcomes by setting into motion an autonomous process
LOOPS!
ellipse(0, height/2, 50);
ellipse(100, height/2, 50);
ellipse(200, height/2, 50);
ellipse(300, height/2, 50);
ellipse(400, height/2, 50);
ellipse(500, height/2, 50);
WE ARE ADDING 100 TO X EVERY TIME...
let x = 0;
ellipse(x, height/2, 50);
x += 100; // x is 100
ellipse(x, height/2, 50);
x += 100; // x is 200
ellipse(x, height/2, 50);
x += 100; // x is 300
ellipse(x, height/2, 50);
x += 100; // x is 400
ellipse(x, height/2, 50);
x += 100; // x is 500
ellipse(x, height/2, 50);
WE ARE REPEATING IDENTICAL LINES!
let x = 0;
ellipse(x, height/2, 50);
x += 100; // x is 100
ellipse(x, height/2, 50);
x += 100; // x is 200
ellipse(x, height/2, 50);
x += 100; // x is 300
ellipse(x, height/2, 50);
x += 100; // x is 400
ellipse(x, height/2, 50);
x += 100; // x is 500
ellipse(x, height/2, 50);
REPEAT THIS UNTIL X IS 500
while(condition){
//repeat while condition is true
}
let x = 0; //define variable
while(x <= 500){ //repeat while condition is true
ellipse(x, height/2, 50); //draw ellipse
x += 100; //increment variable
}
boolean expression
curly brackets
let x = 0; //define variable
while(x <= 500){ //repeat while condition is true
ellipse(x, height/2, 50); //draw ellipse
x += 100; //increment variable
}
let x = 0;
while(x <= 500){
ellipse(x, height/2, 50);
x += 100;
}
INCREMENT
INITIAL STATE
CONDITION
for(initial state; condition; increment){
//code block to be repeated
}
indent
semicolons between statements
curly brackets
for(initial state; condition; increment){
//code block to be repeated
}
executed when loop starts
condition for repeating the loop
executed after each repetition
for(let x = 0; x <= 500; x += 100){
ellipse(x, height/2, 50);
}
let x has local scope inside the loop!
for(let i = 0; i < 6; i++){
ellipse(i*100, height/2, 50);
}
for(let x = 0; x <= 500; x += 100){
ellipse(x, height/2, 50);
}
LOOPING ACROSS THE DISTANCE
Reapeat until x > width
LOOPING A NUMBER OF TIMES
Repeat until we have done 6 loops
THESE DO THE SAME THING IN A DIFFERENT WAY
When you don’t know how many times you have to repeat the code
When you know how many times you want to repeat the code
WHILE
FOR
LOOPING
!=
"ANIMATING"
Loop diverts the program flow
"Top to bottom, inside out"
Loop structures inside draw() are completed before the frame is refreshed
Loop does not appear as animated!
function draw(){
//this stuff happens first
for(let i = 0; i < 10; i++){
//this stuff happens until loop ends
}
//this stuff happens last
}
PROBLEM:
HOW TO CONSTRAIN VALUES?
map(value, start1, stop1, start2, stop2);
let x = map(mouseX, 0, width, 180, 360);
Converts values from one range to another
VALUE: the incoming value to be converted
START1: lower bound of the value's current range
STOP1: upper bound of the value's current range
START2: lower bound of target range
STOP2: upper bound of target range
//transformations
translate(x,y); //move point of origin
rotate(rad); //rotate around origin, default in radians
scale(p); //scale coordinate system in decimal percents
push(); //save previous transformations and drawing styles
pop(); //reset to previous transformations and styles
angleMode(MODE); // set angle unit to DEGREES or RADIANS
//while-loop
while(condition){
//repeat while condition is true
}
//for-loop
for(initial state; condition; increment){
//code block to be repeated
}
//converting values from one range to other
map(value, start1, stop1, start2, stop2);
Comment your code well!
Add your name and date on the top
Explain how the code works
Explain the key features of your clock. How does it visualise time?
If you borrow code, include references and be explicit how you have modified it!
Submit a web editor link to your sketch in MyCourses by Tuesday
Weekly Reading II