MODULE v1.8.0

math

pykraken.math

Math related functions

Reference

PYTHON API
angle_between(a: Vec2, b: Vec2) -> float

Calculate the angle between two vectors.

Args

  • a (Vec2) — The first vector.
  • b (Vec2) — The second vector.

Returns

  • float — The angle between the vectors in radians [0, π].

clamp

function2 overloads
clamp(vec: Vec2, min_vec: Vec2, max_vec: Vec2) -> Vec2
clamp(value: float, min_val: float, max_val: float) -> float

Overloaded function.

  1. clamp(vec: Vec2, min_vec: Vec2, max_vec: Vec2) -> Vec2

Clamp a vector between two boundary vectors.

Args

  • vec (Vec2) — The vector to clamp.
  • min_vec (Vec2) — The minimum boundary vector.
  • max_vec (Vec2) — The maximum boundary vector.

Returns

  • Vec2 — A new vector with components clamped between min and max.
  1. clamp(value: float, min_val: float, max_val: float) -> float

Clamp a value between two boundaries.

Args

  • value (float) — The value to clamp.
  • min_val (float) — The minimum boundary.
  • max_val (float) — The maximum boundary.

Returns

  • float — The clamped value.

cross

function
cross(a: Vec2, b: Vec2) -> float

Calculate the 2D cross product of two vectors. (a.x * b.y - a.y * b.x)

Args

  • a (Vec2) — The first vector.
  • b (Vec2) — The second vector.

Returns

  • float — The 2D cross product.

dot

function
dot(a: Vec2, b: Vec2) -> float

Calculate the dot product of two vectors.

Args

  • a (Vec2) — The first vector.
  • b (Vec2) — The second vector.

Returns

  • float — The dot product (a.x * b.x + a.y * b.y).

from_polar

function
from_polar(angle: float, radius: float) -> Vec2

Convert polar coordinates to a Cartesian vector.

Args

  • angle (float) — The angle in radians.
  • radius (float) — The radius/distance from origin.

Returns

  • Vec2 — The equivalent Cartesian vector.

lerp

function2 overloads
lerp(a: Vec2, b: Vec2, t: float) -> Vec2
lerp(a: float, b: float, t: float) -> float

Overloaded function.

  1. lerp(a: Vec2, b: Vec2, t: float) -> Vec2

Linearly interpolate between two Vec2s.

Args

  • a (Vec2) — The start vector.
  • b (Vec2) — The end vector.
  • t (float) — The interpolation factor [0.0, 1.0].

Returns

  • Vec2 — The interpolated vector.
  1. lerp(a: float, b: float, t: float) -> float

Linearly interpolate between two values.

Args

  • a (float) — The start value.
  • b (float) — The end value.
  • t (float) — The interpolation factor [0.0, 1.0].

Returns

  • float — The interpolated value.

move_toward

function
move_toward(current: float, target: float, delta: float) -> float

Move a value toward a target by a maximum delta.

Args

  • current (float) — The current value.
  • target (float) — The target value.
  • delta (float) — The maximum amount to move toward the target.

Returns

  • float — The new value after moving toward the target.

remap

function
remap(in_min: float, in_max: float, out_min: float, out_max: float, value: float) -> float

Remap a value from one range to another.

Args

  • in_min (float) — Input range minimum.
  • in_max (float) — Input range maximum.
  • out_min (float) — Output range minimum.
  • out_max (float) — Output range maximum.
  • value (float) — The value to remap.

Returns

  • float — The remapped value in the output range.

Raises

  • ValueError — If in_min equals in_max.

Find your next step

Start typing to search the manual.