Positions -> Vector#
- class miniworlds.positions.vector.Vector(x, y)[source]#
Represents a 2D vector for use in movement, geometry, and physics.
Supports arithmetic with both other Vectors and 2D tuples.
Examples
>>> 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)
Public Methods:
__init__
(x, y)__getitem__
(index)from_position
(position)from_positions
(p1, p2)from_direction
(direction)Creates a vector from miniworlds direction.
from_actors
(t1, t2)Create a vector from two actors.
from_actor_and_position
(t1, pos)Create a vector from actor and position
from_actor_direction
(actor)Creates a vector from actor direction
rotate
(theta)length
()limit
(max_length)multiply
(other)add_to_position
(position)dot
(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__
()Return str(self).
__repr__
()Return repr(self).
Private Methods:
_to_vector
(value)
- angle_to(other)[source]#
Computes the angle between this vector and another in degrees.
- Return type:
- Parameters:
other – A Vector or tuple.
- Returns:
Angle in degrees between 0 and 180.
Examples
>>> Vector(1, 0).angle_to((0, 1)) 90.0
- distance_to(other)[source]#
Calculates Euclidean distance to another vector or position.
- Return type:
- Parameters:
other – A Vector or tuple.
- Returns:
The distance as float.
Examples
>>> Vector(0, 0).distance_to((3, 4)) 5.0
- classmethod from_actor_and_position(t1, pos)[source]#
Create a vector from actor and position
The vector describes is generated from: actor2.center - position
- Return type:
Vector
- classmethod from_actor_direction(actor)[source]#
Creates a vector from actor direction
Examples: :rtype: Vector
Creates rotating rectangle
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)[source]#
Create a vector from two actors.
The vector describes is generated from: actor2.center - actor1.center
- Return type:
Vector