Math#

arcade.math.clamp(a, low: float, high: float) float[source]#

Clamp a number between a range.

arcade.math.get_angle_degrees(x1: float, y1: float, x2: float, y2: float) float[source]#

Get the angle in degrees between two points.

Parameters:
  • x1 – x coordinate of the first point

  • y1 – y coordinate of the first point

  • x2 – x coordinate of the second point

  • y2 – y coordinate of the second point

arcade.math.get_angle_radians(x1: float, y1: float, x2: float, y2: float) float[source]#

Get the angle in radians between two points.

Parameters:
  • x1 – x coordinate of the first point

  • y1 – y coordinate of the first point

  • x2 – x coordinate of the second point

  • y2 – y coordinate of the second point

arcade.math.get_distance(x1: float, y1: float, x2: float, y2: float) float[source]#

Get the distance between two points.

Parameters:
  • x1 – x coordinate of the first point

  • y1 – y coordinate of the first point

  • x2 – x coordinate of the second point

  • y2 – y coordinate of the second point

Returns:

Distance between the two points

arcade.math.lerp(v1: float | int, v2: float | int, u: float) float[source]#

linearly interpolate between two values

arcade.math.lerp_2d(v1: Tuple[float | int, float | int] | Sequence[float | int], v2: Tuple[float | int, float | int] | Sequence[float | int], u: float) Tuple[float, float][source]#
arcade.math.lerp_3d(v1: Tuple[float | int, float | int, float | int] | Sequence[float | int], v2: Tuple[float | int, float | int, float | int] | Sequence[float | int], u: float) Tuple[float, float, float][source]#
arcade.math.lerp_angle(start_angle: float, end_angle: float, u: float) float[source]#

Linearly interpolate between two angles in degrees, following the shortest path.

Parameters:
  • start_angle – The starting angle

  • end_angle – The ending angle

  • u – The interpolation value

Returns:

The interpolated angle

arcade.math.quaternion_rotation(axis: Tuple[float, float, float], vector: Tuple[float, float, float], angle: float) Tuple[float, float, float][source]#

Rotate a 3-dimensional vector of any length clockwise around a 3-dimensional unit length vector.

This method of vector rotation is immune to rotation-lock, however it takes a little more effort to find the axis of rotation rather than 3 angles of rotation. Ref: https://danceswithcode.net/engineeringnotes/quaternions/quaternions.html.

Parameters:
  • axis – The unit length vector that will be rotated around

  • vector – The 3-dimensional vector to be rotated

  • angle – The angle in degrees to rotate the vector clock-wise by

Returns:

A rotated 3-dimension vector with the same length as the argument vector.

arcade.math.rand_angle_360_deg() float[source]#

Returns a random angle in degrees.

arcade.math.rand_angle_spread_deg(angle: float, half_angle_spread: float) float[source]#

Returns a random angle in degrees, within a spread of the given angle.

Parameters:
  • angle – The angle to spread from

  • half_angle_spread – The half angle spread

Returns:

A random angle in degrees

arcade.math.rand_in_circle(center: Tuple[float | int, float | int], radius: float) Tuple[float | int, float | int][source]#

Generate a point in a circle, or can think of it as a vector pointing a random direction with a random magnitude <= radius.

Reference: https://stackoverflow.com/a/30564123

Note

This algorithm returns a higher concentration of points around the center of the circle

Parameters:
  • center – The center of the circle

  • radius – The radius of the circle

Returns:

A random point in the circle

arcade.math.rand_in_rect(bottom_left: Tuple[float | int, float | int], width: float, height: float) Tuple[float | int, float | int][source]#

Calculate a random point in a rectangle.

Parameters:
  • bottom_left – The bottom left corner of the rectangle

  • width – The width of the rectangle

  • height – The height of the rectangle

Returns:

A random point in the rectangle

arcade.math.rand_on_circle(center: Tuple[float | int, float | int], radius: float) Tuple[float | int, float | int][source]#

Generate a point on a circle.

Parameters:
  • center – The center of the circle

  • radius – The radius of the circle

Returns:

A random point on the circle

arcade.math.rand_on_line(pos1: Tuple[float | int, float | int], pos2: Tuple[float | int, float | int]) Tuple[float | int, float | int][source]#

Given two points defining a line, return a random point on that line.

Parameters:
  • pos1 – The first point

  • pos2 – The second point

Returns:

A random point on the line

arcade.math.rand_vec_magnitude(angle: float, lo_magnitude: float, hi_magnitude: float) tuple[float, float][source]#

Returns a random vector, within a spread of the given angle.

Parameters:
  • angle – The angle to spread from

  • lo_magnitude – The lower magnitude

  • hi_magnitude – The higher magnitude

Returns:

A random vector

arcade.math.rand_vec_spread_deg(angle: float, half_angle_spread: float, length: float) tuple[float, float][source]#

Returns a random vector, within a spread of the given angle.

Parameters:
  • angle – The angle to spread from

  • half_angle_spread – The half angle spread

  • length – The length of the vector

Returns:

A random vector

arcade.math.rotate_point(x: float, y: float, cx: float, cy: float, angle_degrees: float) Tuple[float | int, float | int][source]#

Rotate a point around a center.

Parameters:
  • x – x value of the point you want to rotate

  • y – y value of the point you want to rotate

  • cx – x value of the center point you want to rotate around

  • cy – y value of the center point you want to rotate around

  • angle_degrees – Angle, in degrees, to rotate

Returns:

Return rotated (x, y) pair

arcade.math.round_fast(value: float, precision: int) float[source]#

A high performance version of python’s built-in round() function.

Note

This function is not as accurate as the built-in round() function. But is sufficient in some cases.

Example:

>>> round(3.5662457892, 1)
3.6
>>> round(3.5662457892, 2)
3.57
>>> round(3.5662457892, 3)
3.566
>>> round(3.5662457892, 4)
3.5662
Parameters:
  • value – The value to round

  • precision – The number of decimal places to round to

Returns:

The rounded value