p5.js Coding Tutorial | The Making of Animation - Beautiful Trigonometry

Patt Vira
6 Dec 202315:40

Summary

TLDRThis coding tutorial demonstrates how to create an illusion of rotating circles using the concept of oscillation. The video guides viewers through drawing a large circle and smaller white circles that independently travel in an oscillating motion, creating a complex circular pattern. It covers the basics of trigonometry for coordinate conversion, uses the 'map' function to control motion with the mouse, and introduces simple harmonic motion. The tutorial also explains how to adjust axes and circle positions for different angles, ultimately showing how to create a visually appealing, dynamic effect with a customizable number of axes.

Takeaways

  • 🎨 The video demonstrates how to create an illusion of rotating circles using the concept of oscillation.
  • πŸ“ It begins by drawing the largest circle using the 'ellipse' function with arguments for center coordinates and dimensions.
  • πŸ”„ The origin point is translated to the center of the canvas to facilitate drawing symmetrically around the center.
  • πŸ“Š Smaller white circles are positioned using trigonometric equations to convert from polar to Cartesian coordinates.
  • πŸ”΄ A red circle is animated to move along a circular path based on the mouse's X position, mapped to a range of angles from 0 to 360 degrees.
  • πŸ”΅ Two additional white circles are drawn to follow the X and Y positions of the red circle, illustrating the concept of oscillation along a straight line.
  • πŸ“ Axes are drawn to represent the oscillating motion, using the 'line' function with coordinates for the start and end points of each axis.
  • πŸŒ€ Additional axes are introduced, rotated by 45Β° increments to create a grid effect, enhancing the visual illusion of motion.
  • πŸ”’ The number of axes and their rotation angles are controlled by variables, allowing for customization of the visual effect.
  • 🎨 The color and transparency of the circles and background can be adjusted to create a desired aesthetic, such as a turquoise background with black and white circles.
  • ✨ The final result is a visually appealing animation that uses simple harmonic motion to create the illusion of complex circular motion.

Q & A

  • What is the main concept demonstrated in the coding tutorial?

    -The main concept demonstrated in the tutorial is creating the illusion of rotating circles using the concept of oscillation.

  • What is the purpose of the 'ellipse' function in the tutorial?

    -The 'ellipse' function is used to draw the circles, including the largest circle and the smaller white circles that appear to be part of a bigger rotating circle.

  • Why is the 'translate' function used in the script?

    -The 'translate' function is used to move the origin point from the top left corner of the canvas to the middle point, which simplifies the centering of the circles.

  • What trigonometric equations are used to determine the X and Y locations of the smaller circles?

    -The trigonometric equations used are X = R * cos(angle) and Y = R * sin(angle), which convert from polar to Cartesian coordinates.

  • How does the script create the illusion of the smaller circles traveling in a circular path?

    -The script creates the illusion by having the smaller circles travel independently in an oscillating motion along a straight line, which is then mapped to the circular path by trigonometric functions.

  • What is the role of the 'map' function in the tutorial?

    -The 'map' function is used to map the mouse X location from 0 to width to the angle from 0 to 360 degrees, controlling the rotation of the red circle.

  • How does the script handle the drawing of additional circles that follow the X and Y locations of the red circle?

    -The script uses the same trigonometric equations to calculate the X and Y coordinates for the additional circles and then draws them at those coordinates.

  • What is the significance of the 'angleMode' function in the script?

    -The 'angleMode' function is used to set the angle to be interpreted in degrees rather than the default radians, which is more intuitive for the given context.

  • How does the script create multiple axes and corresponding circles?

    -The script uses loops and arrays to calculate shifting angles for multiple axes and then uses these angles to determine the X and Y coordinates for the circles along these axes.

  • What are the 'push' and 'pop' functions used for in the transformation process?

    -The 'push' and 'pop' functions are used to save and restore the state of the canvas before and after applying transformations like rotation, ensuring that the transformations do not affect subsequent drawings.

  • How does the tutorial achieve the final aesthetic of the rotating circles?

    -The tutorial achieves the aesthetic by adjusting the background color, circle colors, and stroke transparency, as well as incrementing the angle to create continuous motion.

Outlines

00:00

🎨 Introduction to Rotating Circles Tutorial

This paragraph introduces a coding tutorial focused on creating an animation of rotating circles using the concept of oscillation. The animation consists of smaller white circles arranged in a larger circular shape, which appears to move along a circular path. However, these circles are independently oscillating in a straight line. The tutorial suggests watching a previous video on oscillation for better understanding and begins by drawing the largest circle using the 'ellipse' function, detailing the parameters for its center and size. It also explains how to move the origin point to the center of the canvas using 'translate' and sets up the initial drawing of smaller circles using trigonometric equations to determine their positions.

05:00

πŸ“ Understanding Oscillation and Axes Drawing

The second paragraph delves into the concept of simple harmonic motion, represented by sine and cosine functions, which are used to calculate the X and Y positions of the oscillating circles. It describes how to draw additional axes that are rotated by 45Β° counterclockwise using the 'rotate' function and introduces the idea of shifting the coordinate system to follow the red circle's motion. The paragraph also explains the process of adjusting the equations for X and Y to ensure that the oscillating circles align correctly with the red circle's position at different angles, and how to implement multiple axes with a variable number of them controlled by 'num_axis'.

10:01

πŸ”„ Implementing Multiple Axes and Circles Animation

This paragraph explains how to dynamically generate multiple axes based on the 'num_axis' variable using a for loop and mathematical calculations to determine the shifting angles. It details the process of converting single variables into arrays to accommodate multiple axes and circles, and how to update the drawing code to use these arrays. The importance of using 'push' and 'pop' functions for saving and restoring the canvas state after each transformation is emphasized, ensuring that the animation remains consistent and accurate. The paragraph concludes with a discussion on adjusting the number of axes and the motion of the circles, suggesting an incremental approach to their movement.

15:03

🌈 Final Touches and Beautification

The final paragraph discusses the aesthetic aspects of the animation, including changing the background color to turquoise, the main circle to black, and the stroke to a semi-transparent white for a visually pleasing effect. It also touches on the concept of oscillation, where the circles create an illusion of circular motion by traveling back and forth on a straight path. The paragraph encourages the viewer to experiment with the number of axes and appreciates the cool visual effect achieved through this coding technique.

Mindmap

Keywords

πŸ’‘Oscillation

Oscillation refers to the repetitive motion of an object moving back and forth around an equilibrium point. In the context of the video, this concept is used to create the illusion of smaller circles moving in a circular path by actually moving them in a straight line in an oscillating manner. The script mentions using the cosine and sine functions to represent this motion, which are fundamental in describing simple harmonic motion.

πŸ’‘Ellipse Function

The ellipse function is a method used in graphics programming to draw an ellipse or a circle. The video script describes using this function with four arguments: the X and Y coordinates of the center, and the width and height of the ellipse. It is fundamental in creating the base shape that the smaller circles will oscillate around.

πŸ’‘Trigonometry

Trigonometry is a branch of mathematics that deals with the relationships between the sides and angles of triangles, particularly right-angled triangles. In the script, trigonometry is essential for converting polar coordinates (using angles) to Cartesian coordinates (X and Y positions), which allows the placement of the smaller circles along a circular path.

πŸ’‘Polar Coordinates

Polar coordinates are a two-dimensional coordinate system in which each point is determined by a radius and an angle from a reference direction. The video uses polar coordinates to calculate the position of the smaller circles along the circular path, which is then translated into Cartesian coordinates for drawing on the canvas.

πŸ’‘Cartesian Coordinates

Cartesian coordinates, also known as rectangular coordinates, are a system used to define points in a plane by a pair of numerical values. In the video, Cartesian coordinates are the end result of the conversion from polar coordinates, allowing the script to specify the exact location of the smaller circles on the canvas.

πŸ’‘Map Function

In the context of the video, the map function is a method used to scale and transform a value from one range to another. It is used to map the mouse's X location to the angle of rotation for the smaller circle, creating an interactive element where the circle's motion is influenced by the viewer's mouse movement.

πŸ’‘Simple Harmonic Motion

Simple harmonic motion is a type of periodic motion where the restoring force on the object is directly proportional to the displacement from the equilibrium position. The video describes the motion of the smaller circles as a form of simple harmonic motion, using the sine and cosine functions to create the oscillating effect.

πŸ’‘Transformation

Transformation in graphics refers to the process of modifying the position, orientation, or shape of an object. The script discusses using transformation functions like 'translate' and 'rotate' to move the origin point and to create the illusion of circular motion by rotating and shifting the axes.

πŸ’‘Cosine Function

The cosine function is a fundamental trigonometric function that relates the angle of a right-angled triangle to the ratio of the adjacent side over the hypotenuse. In the video, the cosine function is used to calculate the X position of the smaller circles as they oscillate, creating the horizontal component of their motion.

πŸ’‘Sine Function

The sine function is another key trigonometric function that relates the angle of a right-angled triangle to the ratio of the opposite side over the hypotenuse. The video uses the sine function to calculate the Y position of the circles, creating the vertical component of the oscillating motion.

πŸ’‘Angle Mode

In the context of the video, angle mode refers to the units used for angles in trigonometric calculations, such as degrees or radians. The script specifies using 'angle mode degrees' to ensure that the trigonometric functions work with degrees, which is more intuitive for the viewer.

Highlights

Introduction to a coding tutorial on creating rotating circles using oscillation.

Demonstration of an illusion where smaller white circles appear to move in a larger circular path but are actually oscillating independently.

Explanation of using the 'ellipse' function to draw the largest circle with specified width and height.

Use of 'translate' function to move the origin point to the center of the canvas for circle drawing.

Trigonometry rules applied to calculate the X and Y coordinates of the smaller circles on the circular path.

Introduction of 'map' function to control the motion of the smaller circle based on the mouse's X location.

Drawing additional circles to follow the X and Y coordinates of the red circle, creating a visual effect of oscillation.

Utilization of 'line' function to draw the X and Y axes for better visualization of the oscillating motion.

Concept of simple harmonic motion represented by sine and cosine functions to explain the oscillating movement.

Rotating the coordinate axes by 45Β° to create a new set of axes for the circles to follow.

Adjusting the equations for X and Y coordinates to align with the new rotated axes.

Use of an array to manage multiple shifting angles for different axes.

Implementation of a loop to calculate shifting angles based on the number of desired axes.

Dynamic drawing of smaller white circles based on the calculated X2 and Y2 coordinates for each axis.

Importance of 'push' and 'pop' functions in maintaining the state during transformations.

Customization of the background color and circle properties for aesthetic appeal.

Final result showcasing multiple axes and circles creating an illusion of complex motion from simple oscillation.

Transcripts

play00:00

welcome to another coding tutorial and

play00:02

in today's video I'm going to show you

play00:04

how to make this rotating circles using

play00:06

the concept of

play00:09

oscillation what you may see here is a

play00:11

set of smaller white circles in a shape

play00:13

of a bigger Circle that is traveling in

play00:16

an even bigger Circle circular path but

play00:18

that is not actually what is happening

play00:20

these smaller white circles are actually

play00:22

traveling independently of each other in

play00:25

an oscillating motion along a straight

play00:27

line before I show you how it works I

play00:29

have a coating Mas video on the concept

play00:31

of oscillation so if you want to get a

play00:33

better understanding be sure to check

play00:34

that out first let's start by drawing

play00:36

the biggest circle so I'm going to use a

play00:38

function called ellipse and ellipse

play00:41

takes in a total of four arguments the

play00:43

first two are the X and Y coordinates of

play00:45

the center of the circle and then the

play00:47

third and the fourth are the width and

play00:48

the height of the circle so I want the

play00:50

circle to be in the middle of my canvas

play00:54

so we're going to put width / by two and

play00:56

height / two here then let's do the size

play00:59

to to be 300 by

play01:01

300 so actually instead of putting width

play01:05

divided two and height divided by two

play01:07

here I want to translate or I want to

play01:10

move the origin point from the top left

play01:13

corner of my canvas to the Middle Point

play01:16

here so I can do that by using a

play01:18

translate

play01:19

function and then put in the two

play01:21

arguments as the new origin point so

play01:23

let's do width / two and height divid

play01:26

two and once we move the origin point we

play01:29

can put in 0a 0

play01:32

here all right so everything is still

play01:34

the same next I want to draw a smaller

play01:38

Circle and this smaller circle is going

play01:40

to travel along the circular path here

play01:43

so we need to figure out what are the X

play01:45

and Y location of this smaller Circle

play01:47

and we can do that by using the rules of

play01:49

trigonometry you can watch my video on

play01:52

how we convert from Polar coordinates to

play01:54

cartisian coordinates but essentially we

play01:56

need two equations and these two

play01:58

equations are X = = to R * cosine of

play02:01

angle and y = r * s of angle so let's us

play02:07

also

play02:09

declare X and Y variables as well as

play02:14

angle let's set angle to zero and also

play02:17

how about we

play02:19

also do radius equals to 150 and then

play02:23

instead of putting 300 here let's do R *

play02:25

2 and R * 2 and do not forget to draw

play02:29

this small small circle so let's do it

play02:31

at X and Y and then give it a size of

play02:35

20 a few tweaks so I'm going to put this

play02:39

after and then I'm going to not color

play02:42

the big circle and I want the smaller

play02:44

Circle to be the color

play02:47

red and also I am going to use the angle

play02:51

mode degrees so I'm going to also write

play02:54

a function called angle mode and put in

play02:57

degrees here so right now this red

play02:59

circle is not traveling anywhere it's a

play03:01

stuck at angle equals to zero so let's

play03:03

move it we're going to move it based on

play03:06

the location of the mouse we can map

play03:08

using the equation called map and we're

play03:11

going to map what we're going to map the

play03:13

mouse X location from 0 to

play03:16

width to the degrees of 0 to

play03:20

360 all right so now it's traveling in

play03:23

the clockwise Direction along this

play03:25

circular path now I'm going to draw two

play03:27

more circles and these two cires

play03:30

one is going to follow the X location of

play03:32

the red circle and the other one is

play03:34

going to follow the Y location of the

play03:36

red circle so we can do that

play03:39

by calling the UST function again and

play03:42

instead of putting X comma y we're going

play03:44

to do X comma 0 Let's do the same size

play03:47

then the second one is going to be 0

play03:50

comma y same size and let's give it a

play03:53

color how about

play03:58

white all right so right now one is

play04:03

following the X location of the red

play04:05

circle and the other one is following

play04:08

the Y location of the red circle you can

play04:12

see that it's traveling what along a

play04:14

straight line right along the X and Y

play04:18

AIS how about we draw the axis out as

play04:21

well so we can use a function called

play04:23

line and line takes in a total of four

play04:25

arguments the X and Y coordinates of the

play04:27

first point and the x and y coordinate

play04:29

of of the second point where these two

play04:31

points are connected to make a line so

play04:33

let's do the xaxis first so it's going

play04:35

to be R comma 0 and R comma

play04:40

0 all right and then another one will be

play04:43

0 comma R and0 comma

play04:49

R and what these two white circles are

play04:52

doing is that it is traveling in an

play04:54

oscillating motion right around a center

play04:56

point which is the origin point

play05:00

and this oscillating motion is called a

play05:02

simple harmonic motion which can be

play05:04

represented by the sign and the cosine

play05:06

function which is exactly what these two

play05:10

locations are right because X = to R *

play05:13

cosine of angle and y = to R Time s of

play05:16

angle what we're going to do next is

play05:18

that we're going to draw another set of

play05:20

X and Y AIS but we're going to rotate it

play05:24

counterclockwise by

play05:25

45Β° so let's copy these two lines of

play05:28

code

play05:32

and use a rotate function and we're

play05:34

going to rotate by 45Β° counterclockwise

play05:38

and that's why I need to put -45 here

play05:41

all right so let's give it a different

play05:44

color so we can see so let's

play05:48

do

play05:52

blue for the Y AIS and green for the x-

play05:58

axis

play06:00

and then we need to also reset it back

play06:04

to black here all right let's try

play06:07

this and I'm also going to comment this

play06:11

out first all right so now we shift so

play06:15

if we don't shift it at all right

play06:18

so we can see that the x axis is blue

play06:22

and then the Y AIS is green and now we

play06:24

shift it by

play06:26

45Β° now what if we want to draw two more

play06:29

of these white circles have them travel

play06:31

along this new axis but still follow the

play06:35

location of the red circle what if we

play06:38

start by declaring two more variables

play06:42

the new X and Y coordinates so let's do

play06:44

X2 equals to and we're going to just set

play06:47

it equals to the same equation first and

play06:51

let's see what happens so Y2 = to R * s

play06:56

of

play06:58

angle and then we're going to draw it

play07:00

out as well so we're going to draw the

play07:03

exact same thing as

play07:06

this but we're going to replace it by X2

play07:09

and Y2 and then let's give it the same

play07:11

color of

play07:16

white

play07:18

angle all right so and then we're going

play07:21

to have it

play07:24

move okay so this is not exactly what we

play07:27

want right because because right now

play07:31

everything is shifted by 45Β° meaning

play07:34

that at angle equals to Z the x

play07:37

coordinate is at equals to one right and

play07:39

then the y coordinate is equals to zero

play07:42

which is exactly the same thing as what

play07:44

it is on the main axis so we need to

play07:47

change something in these two equations

play07:49

such

play07:50

that the circles are following the X and

play07:53

Y locations of the red circle right so

play07:56

what we want is that let's change this

play07:58

to 45 and comment this out again so at

play08:01

angle equals to 45 the locations of

play08:04

these two circles need to be shifted a

play08:06

little bit right so we want actually

play08:08

this circle to be equals to one and we

play08:10

want the circle to be equals to zero

play08:13

what that means is in terms of X and Y

play08:15

as a function of angle we want Y at

play08:18

angle equal to 45 to have a value of 1

play08:21

and X at angle equal to 45 to have a

play08:24

value of zero if you look at the sign

play08:26

and the cosine graphs here as a function

play08:28

of angle y = 1 and X = to 0 when angle

play08:33

equals to 90 so we need to tweak these

play08:35

two equations such that inside the

play08:37

parentheses it equals to 90 when we give

play08:40

an argument of 45 and we can do that by

play08:43

adding the shifting angle or in this

play08:46

case 45 inside the parentheses so now y

play08:49

of angle equals to S of angle + 45 and X

play08:54

of angle equals to cosine of angle + 45

play08:58

so let's first declare a new variable

play09:00

let's call it shifting

play09:04

angle and set it to 45 and then our

play09:07

angle will be at zero right and then we

play09:10

need to add

play09:12

shifting angle

play09:24

here all right

play09:28

so

play09:32

all right so at angle equals to 45 right

play09:34

and this is what

play09:40

happens now we can start adding more

play09:42

axis so let's first we don't need this

play09:46

green and blue stroke colors anymore we

play09:49

want everything to be black so instead

play09:51

of declaring the shifting angle as a

play09:53

variable that is set to 45 we're going

play09:55

to make it an

play09:57

array so we're going to change it to to

play09:59

an array and then we're going to declare

play10:01

another variable called num axis and

play10:05

this is going to be a variable that

play10:07

controls the number of axis that we want

play10:09

so let's start with two so two will be

play10:12

what we have right now right so the main

play10:14

axis and then another one that is

play10:16

shifted

play10:17

45Β° and then in the setup function we're

play10:20

going to calculate the shifting angles

play10:23

based on the number of axis that we want

play10:25

so let's use a four Loop so four let IAL

play10:29

z i less than num of axis

play10:34

i++ and then the shifting angle of I

play10:38

will be equals to so the equation that

play10:41

we're going to use is going to be I * 90

play10:44

/ num of axis so let's try to understand

play10:48

this so if we only have this one axis

play10:51

which is the main axis I will be equals

play10:53

to 0 right so 0 * 90 / number of axes

play10:57

will be equals to 0 meaning that we're

play11:00

not shifting it anywhere and now we want

play11:04

the second axis to be shifted 45Β° right

play11:08

so if I equal to 1 and then 1 / 2 right

play11:13

is 1 and2 1/2 * 90 is 45 so now that we

play11:18

changed the shifting angle from a

play11:20

variable to an array we also need to

play11:22

change the X2 and Y2 variables to an

play11:24

array as well so let's start by

play11:27

declaring X2 and Y 2 as an

play11:34

array and we are going to use the four

play11:37

loop as well to populate the X2 and Y2

play11:42

array so let's do for let I equal to

play11:46

zero I less than the num

play11:49

axis

play11:51

i++ and then we're going to copy these

play11:53

two lines of

play11:55

code in here it's going to be X2 of I =

play11:59

to R * cosine of angle plus shifting

play12:02

angle of I right and then Y 2 of I = to

play12:07

R * s of angle plus shifting angle of i

play12:11

as well and now we're going to draw the

play12:13

two smaller white circles based on the

play12:15

X2 and Y2

play12:17

location

play12:19

so we have it here already so how about

play12:22

we just first we need to move this down

play12:27

here

play12:32

and then we want to copy the set of

play12:36

code and then put it in

play12:40

here so what do we have here so now we

play12:42

have calculated the X2 and Y2 locations

play12:45

of the smaller white circles we want to

play12:48

rotate it by negative of the shifting

play12:51

angle of I right and then we also need

play12:55

to put X2 of I here and then Y 2 of I

play13:00

let's try

play13:02

this all right so now it's still the

play13:07

same we can clean this up a little bit

play13:09

more too because right now we also

play13:13

have this set of codes that draw the

play13:16

main axis and the main two smaller

play13:18

circles which now it's just a repeated

play13:21

of what we have in here so let's delete

play13:26

this all right

play13:29

and now all we have to do is that we

play13:31

need to change the number of the axis

play13:34

here so let's do

play13:36

three ooh what's going on

play13:38

here all right don't freak out the thing

play13:41

that is going on here is that we forgot

play13:43

to put in two functions that are very

play13:46

important when we do transformation

play13:48

which is push and pop so let's put it

play13:50

here

play13:51

push and pop and essentially the push

play13:55

and pop function push save the

play13:57

transformation and then pop returns it

play13:59

back to the

play14:00

original state that we are in and we

play14:03

need to put these two functions every

play14:05

time we call a new Circle because we

play14:08

want to save the new transformation

play14:10

which is the rotating of Shifting angle

play14:12

right and then we want to return it back

play14:14

before we rotate it again so let's try

play14:18

this all

play14:21

righty and now all we have to do is we

play14:23

just play around with the number of axis

play14:26

that we want

play14:30

okay and so now instead of having it

play14:32

mapped to our Mouse location what if we

play14:35

just increment it by

play14:40

one all

play14:44

right the last piece which is the fun

play14:46

piece is how do we beautify this I am

play14:49

going to change the color of my

play14:51

background to this turquoise color and

play14:56

then I'm going to change the color of

play14:59

my main Circle to be black and then how

play15:03

about let's give the

play15:06

stroke white color and let's do

play15:08

transparency to be 100 so it's not very

play15:11

very bright and yeah just do that and

play15:16

this is just like the original image

play15:17

that I had shown

play15:19

you um and then let's do let's give it

play15:22

just a huge number of axes here and

play15:26

there you go and this is just using the

play15:29

concept of oscillation where the circles

play15:31

just travel back and forth on a straight

play15:33

path and it gives this illusion that

play15:35

it's travel in a circle motion which is

play15:38

really cool so give it a try

Rate This
β˜…
β˜…
β˜…
β˜…
β˜…

5.0 / 5 (0 votes)

Related Tags
Coding TutorialOscillation ConceptTrigonometryCircle AnimationPolar CoordinatesCartesian CoordinatesSimple Harmonic MotionGraphic DesignInteractive ArtJavaScript Animation