Vec2
pykraken.Vec2 A 2D vector representing Cartesian coordinates.
More about Vec2
Attributes
- x (
float) — Horizontal component. - y (
float) — Vertical component.
Methods
- copy — Return a duplicated Vec2.
- is_zero — Test whether components are near zero.
- rotate — Rotate the vector in place.
- to_polar — Convert the vector to a PolarCoordinate.
- scale_to_length — Scale the vector to a specific length.
- project — Project onto another Vec2.
- reject — Remove the projection onto another Vec2.
- reflect — Reflect across another Vec2.
- normalize — Normalize the vector in place.
- distance_to — Measure distance to another Vec2.
- distance_squared_to — Measure squared distance to another Vec2.
At a glance
35 membersConstructormethodanglepropertylengthpropertylength_squaredpropertyxpropertyxxpropertyxypropertyypropertyyxpropertyyypropertyas_intsmethodceilmethodceiledmethodcopymethoddistance_squared_tomethoddistance_tomethodfloormethodflooredmethodis_zeromethodmove_towardmethodmoved_towardmethodnormalizemethodnormalizedmethodprojectmethodreflectmethodrejectmethodrotatemethodrotatedmethodroundmethodroundedmethodscale_to_lengthmethodscaled_to_lengthmethodslidmethodslidemethodto_polarmethodReference
PYTHON APIOverloaded function.
Vec2()
Initialize a Vec2 with zeroed components.
Vec2(value: float)
Initialize a Vec2 with identical x and y values.
Args
- value (
float) — Value assigned to both components.
Vec2(x: float, y: float)
Initialize a Vec2 with explicit component values.
Args
- x (
float) — Horizontal component. - y (
float) — Vertical component.
angle
propertyangle: floatReturn the vector angle in radians.
length
propertylength: floatReturn the magnitude of this Vec2.
length_squared
propertylength_squared: floatReturn the squared magnitude of this Vec2.
x
propertyx: floatThe x component of the vector.
xx
propertyxx: Vec2Return a Vec2 with both components set to x.
xy
propertyxy: Vec2Access or assign the (x, y) components as a Vec2.
y
propertyy: floatThe y component of the vector.
yx
propertyyx: Vec2Access or assign the (y, x) components as a Vec2.
yy
propertyyy: Vec2Return a Vec2 with both components set to y.
as_ints
methodas_ints(self) -> tuple[int, int]Return the vector components truncated to integers as a tuple.
Returns
tuple[int, int]: The (x, y) components as integers.
ceil
methodceil(self) -> NoneCeil both components of this Vec2 in-place.
ceiled
methodceiled(self) -> Vec2Return a new Vec2 with both components ceiled to the nearest integer.
Returns
- Vec2 — A new vector with ceiled components.
copy
methodcopy(self) -> Vec2Return a copy of this Vec2.
Returns
- Vec2 — A duplicated vector with the same components.
distance_squared_to
methoddistance_squared_to(self, other: Vec2) -> floatCompute the squared distance to another Vec2.
Args
- other (
Vec2) — Comparison vector.
Returns
- float — Squared distance between the vectors.
distance_to
methoddistance_to(self, other: Vec2) -> floatCompute the Euclidean distance to another Vec2.
Args
- other (
Vec2) — Comparison vector.
Returns
- float — Distance between the vectors.
floor
methodfloor(self) -> NoneFloor both components of this Vec2 in-place.
floored
methodfloored(self) -> Vec2Return a new Vec2 with both components floored to the nearest integer.
Returns
- Vec2 — A new vector with floored components.
is_zero
methodis_zero(self, tolerance: float = 1e-08) -> boolDetermine whether this Vec2 is effectively zero.
Args
- tolerance (
float) — Largest allowed absolute component magnitude.
Returns
- bool — True if both components are within the tolerance.
move_toward
methodmove_toward(self, target: Vec2, delta: float) -> NoneMove this Vec2 toward a target Vec2 by a specified delta.
Args
- target (
Vec2) — The target vector to move towards. - delta (
float) — The maximum distance to move.
moved_toward
methodReturn a new Vec2 moved toward a target Vec2 by a specified delta.
Args
- target (
Vec2) — The target vector to move towards. - delta (
float) — The maximum distance to move.
Returns
- Vec2 — A new vector moved toward the target.
normalize
methodnormalize(self) -> NoneNormalize this Vec2 in place.
normalized
methodnormalized(self) -> Vec2Return a new normalized Vec2.
Returns
- Vec2 — A new vector with unit length.
project
methodProject this Vec2 onto another Vec2.
Args
- other (
Vec2) — The vector to project onto.
Returns
- Vec2 — Projection of this vector onto the other vector.
reflect
methodReflect this Vec2 across another Vec2.
Args
- other (
Vec2) — The vector used as the reflection normal.
Returns
- Vec2 — Reflected vector.
reject
methodCompute the rejection of this Vec2 from another Vec2.
Args
- other (
Vec2) — The vector defining the projection axis.
Returns
- Vec2 — Component of this vector orthogonal to the other vector.
rotate
methodrotate(self, radians: float) -> NoneRotate this Vec2 in place.
Args
- radians (
float) — Rotation angle in radians.
rotated
methodrotated(self, radians: float) -> Vec2Return a new Vec2 rotated by a specified angle.
Args
- radians (
float) — Rotation angle in radians.
Returns
- Vec2 — A new vector rotated by the given angle.
round
methodround(self) -> NoneRound both components of this Vec2 in-place.
rounded
methodrounded(self) -> Vec2Return a new Vec2 with both components rounded to the nearest integer.
Returns
- Vec2 — A new vector with rounded components.
scale_to_length
methodscale_to_length(self, length: float) -> NoneScale this Vec2 to a specific magnitude.
Args
- length (
float) — Target vector length.
scaled_to_length
methodscaled_to_length(self, length: float) -> Vec2Return a new Vec2 scaled to a specific magnitude.
Args
- length (
float) — Target vector length.
Returns
- Vec2 — A new vector scaled to the specified length.
slid
methodReturn a new Vec2 slid along a surface defined by a normal vector.
Args
- normal (
Vec2) — The normal vector defining the surface.
Returns
- Vec2 — A new vector slid along the surface.
slide
methodslide(self, normal: Vec2) -> NoneSlide this Vec2 along a surface defined by a normal vector.
Args
- normal (
Vec2) — The normal vector defining the surface.
Returns
- Vec2 — A new vector slid along the surface.
to_polar
methodto_polar(self) -> PolarCoordinateConvert this Vec2 to polar coordinates.
Returns
- PolarCoordinate — Polar representation with angle and length.