CLASS v1.8.0

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.

Reference

PYTHON API

Constructor

method3 overloads
Vec2()
Vec2(value: float)
Vec2(x: float, y: float)

Overloaded function.

  1. Vec2()

Initialize a Vec2 with zeroed components.

  1. Vec2(value: float)

Initialize a Vec2 with identical x and y values.

Args

  • value (float) — Value assigned to both components.
  1. Vec2(x: float, y: float)

Initialize a Vec2 with explicit component values.

Args

  • x (float) — Horizontal component.
  • y (float) — Vertical component.

angle

property
angle: float

Return the vector angle in radians.

length

property
length: float

Return the magnitude of this Vec2.

length_squared: float

Return the squared magnitude of this Vec2.

x

property
x: float

The x component of the vector.

xx

property
xx: Vec2

Return a Vec2 with both components set to x.

xy

property
xy: Vec2

Access or assign the (x, y) components as a Vec2.

y

property
y: float

The y component of the vector.

yx

property
yx: Vec2

Access or assign the (y, x) components as a Vec2.

yy

property
yy: Vec2

Return a Vec2 with both components set to y.

as_ints

method
as_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

method
ceil(self) -> None

Ceil both components of this Vec2 in-place.

ceiled

method
ceiled(self) -> Vec2

Return a new Vec2 with both components ceiled to the nearest integer.

Returns

  • Vec2 — A new vector with ceiled components.

copy

method
copy(self) -> Vec2

Return a copy of this Vec2.

Returns

  • Vec2 — A duplicated vector with the same components.
distance_squared_to(self, other: Vec2) -> float

Compute the squared distance to another Vec2.

Args

  • other (Vec2) — Comparison vector.

Returns

  • float — Squared distance between the vectors.
distance_to(self, other: Vec2) -> float

Compute the Euclidean distance to another Vec2.

Args

  • other (Vec2) — Comparison vector.

Returns

  • float — Distance between the vectors.

floor

method
floor(self) -> None

Floor both components of this Vec2 in-place.

floored

method
floored(self) -> Vec2

Return a new Vec2 with both components floored to the nearest integer.

Returns

  • Vec2 — A new vector with floored components.

is_zero

method
is_zero(self, tolerance: float = 1e-08) -> bool

Determine 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(self, target: Vec2, delta: float) -> None

Move 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(self, target: Vec2, delta: float) -> Vec2

Return 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

method
normalize(self) -> None

Normalize this Vec2 in place.

normalized(self) -> Vec2

Return a new normalized Vec2.

Returns

  • Vec2 — A new vector with unit length.

project

method
project(self, other: Vec2) -> Vec2

Project this Vec2 onto another Vec2.

Args

  • other (Vec2) — The vector to project onto.

Returns

  • Vec2 — Projection of this vector onto the other vector.

reflect

method
reflect(self, other: Vec2) -> Vec2

Reflect this Vec2 across another Vec2.

Args

  • other (Vec2) — The vector used as the reflection normal.

Returns

  • Vec2 — Reflected vector.

reject

method
reject(self, other: Vec2) -> Vec2

Compute 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

method
rotate(self, radians: float) -> None

Rotate this Vec2 in place.

Args

  • radians (float) — Rotation angle in radians.

rotated

method
rotated(self, radians: float) -> Vec2

Return 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

method
round(self) -> None

Round both components of this Vec2 in-place.

rounded

method
rounded(self) -> Vec2

Return a new Vec2 with both components rounded to the nearest integer.

Returns

  • Vec2 — A new vector with rounded components.
scale_to_length(self, length: float) -> None

Scale this Vec2 to a specific magnitude.

Args

  • length (float) — Target vector length.
scaled_to_length(self, length: float) -> Vec2

Return 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

method
slid(self, normal: Vec2) -> Vec2

Return 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

method
slide(self, normal: Vec2) -> None

Slide 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

method
to_polar(self) -> PolarCoordinate

Convert this Vec2 to polar coordinates.

Returns

  • PolarCoordinate — Polar representation with angle and length.

Find your next step

Start typing to search the manual.