Review Session April 19

GMT20230419-180415_Recording_1920x1080.mp4

1. Map Function

When you have a variable that could contain a numeric range of data and you want to “map” it to a new range. (For instance I have a variable that has a range of numbers from -1 to 1, yet I want to “map” it to a range of numbers from 0 to 400). We can use the map function:

// if oldValue goes from -1 to 1
//   newValue will go from 0 to 400
// For instance if oldValue=0.5, newValue=300
let newValue=map(oldValue,-1,1,0,400);

2. Basic Trigonometry (sin and cos)

The basic trigonometry functions are extremely useful for generative art. I like to use these functions for two reasons, the first is that no matter what “x” value I pass to sin(x) or cos(x), I know it will always return a number from -1 to 1. Second, I like to use these functions, especially with movement because I know that their results will look more natural than movement that happens linearly over time.

https://en.wikipedia.org/wiki/Sine_and_cosine

https://en.wikipedia.org/wiki/Sine_and_cosine

From Class, creating an interesting design using Sin. Feel free to change some of the parameters to see how it effects the design:

Look at the source at P5 Editor

Notice how the balls moving in the following sketch appear to move with a rhythm that looks more natural:

Look at the source at P5 Editor

3. Random

Randomness is also an interesting topic within Generative Art. One reason for using randomness is when you want something to happen but you would prefer it not to be a pattern. Another interesting use for randomness is that it can be used to space objects throughout a canvas.

The random function has three different ways that we can call it:

// Pick a random number from 0 to 1
let x=random();

// Pick a random number from 0 to 100
let y=random(100);

// Pick a random number from -100 to 100
let z=random(-100,100);

// Note the random function never returns the top bounding number.
// So x,y,z will never be 1,100 or 100 respectively