Math

Note

Many math functions available in Processing are already included in Python’s math standard library. There methods aren’t listed here. Please refer to the documentation for the math standard library.

Vector

class p5.Vector(x, y, z=0)

Describes a vector in two or three dimensional space.

A Vector – specifically an Euclidean (or geometric) vector – in two or three dimensional space is a geometric entity that has some magnitude (or length) and a direction.

Examples:

>>> vec_2d = Vector(3, 4)
>>> vec_2d
Vector(3.00, 4.00, 0.00)

>>> vec_3d = Vector(2, 3, 4)
>>> vec_3d
Vector(2.00, 3.00, 4.00)
Parameters:
  • x (int or float) – The x-component of the vector.
  • y (int or float) – The y-component of the vector.
  • z (int or float) – The z-component of the vector (0 by default; only required for 3D vectors; )
__abs__()

Return the magnitude of the vector.

__add__(other)

Add the location of one point to that of another.

Examples:

>>> p = Vector(2, 3, 6)
>>> q = Vector(3, 4, 5)
>>> p + q
Vector(5.00, 7.00, 11.00)
Parameters:other
Returns:The point obtained by adding the corresponding components of the two vectors.
__eq__(other)

Return self==value.

__getitem__(key)

Return self[key].

__init__(x, y, z=0)

Initialize self. See help(type(self)) for accurate signature.

__iter__()

Return the components of the vector as an iterator.

Examples:

>>> p = Vector(2, 3, 4)
>>> print([ c for c in p])
[2, 3, 4]
__mul__(k)

Multiply the point by a scalar.

Examples:

>>> p = Vector(2, 3, 6)
>>> p * 2
Vector(4.00, 6.00, 12.00)

>>> 2 * p
Vector(4.00, 6.00, 12.00)

>>> p * p
Traceback (most recent call last):
    ...
TypeError: Can't multiply/divide a point by a non-numeric.

>>> p = Vector(2, 3, 6)
>>> -p
Vector(-2.00, -3.00, -6.00)

>>> p = Vector(2, 3, 6)
>>> p / 2
Vector(1.00, 1.50, 3.00)
Parameters:k (int, float) –
Returns:The vector obtained by multiplying each component of self by k.
Raises:TypeError – When k is non-numeric.
__neg__()

Negate the vector.

__repr__()

Return a nicely formatted representation string

__rmul__(other)

Return value*self.

__str__()

Return a nicely formatted representation string

__sub__(other)

Subtract the location of one point from that of another.

Examples:

>>> p = Vector(2, 3, 6)
>>> q = Vector(3, 4, 5)
>>> p - q
Vector(-1.00, -1.00, 1.00)
Parameters:other
Returns:The vector obtained by subtracteing the corresponding components of the vector from those of another.
__truediv__(other)

Divide the vector by a scalar.

angle

The angle of rotation of the vector (in radians).

This attribute isn’t available for three dimensional vectors.

Examples:

>>> from math import pi, isclose
>>> p = Vector(1, 0, 0)
>>> isclose(p.angle, 0)
True

>>> p = Vector(0, 1, 0)
>>> isclose(p.angle, pi/2)
True

>>> p = Vector(1, 1, 1)
>>> p.angle
Traceback (most recent call last):
    ...
ValueError: Can't compute the angle for a 3D vector.

>>> p = Vector(1, 1)
>>> isclose(p.angle, pi/4)
True
>>> p.angle = pi/2
>>> isclose(p.angle, pi/2)
True

>>> p = Vector(1, 1)
>>> isclose(p.angle, pi/4)
True
>>> p.rotate(pi/4)
>>> isclose(p.angle, pi/2)
True
Raises:ValueError – If the vector is three-dimensional
angle_between(other)

Calculate the angle between two vectors.

Examples:

>>> from math import degrees
>>> k = Vector(0, 1)
>>> j = Vector(1, 0)
>>> degrees(k.angle_between(j))
90.0
Parameters:other (Vector) –
Returns:The angle between self and other (in radians)
Return type:float
copy()

Return a copy of the current point.

Returns:A copy of the current point.
Return type:Vector
cross(other)

Return the cross product of the two vectors.

Examples:

>>> i = Vector(1, 0, 0)
>>> j = Vector(0, 1, 0)
>>> i.cross(j)
Vector(0.00, 0.00, 1.00)
Parameters:other (Vector) –
Returns:The vector perpendicular to both self and other i.e., the vector obtained by taking the cross product of self and other.
Return type:Vector
dist(other)

Return the distance between two points.

Returns:The distance between the current point and the given point.
Return type:float
distance(other)

Return the distance between two points.

Returns:The distance between the current point and the given point.
Return type:float
dot(other)

Compute the dot product of two vectors.

Examples:

>>> p = Vector(2, 3, 6)
>>> q = Vector(3, 4, 5)
>>> p.dot(q)
48
>>> p @ q
48
Parameters:other (Vector) –
Returns:The dot product of the two vectors.
Return type:int or float
classmethod from_angle(angle)

Return a new unit vector with the given angle.

Parameters:angle (float) – Angle to be used to create the vector (in radians).
lerp(other, amount)

Linearly interpolate from one point to another.

Parameters:
  • other – Point to be interpolate to.
  • amount (float) – Amount by which to interpolate.
Returns:

Vector obtained by linearly interpolating this vector to the other vector by the given amount.

limit(upper_limit=None, lower_limit=None)

Limit the magnitude of the vector to the given range.

Parameters:
  • upper_limit (float) – The upper limit for the limiting range (defaults to None).
  • lower_limit (float) – The lower limit for the limiting range (defaults to None).
magnitude

The magnitude of the vector.

Examples:

>>> p = Vector(2, 3, 6)
>>> p.magnitude
7.0

>>> abs(p)
7.0

>>> p.magnitude = 14
>>> p
Vector(4.00, 6.00, 12.00)

>>> p.normalize()
>>> print(p)
Vector(0.29, 0.43, 0.86)
magnitude_sq

The squared magnitude of the vector.

normalize()

Set the magnitude of the vector to one.

classmethod random_2D()

Return a random 2D unit vector.

classmethod random_3D()

Return a new random 3D unit vector.

rotate(theta)

Rotates the vector by an angle.

Parameters:theta (float or int) – Angle (in radians).
x

The x-component of the point.

y

The y-component of the point.

z

The z-component of the point.

Calculation

ceil()

p5.ceil()

Return the ceiling of x as an Integral.

This is the smallest integer >= x.

constrain()

p5.constrain(amount, low, high)

Constrain the given value in the specified range.

Examples

>>> constrain(8, 1, 5)
5

>>> constrain(5, 1, 5)
5

>>> constrain(3, 1, 5)
3

>>> constrain(1, 1, 5)
1

>>> constrain(-3, 1, 5)
1
Parameters:
  • amount – The the value to be contrained.
  • low – The lower constain.
  • high – The upper constain.

dist()

p5.dist(point_1, point_2)

Return the distance between two points.

Examples

>>> distance((0, 0, 0), (2, 3, 6))
7.0

>>> distance((2, 3, 6), (2, 3, 6))
0.0

>>> distance((6, 6, 6), (2, 3, 6))
5.0
Parameters:
  • point_1 (tuple) –
  • point_2 (tuple) –
Returns:

The distance between two points

Return type:

float

exp()

p5.exp()

Return e raised to the power of x.

floor()

p5.floor()

Return the floor of x as an Integral.

This is the largest integer <= x.

lerp()

p5.lerp(start, stop, amount)

Linearly interpolate the start value to the stop value.

Examples

>>> lerp(0, 10, 0.0)
0.0

>>> lerp(0, 10, 0.5)
5.0

>>> lerp(0, 10, 0.8)
8.0

>>> lerp(0, 10, 1.0)
10.0
Parameters:
  • start – The start value
  • stop – The stop value
  • amount (float) – The amount by which to interpolate. (\(0 \leq amount \leq 1\)).

log()

p5.log(x[, base=math.e])

Return the logarithm of x to the given base.

If the base not specified, returns the natural logarithm (base e) of x.

sq()

p5.sq(number)

Square a number.

Examples

>>> square(-25)
625

>>> square(0)
0

>>> square(13)
169
Parameters:number (float) – The number to be squared.
Returns:The square of the number.
Return type:float

magnitude()

p5.magnitude(x, y, z=0)

Return the magnitude of the given vector.

Examples

>>> magnitude(3, 4)
5.0

>>> magnitude(2, 3, 6)
7.0

>>> magnitude(0, 0, 0)
0.0
Parameters:
  • x (float) – The x-component of the vector.
  • y (float) – The y-component of the vector.
  • z (float) – The z-component of the vector (defaults to 0).
Returns:

The magnitude of the vector.

Return type:

float

remap()

p5.remap(value, source_range, target_range)

Remap a value from the source range to the target range.

Examples

>>> remap(50, (0, 100), (0, 10))
5.0

>>> remap(5, (0, 10), (0, 100))
50.0

>>> remap(5, (0, 10), (10, 20))
15.0

>>> remap(15, (10, 20), (0, 10))
5.0
Parameters:
  • value – The value to be remapped.
  • source_range (tuple) – The source range for value
  • target_range (tuple) – The target range for value

normalize()

p5.normalize(value, low, high)

Normalize the given value to the specified range.

Examples

>>> normalize(10, 0, 100)
0.1

>>> normalize(0.3, 0, 1)
0.3

>>> normalize(100, 0, 100)
1.0

>>> normalize(1, 1, 15)
0.0
Parameters:
  • value (float) –
  • low (float) – The lower bound for the range.
  • high (float) – The upper bound for the range.

sqrt()

p5.sqrt()

Return the square root of x.

Trigonometry

acos()

p5.acos()

Return the arc cosine (measured in radians) of x.

asin()

p5.asin()

Return the arc sine (measured in radians) of x.

atan()

p5.atan()

Return the arc tangent (measured in radians) of x.

atan2()

p5.atan2()

Return the arc tangent (measured in radians) of y/x.

Unlike atan(y/x), the signs of both x and y are considered.

cos()

p5.cos()

Return the cosine of x (measured in radians).

degrees()

p5.degrees()

Convert angle x from radians to degrees.

radians()

p5.radians()

Convert angle x from degrees to radians.

sin()

p5.sin()

Return the sine of x (measured in radians).

tan()

p5.tan()

Return the tangent of x (measured in radians).

Random

noise()

p5.noise(x, y=0, z=0)

Return perlin noise value at the given location.

Parameters:
  • x (float) – x-coordinate in noise space.
  • y (float) – y-coordinate in noise space.
  • z (float) – z-coordinate in noise space.
Returns:

The perlin noise value.

Return type:

float

noise_detail()

p5.noise_detail(octaves=4, falloff=0.5)

Adjust the level of noise detail produced by noise().

Parameters:
  • octaves (int) – The number of octaves to compute the noise for (defaults to 4).
  • falloff (float) –
Note:

For falloff values greater than 0.5, noise() will return values greater than 1.0.

noise_seed()

p5.noise_seed(seed)

Set the seed value for noise()

By default noise() produes different values each time the sketch is run. Setting the seed parameter to a constant will make noise() return the same values each time the sketch is run.

Parameters:seed (int) – The required seed value.

random_uniform()

p5.random_uniform(high=1, low=0)

Return a uniformly sampled random number.

Parameters:
  • high (float) – The upper limit on the random value (defaults to 1).
  • low (float) – The lowe limit on the random value (defaults to 0).
Returns:

A random number between low and high.

Return type:

float

random_gaussian()

p5.random_gaussian(mean=0, std_dev=1)

Return a normally sampled random number.

Parameters:
  • mean (float) – The mean value to be used for the normal distribution (defaults to 0).
  • std_dev (float) – The standard deviation to be used for the normal distribution (defaults to 1).
Returns:

A random number selected from a normal distribution with the given mean and std_dev.

Return type:

float

random_seed()

p5.random_seed(seed)

Set the seed used to generate random numbers.

Parameters:seed (int) – The required seed value.