Vec2

A 2D mathematical vector representation.

Constructor

  • Vec2()
  • Vec2(value: float)
  • Vec2(x: float, y: float)

Parameters

  • value : Value assigned to both x and y components.
  • x : Horizontal component.
  • y : Vertical component.

Properties


NameDescriptionType
xThe x-component of the vectorfloat
yThe y-component of the vectorfloat
xyA tuple representing the (x, y) components of the vectortuple[float, float]
yxA tuple representing the (y, x) components of the vectortuple[float, float]
xxA tuple representing the (x, x) components of the vectortuple[float, float]
yyA tuple representing the (y, y) components of the vectortuple[float, float]
lengthThe length (magnitude) of the vectorfloat
length_squaredThe squared length of the vectorfloat
angleThe angle of the vector in radiansfloat

Methods


Rotate

rotate(radians: float) None

Rotate the vector by a given angle in radians.

Parameters

  • radians : The angle to rotate the vector by, in radians.

Normalize

normalize() None

Normalize the vector, making its length 1.

Note:

If the vector has zero length, it will remain unchanged.

Scale To Length

scale_to_length(length: float) None

Scale the vector to a specific length.

Parameters

  • length : The length to scale the vector to.

Distance To

distance_to(other: Vec2) float

Calculate the distance to another vector.

Parameters

  • other : The other vector to calculate the distance to.

Returns

float : The distance to the other vector.

Distance Squared To

distance_squared_to(other: Vec2) float

Calculate the squared distance to another vector.

Parameters

  • other : The other vector to calculate the squared distance to.

Returns

float : The squared distance to the other vector.

To Polar

to_polar() PolarCoordinate

Convert the vector to polar coordinates.

Returns

PolarCoordinate : The polar coordinate object representation of the vector.

Copy

copy() Vec2

Create a copy of the vector.

Returns

Vec2 : A new vector object that is a copy of the original.

Is Zero

is_zero(tolerance: float = 1e-08) bool

Determine whether this Vec2 is effectively zero.

Parameters

  • tolerance : Largest allowed absolute component magnitude.

Returns

bool : True if both components are within the tolerance.

Project

project(other: Vec2) Vec2

Project this Vec2 onto another Vec2.

Parameters

  • other : The vector to project onto.

Returns

Vec2 : Projection of this vector onto the other vector.

Reflect

reflect(other: Vec2) Vec2

Reflect this Vec2 across another Vec2.

Parameters

  • other : The vector used as the reflection normal.

Returns

Vec2 : Reflected vector.

Reject

reject(other: Vec2) Vec2

Compute the rejection of this Vec2 from another Vec2.

Parameters

  • other : The vector defining the projection axis.

Returns

Vec2 : Component of this vector orthogonal to the other vector.