# Math#

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

Clamp a number between a range.

arcade.math.get_angle_degrees(x1: float, y1: float, x2: float, y2: 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

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) [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, v2: float, u: float) [source]#

linearly interpolate between two values

arcade.math.lerp_angle(start_angle: float, end_angle: float, u: 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.lerp_vec(v1: , v2: , u: float) [source]#

Returns a random angle in degrees.

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

Parameters:
• angle – The angle to spread from

Returns:

A random angle in degrees

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

Returns:

A random point in the circle

arcade.math.rand_in_rect(bottom_left: , width: float, height: float) [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

Generate a point on a circle.

Parameters:
• center – The center of the circle

Returns:

A random point on the circle

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) [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

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

Parameters:
• angle – The angle to spread from

• 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) [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

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