WEEK 3 DAY 2
PROBLEM:
HOW TO MAKE A GRID?
//draw circles 50px apart until x > width
for(let x = 0; x <= width; x+= 50){
ellipse(x,0,40);
}
//draw circles 50px apart until y > height
for(let y = 0; y <= height; y+= 50){
ellipse(0,y,40);
}
Loop inside a loop
Execute a second loop for every iteration of first loop
Works like a table
For every column (x) make rows (y)
columns (x)
rows (y)
width
height
columns = 4
rows = 3
cellWidth = width/columns
cellHeight = height/rows
cellWidth
cellHeight
Calculating the coordinates of the upper left corner of cells using cellWidth and cellHeight:
0
1
2
3
0
1
2
C0 R0: (0,0)
C1 R0: (cellWidth, 0)
C2 R1: (2*cellWidth, cellHeight)
C3 R2: (3*cellWidth, 2*cellHeight)
IN CODING WE ALWAYS START COUNTING FROM 0!
columns
rows
0
1
2
3
0
1
2
WE WANT UNIVERSAL FORMULAS!
UPPER LEFT CORNER OF EACH CELL
X = column * cellWidth
Y = row * cellHeight
CENTER OF EACH CELL
X = column * cellWidth + cellWidth/2
Y = row * cellHeight + cellHeight/2
columns
rows
THIS WORKS FOR ANY NUMBER AND SIZE OF COLUMNS AND ROWS!
const columns = 4;
const rows = 3;
let cellW = width / columns;
let cellH = height / rows;
for(let i = 0; i < columns; i++){
for(let j = 0; j < rows; j++){
//calculate coordinates
let x = i * cellW + cellW/2;
let y = j * cellH + cellH/2;
ellipse(x,y,40);
}
}
Modify the Kinetic Grid exercise so that each cell has a slightly different rotation from the previous cell (in addition to the animated rotation)
How to achieve that using the variables in the nested loop?
Calculating the index number of each cell
0 | 1 | 2 | 3 | 4 |
---|---|---|---|---|
5 | 6 | 7 | 8 | 9 |
10 | 11 | 12 | 13 | 14 |
15 | 16 | 17 | 18 | 19 |
i=0
i=1
i=2
i=3
j=0
j=1
j=2
INDEX NUMBER OF EACH CELL:
j*columns+i
i=4
j=3
columns = 5
rows = 4
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)
PROBLEM: HOW TO DRAW MULTIPLE SIMILAR SHAPES?
In addition to using pre-existing functions, you can define and use your own functions
Functions are reusable blocks of code that perform a specific task
Useful for performing the same task with different parameters
Useful for organising your code in separate blocks
Useful for distributing code
function myFunction(argument1, argument2,...){
// do stuff with arguments
}
function name
(arbitrary)
OPTIONAL
argument names in brackets, separated with commas
(arbitrary)
function body inside curly brackets
declare function
myFunction(100,200);
values for arguments
name of defined function
Your custom function is executed when it’s called somewhere outside the function definition
You can use your functions in draw() the same way you would use predefined functions (eg. ellipse)
Declare your function outside draw() and setup()
Call your function inside draw(), setup() or other function
Function arguments and other possible variables inside the function are local. They can not be accessed outside the function
PROBLEM: HOW TO GET RANDOM VALUES THAT DO NOT CHANGE IN EVERY FRAME?
Calling randomSeed() with a constant argument makes random() produce the same results each time a sketch is run and in every frame.
The seed value is arbitrary, as long as its constant.
Functions can return values to the caller
Eg. performing a calculation on the arguments and returning the solution
Same way as eg. the random() function returns a random number
function average(a,b){
let result = (a+b)/2;
return result;
}
let myAverage = average(1,3); //myAverage has value 2
//calculating grid cells
let cellW = width / columns;
let cellH = height / rows;
//drawing a grid with a nested loop
for(let i = 0; i < columns; i++){
for(let j = 0; j < rows; j++){
//calculate coordinates for cell centers
let x = i * cellW + cellW/2;
let y = j * cellH + cellH/2;
//draw eg. ellipse(x,y,w)
}
}
//defining a custom function
function myFunction(argument1, argument2,...){
// do stuff with arguments
}
//calling a custom function
myFunction(value1, value2,...)
//setting a seed value for the random function to get constant random values
randomSeed(99);
Make a program that generates grid-based compositions
The program should define and use at least one custom function (in a meaningful way!)
Comment your code well!
Add your name and date on the top
Explain how the code works
Reference any borrowed code (include links)
Explain which parts you have borrowed and how have you modified them
(No need to reference exercises from class)
Add instructions for interaction if needed!
Submit a link in MyCourses by next Tuesday