Positions > Vektor#

API-Referenz#

class miniworlds.positions.vector.Vector(x, y)[Quellcode]#

Represents a 2D vector for use in movement, geometry, and physics.

Supports arithmetic with both other Vectors and 2D tuples.

Beispiele

>>> v = Vector(1, 2)
>>> v2 = Vector(3, 4)
>>> v + v2
Vector(4.0, 6.0)
>>> v + (1, 1)
Vector(2.0, 3.0)
>>> (1, 1) + v
Vector(2.0, 3.0)

Öffentliche Datenattribute:

x

The x-component of the vector.

y

The y-component of the vector.

angle

Direction angle of the vector in miniworlds convention (0° = up, 90° = right).

Öffentliche Methoden:

__init__(x, y)

Creates a 2D vector.

__getitem__(index)

to_position()

Converts the vector to a plain (x, y) tuple.

from_position(position)

Creates a Vector from a 2-tuple position.

from_positions(p1, p2)

Creates a Vector pointing from p1 to p2.

from_direction(direction)

Creates a unit vector from a miniworlds direction value.

from_actors(t1, t2)

Creates a vector from one actor to another actor.

from_actor_and_position(t1, pos)

Creates a vector from an actor center to a target position.

from_actor_direction(actor)

Creates a vector from actor direction

rotate(theta)

Rotates the vector in-place by theta degrees counter-clockwise.

to_direction()

Converts the vector to a miniworlds direction value.

normalize()

Normalizes the vector to length 1 in-place.

length()

Returns the Euclidean length (magnitude) of the vector.

limit(max_length)

Caps the vector's length to max_length without changing its direction.

multiply(other)

Multiplies the vector by a scalar or computes the dot product with another vector.

add_to_position(position)

Adds the vector to a position tuple and returns the result.

get_normal()

Returns a vector perpendicular (normal) to this one.

dot(other)

Computes the dot product of this vector and other.

distance_to(other)

Calculates Euclidean distance to another vector or position.

angle_to(other)

Computes the angle between this vector and another in degrees.

__add__(other)

__radd__(other)

__sub__(other)

__rsub__(other)

__mul__(other)

__rmul__(other)

__neg__()

__eq__(other)

Return self==value.

__str__()

Gib str(self) zurück.

__repr__()

Return repr(self).

Private Methods:

_to_vector(value)


__init__(x, y)[Quellcode]#

Creates a 2D vector.

Parameter:
  • x – Horizontal component.

  • y – Vertical component.

add_to_position(position)[Quellcode]#

Adds the vector to a position tuple and returns the result.

Rückgabetyp:

Tuple[float, float]

Parameter:

position – A (x, y) position tuple.

Rückgabe:

New position as (x + self.x, y + self.y).

property angle: float#

Direction angle of the vector in miniworlds convention (0° = up, 90° = right).

Equivalent to calling to_direction().

angle_to(other)[Quellcode]#

Computes the angle between this vector and another in degrees.

Rückgabetyp:

float

Parameter:

other – A Vector or tuple.

Rückgabe:

Angle in degrees between 0 and 180.

Beispiele

>>> Vector(1, 0).angle_to((0, 1))
90.0
distance_to(other)[Quellcode]#

Calculates Euclidean distance to another vector or position.

Rückgabetyp:

float

Parameter:

other – A Vector or tuple.

Rückgabe:

The distance as float.

Beispiele

>>> Vector(0, 0).distance_to((3, 4))
5.0
dot(other)[Quellcode]#

Computes the dot product of this vector and other.

Rückgabetyp:

float

Parameter:

other – The other Vector.

Rückgabe:

Dot product as a float.

classmethod from_actor_and_position(t1, pos)[Quellcode]#

Creates a vector from an actor center to a target position.

Rückgabetyp:

Vector

Parameter:
  • t1 – The start actor.

  • pos – Target position as (x, y).

Rückgabe:

A vector equal to pos - t1.center.

classmethod from_actor_direction(actor)[Quellcode]#

Creates a vector from actor direction

Examples: :rtype: Vector

Erstellt ein rotierendes Rechteck

from miniworlds import *

world = World()

player = Rectangle((200,200),40, 40)
player.speed = 1
player.direction = 80

@player.register
def act(self):
    v1 = Vector.from_actor_direction(self)
    v1.rotate(-1)
    self.direction = v1

world.run()
classmethod from_actors(t1, t2)[Quellcode]#

Creates a vector from one actor to another actor.

Rückgabetyp:

Vector

Parameter:
  • t1 – Start actor.

  • t2 – Target actor.

Rückgabe:

A vector equal to t2.center - t1.center.

Beispiele

enemy_vector = Vector.from_actors(player, enemy)
print(enemy_vector.length())
classmethod from_direction(direction)[Quellcode]#

Creates a unit vector from a miniworlds direction value.

Rückgabetyp:

Vector

Parameter:

direction – Direction in miniworlds convention (0 = up, 90 = right, -90 = left, 180 = down).

Rückgabe:

A new unit Vector pointing in the given direction.

classmethod from_position(position)[Quellcode]#

Creates a Vector from a 2-tuple position.

Rückgabetyp:

Vector

Parameter:

position – A (x, y) tuple.

Rückgabe:

A new Vector with the same x and y components.

classmethod from_positions(p1, p2)[Quellcode]#

Creates a Vector pointing from p1 to p2.

Rückgabetyp:

Vector

Parameter:
  • p1 – Start position as (x, y).

  • p2 – End position as (x, y).

Rückgabe:

Vector equal to p2 - p1.

get_normal()[Quellcode]#

Returns a vector perpendicular (normal) to this one.

Rückgabetyp:

Vector

Rückgabe:

A new Vector rotated 90° counter-clockwise.

length()[Quellcode]#

Returns the Euclidean length (magnitude) of the vector.

Rückgabetyp:

float

Rückgabe:

Length as a float.

limit(max_length)[Quellcode]#

Caps the vector’s length to max_length without changing its direction.

Rückgabetyp:

Vector

Parameter:

max_length – Maximum allowed length.

Rückgabe:

The vector itself.

multiply(other)[Quellcode]#

Multiplies the vector by a scalar or computes the dot product with another vector.

Rückgabetyp:

Union[Vector, float]

Parameter:

other – A scalar int/float or another Vector.

Rückgabe:

A new scaled Vector when other is a scalar, or a float dot product.

normalize()[Quellcode]#

Normalizes the vector to length 1 in-place.

Rückgabetyp:

Vector

Rückgabe:

The vector itself. A zero-length vector is returned unchanged.

rotate(theta)[Quellcode]#

Rotates the vector in-place by theta degrees counter-clockwise.

Rückgabetyp:

Vector

Parameter:

theta – Rotation angle in degrees.

Rückgabe:

The vector itself (mutated in-place).

to_direction()[Quellcode]#

Converts the vector to a miniworlds direction value.

Rückgabetyp:

float

Rückgabe:

Direction as a float in miniworlds convention (0° = up, 90° = right, 180° = down, -90° = left). Returns 0 for a zero-length vector.

to_position()[Quellcode]#

Converts the vector to a plain (x, y) tuple.

Rückgabetyp:

Tuple[float, float]

property x: float#

The x-component of the vector.

property y: float#

The y-component of the vector.