Math Module

class box2d.math.VectorLike(*args, **kwargs)[source]

A protocol representing a vector-like object.

A vector-like object must:
  • Support indexing via __getitem__ (for indices 0 and 1) returning a float.

  • Be iterable, yielding floats.

  • Have a length of exactly 2.

class box2d.math.AABB(lower: ~box2d.math.VectorLike = Vec2(inf.0, inf.0), upper: ~box2d.math.VectorLike = Vec2(-inf.0, -inf.0))[source]

Axis-Aligned Bounding Box (AABB) for 2D spatial queries.

Features: - Immutable design (all operations return new instances) - Y-up coordinate system compatibility - Merge operations with other AABBs/points - Intersection calculations - Validity and containment checks

Example

>>> box = AABB((0, 0), (2, 3))
>>> box.center
Vec2(1.0, 1.5)
__init__(lower: ~box2d.math.VectorLike = Vec2(inf.0, inf.0), upper: ~box2d.math.VectorLike = Vec2(-inf.0, -inf.0))[source]

Initialize AABB with lower and upper bounds.

Parameters:
  • lower (VectorLike) – Minimum coordinates (x1, y1)

  • upper (VectorLike) – Maximum coordinates (x2, y2)

Note

Default creates invalid AABB, use from_points for valid initialization

Example

>>> AABB((0, 0), (2, 2))
AABB(lower=Vec2(0.0, 0.0), upper=Vec2(2.0, 2.0))
property b2AABB

Box2D b2AABB equivalent (managed by FFI).

Example

>>> aabb = AABB((1,2), (3,4))
>>> ca = aabb.b2AABB
>>> ca.lowerBound.x, ca.upperBound.y
(1.0, 4.0)
property center

Calculate geometric center of AABB.

Returns:

Center point coordinates

Return type:

Vec2

Example

>>> AABB((0,0), (2,2)).center
Vec2(1.0, 1.0)
contains(other: AABB | VectorLike) bool[source]

Check if another AABB or point is fully contained within this one.

Parameters:

other – AABB or point to test containment

Returns:

True if other AABB or point is completely inside

Return type:

bool

Example

>>> AABB((0,0), (5,5)).contains(AABB((1,1), (3,3)))
True
>>> AABB((0,0), (5,5)).contains((3,3))
True
expanded(margin: float) AABB[source]

Create uniformly expanded/contracted AABB.

Parameters:

margin – Expansion amount (positive expands, negative contracts)

Returns:

New AABB expanded on all sides

Return type:

AABB

Example

>>> AABB((0,0), (2,2)).expanded(1)
AABB(lower=Vec2(-1.0, -1.0), upper=Vec2(3.0, 3.0))
classmethod from_b2AABB(b2_aabb)[source]

Create AABB from Box2D’s b2AABB structure.

Parameters:

b2_aabb – FFI pointer to b2AABB C struct

Example

>>> aabb_c = ffi.new("b2AABB*", ((0,0), (2,3)))
>>> AABB.from_b2AABB(aabb_c)
AABB(lower=Vec2(0.0, 0.0), upper=Vec2(2.0, 3.0))
classmethod from_points(points: Iterable[VectorLike]) AABB[source]

Construct minimal AABB containing all given points.

Parameters:

points – Collection of Vec2 or coordinate tuples

Returns:

Bounding box containing all points

Return type:

AABB

Example

>>> AABB.from_points([(0,1), (2,3), (-1,5)])
AABB(lower=Vec2(-1.0, 1.0), upper=Vec2(2.0, 5.0))
property half_size

Get half dimensions from center to edges.

Returns:

(width/2, height/2) vector

Return type:

Vec2

Example

>>> AABB((0,0), (2,4)).half_size
Vec2(1.0, 2.0)
property height: float

Calculate vertical span of the AABB.

Returns:

Difference between upper and lower y-coordinates

Return type:

float

Example

>>> AABB((1, 2), (3, 5)).height
3.0
property is_valid

Check if AABB represents a valid bounded region.

Returns:

True if lower <= upper and all coordinates finite

Return type:

bool

Example

>>> AABB((0,0), (1,1)).is_valid
True
>>> AABB((1,1), (0,0)).is_valid
False
property lower: Vec2

Minimum boundary point (read-only).

Returns:

Copy of lower bounds vector

Return type:

Vec2

Example

>>> AABB((1,2), (3,4)).lower
Vec2(1.0, 2.0)
merge(other: AABB | VectorLike) AABB[source]

Create new AABB encompassing this and another AABB/point.

Parameters:

other – AABB or point to include

Returns:

Expanded bounding box

Return type:

AABB

Example

>>> AABB((0,0), (1,1)).merge(AABB((2,2), (3,3)))
AABB(lower=Vec2(0.0, 0.0), upper=Vec2(3.0, 3.0))
overlaps(other: AABB) bool[source]

Check if another AABB overlaps with this one.

Parameters:

other – AABB to test overlap

Returns:

True if AABBs overlap

Return type:

bool

Example

>>> AABB((0,0), (2,2)).overlaps(AABB((1,1), (3,3)))
True
translated(offset: VectorLike) AABB[source]

Create translated AABB by given offset.

Parameters:

offset – Translation vector (Vec2 or tuple/list)

Returns:

Shifted AABB

Return type:

AABB

Example

>>> AABB((0,0), (2,2)).translated((1, -1))
AABB(lower=Vec2(1.0, -1.0), upper=Vec2(3.0, 1.0))
property upper: Vec2

Maximum boundary point (read-only).

Returns:

Copy of upper bounds vector

Return type:

Vec2

Example

>>> AABB((1,2), (3,4)).upper
Vec2(3.0, 4.0)
property width: float

Calculate horizontal span of the AABB.

Returns:

Difference between upper and lower x-coordinates

Return type:

float

Example

>>> AABB((1, 2), (4, 5)).width
3.0
class box2d.math.Mat22(*args: float | VectorLike)[source]

A 2x2 matrix for linear transformations.

Features:
  • Column-major storage

  • Matrix-vector/matrix multiplication

  • Matrix inversion and transpose

  • Rotation/identity matrix creation

  • Accepts various input formats (tuples, lists, Vec2s)

Example

>>> Mat22(1, 2, 3, 4)
Mat22(Vec2(1.0, 2.0), Vec2(3.0, 4.0))
__init__(*args: float | VectorLike)[source]

Initialize matrix from multiple formats.

Parameters:

*args

Supported formats: - 4 scalars (a, b, c, d) =>

[[a b]
 [c d]]
  • 2 column vectors

  • Single iterable with 4 elements

Example

>>> Mat22(1, 2, 3, 4)  # Scalar components
Mat22(Vec2(1.0, 2.0), Vec2(3.0, 4.0))
>>> Mat22(Vec2(1,2), Vec2(3,4))  # Column vectors
Mat22(Vec2(1.0, 2.0), Vec2(3.0, 4.0))
property b2Mat22

Box2D b2Mat22 equivalent (managed by FFI).

Example

>>> mat = Mat22(1,2,3,4)
>>> cm = mat.b2Mat22
>>> cm.cx.x, cm.cy.y
(1.0, 4.0)
property columns: tuple[Vec2, Vec2]

Matrix column vectors as a tuple.

Returns:

First and second column vectors

Return type:

(Vec2, Vec2)

Example

>>> Mat22(1, 2, 3, 4).columns
(Vec2(1.0, 2.0), Vec2(3.0, 4.0))
property determinant: float

Matrix determinant (scalar value indicating invertibility).

Calculated as: (cx.x * cy.y) - (cy.x * cx.y)

Returns:

Determinant value

Return type:

float

Example

>>> Mat22(1, 0, 0, 1).determinant
1.0
classmethod from_angle(angle: float) Mat22[source]

Create rotation matrix from angle.

Parameters:

angle (float) – Rotation angle in radians

Returns:

Rotation matrix

Return type:

Mat22

Example

>>> Mat22.from_angle(math.pi/2)
Mat22(Vec2(0.0, 1.0), Vec2(-1.0, 0.0))
classmethod from_b2Mat22(b2_mat22)[source]

Create Mat22 from Box2D’s b2Mat22 structure.

Parameters:

b2_mat22 – FFI pointer to b2Mat22 C struct

Example

>>> mat_c = ffi.new("b2Mat22*", ((1,2), (3,4)))
>>> Mat22.from_b2Mat22(mat_c)
Mat22(Vec2(1.0, 2.0), Vec2(3.0, 4.0))
classmethod from_columns(col1: VectorLike, col2: VectorLike) Mat22[source]

Create matrix from column vectors.

Parameters:
  • col1 – First column vector

  • col2 – Second column vector

Returns:

Column-based matrix

Return type:

Mat22

Example

>>> Mat22.from_columns((1,2), (3,4))
Mat22(Vec2(1.0, 2.0), Vec2(3.0, 4.0))
classmethod from_rows(row1: VectorLike, row2: VectorLike) Mat22[source]

Create matrix from row vectors (transposed).

Parameters:
  • row1 – First row vector

  • row2 – Second row vector

Returns:

Row-based matrix

Return type:

Mat22

Example

>>> Mat22.from_rows((1,3), (2,4))
Mat22(Vec2(1.0, 2.0), Vec2(3.0, 4.0))
classmethod identity() Mat22[source]

Create identity matrix (no transformation).

Returns:

[[1 0]

[0 1]]

Return type:

Mat22

Example

>>> Mat22.identity()
Mat22(Vec2(1.0, 0.0), Vec2(0.0, 1.0))
property inverse: Mat22

Calculate inverse matrix if possible.

Returns:

Inverse or identity matrix if singular

Return type:

Mat22

Example

>>> Mat22(1,1,0,1).inverse
Mat22(Vec2(1.0, -1.0), Vec2(-0.0, 1.0))

Note

Returns identity matrix for singular matrices (det ≈ 0)

property rows: tuple[Vec2, Vec2]

Matrix row vectors as a tuple.

Returns:

First and second row vectors

Return type:

(Vec2, Vec2)

Example

>>> Mat22(1, 2, 3, 4).rows
(Vec2(1.0, 3.0), Vec2(2.0, 4.0))
solve(b: VectorLike) Vec2[source]

Solve the linear system A * x = b.

Parameters:

b – Right-hand side vector (Vec2 or tuple/list)

Returns:

Solution vector x if matrix is invertible

Return type:

Vec2

Note

Returns zero vector if matrix is singular (det ≈ 0)

Example

>>> m = Mat22(2, 0, 0, 2)  # Scales by 2
>>> m.solve(Vec2(4, 6))     # Should divide by 2
Vec2(2.0, 3.0)
>>> singular = Mat22(1, 1, 1, 1)
>>> singular.solve((1, 1))  # Returns zero for singular matrix
Vec2(0.0, 0.0)
transpose() Mat22[source]

Create transposed matrix (swap rows and columns).

Returns:

Transposed matrix

Return type:

Mat22

Example

>>> Mat22(1,2,3,4).transpose()
Mat22(Vec2(1.0, 3.0), Vec2(2.0, 4.0))
class box2d.math.Rot(angle_radians=0.0)[source]

2D rotation represented by cosine and sine components.

Provides common rotation operations and conversions.

Features: - Angle conversions (radians/degrees) - Rotation composition via multiplication - Vector rotation via multiplication - Axis access (x_axis/y_axis properties) - Normalization and inversion

Example

>>> deg_90 = Rot(math.pi/2)
>>> v = Vec2(1, 0)
>>> deg_90 * v
Vec2(0.0, 1.0)
__init__(angle_radians=0.0)[source]

Initialize from rotation angle in radians.

Parameters:

angle_radians (float) – Initial angle in radians. Defaults to 0.0.

Example

>>> r = Rot(math.pi/2)
>>> r.c, r.s
(6.123233995736766e-17, 1.0)
property angle_degrees: float

Rotation angle in degrees [-180, 180].

Returns:

Angle converted to degrees

Return type:

float

Example

>>> Rot(math.pi/2).angle_degrees
90.0
property angle_radians: float

Rotation angle in radians [-π, π].

Returns:

Angle calculated via atan2(s, c)

Return type:

float

Example

>>> Rot(math.pi).angle_radians
3.141592653589793
property as_tuple: tuple[float, float]

Return the rotation as a tuple of sine and cosine components.

Returns: A tuple containing the sine and cosine components of the rotation.

Example

>>> s, c = Rot(math.pi/2).as_tuple
>>> s == 1.0
True
>>> round(c, 6) == 0.0
True
property b2Rot

Box2D b2Rot equivalent (managed by FFI).

Example

>>> rot = Rot(math.pi/4)
>>> cr = rot.b2Rot
>>> cr.s, cr.c
(0.7071067690849304, 0.7071067690849304)
property c

Cosine component of rotation (read-only).

Example

>>> print(f"{Rot(math.pi/2).c:.1f}")
0.0
classmethod from_b2Rot(b2_rot)[source]

Create a Rot instance from Box2D’s b2Rot structure.

Parameters:

b2_rot – FFI pointer to b2Rot C struct

Example

>>> rot_c = ffi.new("b2Rot*", (Rot(math.pi/2).c, Rot(math.pi/2).s))
>>> Rot.from_b2Rot(rot_c).angle_degrees
90.0
classmethod from_degrees(degrees: float) Rot[source]

Create a Rot instance from an angle in degrees.

Parameters:

degrees (float) – The angle in degrees.

Returns:

New rotation instance

Return type:

Rot

Example

>>> r = Rot.from_degrees(90)
>>> r.angle_degrees
90.0
classmethod from_sincos(s: float, c: float) Rot[source]

Create rotation directly from sine/cosine values.

Parameters:
  • s (float) – Sine component

  • c (float) – Cosine component

Returns:

New unnormalized rotation

Return type:

Rot

Example

>>> r = Rot.from_sincos(0, 1)
>>> r.angle_radians
0.0
classmethod identity()[source]

Create identity rotation (0 angle, no rotation).

Returns:

Identity rotation equivalent to Rot(0.0)

Return type:

Rot

Example

>>> Rot.identity().angle_degrees
0.0
interpolate(other, t, ccw=True)[source]

Linearly interpolate between two rotations with a forced direction.

Parameters:
  • other (Rot) – The target rotation.

  • t (float) – Interpolation factor in the range [0.0, 1.0].

  • ccw (bool, optional) – If True (default), force counterclockwise interpolation. If False, force clockwise interpolation.

Returns:

A new rotation interpolated between self and other.

Return type:

Rot

Examples

>>> Rot(0).interpolate(Rot(math.pi/2), 0.5).angle_degrees
45.0
>>> Rot(0).interpolate(Rot(math.pi/2), 0.5, ccw=False).angle_degrees # Clockwise
-135.0
property inverse

Create inverse/opposite rotation.

Returns:

New rotation with negated angle

Return type:

Rot

Example

>>> Rot(math.pi/4).inverse.angle_degrees
-45.0
normalize()[source]

Create normalized rotation with unit-length components.

Returns:

New rotation with same direction but magnitude 1

Return type:

Rot

Note

Handles floating-point imprecision in rotation components

Example

>>> r = Rot.from_sincos(2.0, 3.0).normalize() # Hypothetical non-normalized input
>>> Vec2(r.c, r.s).length
1.0
rotate_vector(v: VectorLike) Vec2[source]

Apply rotation to a vector.

Parameters:

v – Vector-like to rotate (supports any VectorLike input)

Returns:

Rotated vector

Return type:

Vec2

Example

>>> Rot(math.pi/2).rotate_vector(Vec2(1, 0))
Vec2(0.0, 1.0)
property s

Sine component of rotation (read-only).

Example

>>> Rot(0).s
0.0
property x_axis: Vec2

Get rotated X-axis (first column of rotation matrix).

Returns:

Unit vector (c, s)

Return type:

Vec2

Example

>>> Rot(0).x_axis
Vec2(1.0, 0.0)
>>> Rot(math.pi/2).x_axis
Vec2(0.0, 1.0)
property y_axis: Vec2

Get rotated Y-axis (second column of rotation matrix).

Returns:

Unit vector (-s, c)

Return type:

Vec2

Example

>>> Rot(0).y_axis
Vec2(-0.0, 1.0)
>>> Rot(math.pi/2).y_axis
Vec2(-1.0, 0.0)
classmethod zero()[source]

Create zero rotation (synonym for Identity).

Returns:

Same as Identity rotation

Return type:

Rot

Example

>>> Rot.zero() == Rot.identity()
True
class box2d.math.ScaledTransform(position: VectorLike = Vec2(0.0, 0.0), rotation: float | Rot = Rot(0.000000), scale: float | VectorLike = Vec2(1.0, 1.0))[source]

A 2D transformation supporting scaling, rotation, and translation.

Designed for visualization purposes - does not affect Box2D physics calculations. Applies transformations in the order: Scale → Rotate → Translate.

position

Translation component of the transform

Type:

Vec2

rotation

Rotation component of the transform

Type:

Rot

scale

Scaling factors (x, y). Defaults to (1, 1)

Type:

Vec2

Example

>>> t = ScaledTransform(position=(10, 20), rotation=math.pi/2, scale=2)
>>> t(Vec2(1, 0))  # Scale first, then rotate, then translate
Vec2(10.0, 22.0)
__init__(position: VectorLike = Vec2(0.0, 0.0), rotation: float | Rot = Rot(0.000000), scale: float | VectorLike = Vec2(1.0, 1.0))[source]

Initialize a scaled transform.

Parameters:
  • position – Translation offset as Vec2 or tuple. Defaults to (0, 0)

  • rotation – Rotation angle (radians) or Rot instance. Defaults to 0

  • scale – Scaling factors as Vec2, tuple, or single number for uniform scaling. Defaults to (1, 1)

Note

If a single number is provided for scale, it will create uniform scaling in both x and y axes.

Example

>>> # Various initialization styles:
>>> t1 = ScaledTransform()  # Identity transform
>>> t2 = ScaledTransform(scale=2)  # Uniform scaling
>>> t3 = ScaledTransform(scale=(1.5, 0.5))  # Non-uniform scaling
classmethod from_transform(transform: Transform, scale: float | VectorLike = 1.0) ScaledTransform[source]

Create a ScaledTransform from a base Transform and optional scaling.

Parameters:
  • transform – Base Transform containing position and rotation

  • scale – Scaling factor(s). Defaults to 1.0 (no scaling)

Example

>>> base = Transform(Vec2(2,3), Rot(math.pi/2))
>>> st = ScaledTransform.from_transform(base, scale=2)
>>> st.position == base.position
True
>>> st.rotation == base.rotation
True
property inverse: ScaledTransform

Calculate inverse transformation that reverses this transformation.

Returns:

Inverse that satisfies inverse(t(p)) == p

Return type:

ScaledTransform

Note

Handles non-uniform scaling and rotation correctly Will raise ValueError if any scale component is zero

Example

>>> t = ScaledTransform(Vec2(2,3), Rot(math.pi/2), scale=2)
>>> t_inv = t.inverse
>>> t_inv(t(Vec2(1, 0)))  # Should return original point
Vec2(1.0, 0.0)
property position: Vec2

Get/set the translation component of the transform.

Accepts:

VectorLike: Any tuple/list/Vec2 convertible to Vec2

Example

>>> t = ScaledTransform()
>>> t.position = (5, 2)
>>> t.position
Vec2(5.0, 2.0)
property rotation: Rot

Get/set the rotation component.

Accepts:

float: Angle in radians Rot: Direct rotation instance

Example

>>> t = ScaledTransform()
>>> t.rotation = math.pi/2  # Set from angle
>>> t.rotation = Rot(0)     # Set directly
property scale: Vec2

Get/set the scaling factors.

Accepts:

float: Uniform scaling for both axes VectorLike: Separate x/y scaling factors

Example

>>> t = ScaledTransform()
>>> t.scale = 2.5      # Uniform scaling
>>> t.scale = (1, 0.5) # Non-uniform
class box2d.math.Transform(position: VectorLike = Vec2(0.0, 0.0), rotation: float | Rot = Rot(0.000000))[source]

Represents a 2D transformation combining position and rotation.

Features: - Apply transformations to points (rotate then translate) - Calculate inverse transformations - Compatible with Box2D’s b2Transform structure

Example

>>> t = Transform(Vec2(2, 3), Rot(math.pi/2))
>>> t(Vec2(1, 0))  # Rotate then translate
Vec2(2.0, 4.0)
__init__(position: VectorLike = Vec2(0.0, 0.0), rotation: float | Rot = Rot(0.000000))[source]

Initialize transformation with position and rotation.

Parameters:
  • VectorLike (position) – Translation component

  • rotation (Rot | float) – Rotation component (accepts angle in radians)

Example

>>> Transform((1, 2), math.pi)
Transform(p=Vec2(1.0, 2.0), q=Rot(3.141593))
property b2Transform

Box2D b2Transform equivalent (managed by FFI).

Example

>>> tf = Transform(Vec2(1,2), Rot(math.pi))
>>> ct = tf.b2Transform
>>> ct.p.x, ct.p.y
(1.0, 2.0)
classmethod from_b2Transform(b2_transform)[source]

Create Transform from Box2D’s b2Transform structure.

Parameters:

b2_transform – FFI pointer to b2Transform C struct

Example

>>> tf_c = ffi.new("b2Transform*", ((1,2), Rot(math.pi/2).b2Rot[0]))
>>> Transform.from_b2Transform(tf_c)
Transform(p=Vec2(1.0, 2.0), q=Rot(1.570796))
property inverse: Transform

Calculate inverse transformation.

Returns:

Inverse that reverses this transformation

Return type:

Transform

Note

The inverse transform satisfies: t.inverted()(t(p)) == p

Example

>>> t = Transform(Vec2(2, 3), Rot(math.pi/2))
>>> t_inv = t.inverse
>>> t_inv(t(Vec2(1, 0)))  # Should return original point
Vec2(1.0, 0.0)
property position: Vec2

Get the position component of the transform.

property rotation: Rot

Get the rotation component of the transform.

Returns:

Current rotation component

Return type:

Rot

class box2d.math.Vec2(x, y)[source]

2D vector with Box2D math operations.

vector-like

Any object that is indexable (with [0] and [1]), iterable (yielding floats), and of length 2 (tuples, lists, numpy arrays, other Vec2 instances, etc.)

Features:
  • Component-wise operations

  • Tuple interoperability (+, -, etc.)

  • Rotation and projection operations

  • Factory methods for common vectors (Zero, Right, Left, etc.)

Example

>>> v = Vec2(1, 2)
>>> v.x
1.0
__init__(x, y)[source]

Initialize a 2D vector with the given components.

Parameters:
  • x (float) – The x-component of the vector.

  • y (float) – The y-component of the vector.

Returns:

A new instance of Vec2 with the specified components.

Return type:

Vec2

property angle: float

Return the angle of the vector in radians.

Returns:

The angle in radians, computed using math.atan2(y, x).

Return type:

float

Example

>>> Vec2(1.0, 1.0).angle
0.7853981633974483
property as_tuple: tuple[float, float]

Return the vector as a tuple of floats.

Returns:

A tuple containing the x and y components of the vector.

Return type:

tuple

property b2Vec2

Box2D b2Vec2 equivalent (managed by FFI).

Example

>>> cv = Vec2(1.5, 2.5).b2Vec2
>>> cv.x
1.5
>>> cv.y
2.5
clamp(min_value: VectorLike, max_value: VectorLike) Vec2[source]

Clamp this vector within a specified range.

The inputs min_value and max_value are each converted to a Vec2.

cross(other: VectorLike) float[source]

Compute the 2D cross product (a scalar) with another vector-like object.

cross_scalar(s: float, direction: str = 'right') Vec2[source]

Compute the cross product with a scalar following Box2D conventions.

Parameters:
  • s (float) – The scalar multiplier.

  • direction (str, optional) – ‘right’ (default) or ‘left’.

distance_to(other: VectorLike) float[source]

Calculate the Euclidean distance between this vector and another vector-like object.

dot(other: VectorLike) float[source]

Compute the dot product with another vector-like object.

classmethod down()[source]

Create a down-pointing vector (0,-1).

Returns:

A down vector instance.

Return type:

Vec2

Example

>>> Vec2.down()
Vec2(0.0, -1.0)
classmethod from_angle(angle)[source]

Create a unit vector from the given angle in radians.

Parameters:

angle (float) – The angle in radians.

Returns:

A unit vector instance.

Return type:

Vec2

Example

>>> Vec2.from_angle(math.pi/2)
Vec2(0.0, 1.0)
classmethod from_b2Vec2(b2_vec)[source]

Create from Box2D b2Vec2 structure.

Example

>>> vec_c = ffi.new("b2Vec2*", (1.5, 2.5))
>>> Vec2.from_b2Vec2(vec_c)
Vec2(1.5, 2.5)
property heading: Vec2

Return the normalized (heading) vector.

property inverse: Vec2

Return a new vector which is the component-wise inverse (negation).

is_close(other: VectorLike, tolerance: float = 1e-06) bool[source]

Check if this vector is approximately equal to another vector-like object.

The parameter is first converted via to_vec2.

property is_finite

Check if both components are finite numbers.

Returns:

True if both components are finite, False otherwise.

Return type:

bool

classmethod left()[source]

Create a left-pointing vector (-1,0).

Returns:

A left vector instance.

Return type:

Vec2

Example

>>> Vec2.left()
Vec2(-1.0, 0.0)
property length: float

The Euclidean length (magnitude) of the vector.

Returns:

The length of the vector.

Return type:

float

Example

>>> Vec2(3.0, 4.0).length
5.0
property length_squared: float

The square of the Euclidean length of the vector.

Returns:

The squared length of the vector.

Return type:

float

Example

>>> Vec2(3.0, 4.0).length_squared
25.0
lerp(other: VectorLike, t: float) Vec2[source]

Linearly interpolate between this vector and another vector-like object.

Parameters:

t (float) – Interpolation factor (typically between 0 and 1).

max(other: VectorLike) Vec2[source]

Return the component-wise maximum comparing this vector and another.

min(other: VectorLike) Vec2[source]

Return the component-wise minimum comparing this vector and another.

multiply_componentwise(other: VectorLike) Vec2[source]

Multiply this vector with another vector-like object component-wise.

normalize() Vec2[source]

Return a unit vector in the direction of this vector.

Returns:

A unit vector if the length is non-zero; otherwise, a zero vector.

Return type:

Vec2

Example

>>> Vec2(3.0, 4.0).normalize()
Vec2(0.6, 0.8)
perpendicular(direction='right') Vec2[source]

Return a perpendicular vector.

Parameters:

direction (str, optional) – ‘right’ returns (y, -x), ‘left’ returns (-y, x).

Raises:

ValueError – If the direction is not ‘left’ or ‘right’.

project(other: VectorLike) Vec2[source]

Project this vector onto another vector-like object.

Raises:

ValueError – If the other vector is the zero vector.

reject(other: VectorLike) Vec2[source]

Return the component of this vector perpendicular to another vector-like object.

classmethod right()[source]

Create a right-pointing vector (1,0).

Returns:

A right vector instance.

Return type:

Vec2

Example

>>> Vec2.right()
Vec2(1.0, 0.0)
rotate(angle: float) Vec2[source]

Rotate the vector by the given angle in radians.

classmethod up()[source]

Create an up-pointing vector (0,1).

Returns:

An up vector instance.

Return type:

Vec2

Example

>>> Vec2.up()
Vec2(0.0, 1.0)
property x

The x-component of the vector as a float.

property y

The y-component of the vector as a float.

classmethod zero()[source]

Create a zero vector (0,0).

Returns:

A zero vector instance.

Return type:

Vec2

Example

>>> Vec2.zero()
Vec2(0.0, 0.0)