Open In App

p5.js Math Complete Reference

Improve
Improve
Like Article
Like
Save
Share
Report

The Math object in p5.js is used to perform mathematical operations on numbers. There are many math objects exists in p5.js which are listed below.

Calculation

Description

abs() abs() function is used to calculate the absolute value of a number.
ceil() ceil() function is used to calculate the ceil value of a number.
constrain() It is used to constrain a number between a given minimum and maximum limit.
dist() dist() function calculates the Euclidean distance in 2D or 3D.
floor() floor() function is used to calculate the floor value of a number.
lerp() lerp() function is used to find a number between two numbers.
log() It is used to get the natural logarithm (of base “e”) of any number taken as input for the parameter of log() function.
mag() mag() function is used to find the magnitude or length of a vector.
max() It is used to get the maximum value among sequence of numbers or two given numbers.
min() min() function is used to get the minimum value from the sequence of numbers.
pow() pow() function is used to calculate the power of a number to the given number.
round() round() function is used to calculate the round value of a number.
sq() sq() function is used to calculate the square value of a number.
sqrt() sqrt() function is used to get the square root of any input number.
fract() fract() function is used to find the fractional part of a number.

Vector

Description

createVector() It creates the new p5 vector which contains both magnitude and direction.

Noise

Description

noise() noise() function is used to return a number generated by Perlin noise at the given coordinates.
noiseSeed() noiseSeed() function is used to set a seed value for the noise() function.

Random

Description

randomSeed() randomSeed() function is used to return a random number each time when run the program.
random() It is used to return a random floating point number between ranges given as the parameter.
randomGaussian() It is used to return a random value fitting a Gaussian or normal distribution.

Trigonometry

Description

acos() acos() function is used to calculate the arc cosine value.
asin() asin() function is used to calculate the inverse of sine (arc sine).
atan() atan() function is used to calculate the inverse of tan() or arc tangent.
atan2() It is used to calculate the angle from a specified point of origin measured from the positive x-axis.
cos() cos() function is used to calculate the cosine value of an angle.
tan() tan() function is used to calculate the tangent value of an angle.
degrees() It converts a given radian measurement value to its corresponding value in degrees.
radians() It converts a given degree measurement value to its corresponding value in radians.
angleMode() angleMode() function is used to set the mode in which the angle is interpreted.

Last Updated : 23 Aug, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads