Learn creative coding writing simple programs

43. Animate using sin(). Less math thanks to map()

This time we use the sin() function to animate a circle. In our example it will move smoothly from left to right and then back, repeating this motion again and again.
The sin() function returns a number between -1 and 1. The noise() function returns a number between 0 and 1. All these numbers are too small for measuring pixels and drawing things on the screen. That's why we have to multiply those numbers to make them larger.
Since the sin() function returns negative and positive numbers, it can be a bit tricky to convert them into interesting numbers we can use to draw things on the screen.
That's why we learn to use the map() function, which converts one number from a given range to another number in a new range. For example, it allows us to convert numbers coming out from the sin() function, which are in the -1 to 1 range, into a number which is in the 0 to 400 range, much better for drawing things on the screen.


Code editor

You can make changes to the code below. Then
Below you can ask and answer questions related to this video. If your question is unrelated to the video, please post it at the Processing Forum as I'm unable to help with all of your programs and still have enough free time to eat and sleep :)
comments powered by Disqus