Positions -> Vector#
- class miniworlds.positions.vector.Vector(x, y)[source]#
Describes a two-dimensional vector.
It is used to describe a position, acceleration or velocity.
Examples
Create a circle which follows the mouse.
from miniworlds import * world = World(800, 800) mover = Circle() mover.velocity = Vector(0, 0) mover.topspeed = 10 @world.register def act(self): mouse_vec = Vector(world.mouse.x(), world.mouse.y()) location = Vector.from_actor_position(mover) acceleration = mouse_vec - location acceleration.normalize() * 2 mover.velocity.add(acceleration) mover.velocity.limit(mover.topspeed) mover.move_vector(mover.velocity) world.run()
Public Data Attributes:
Public Methods:
__init__
(x, y)__getitem__
(item)from_positions
(p1, p2)Create a vector from actor and position
from_position
(position)Create a vector from actor and position
from_actor_and_position
(t1, pos)Create a vector from actor and position
from_actors
(t1, t2)Create a vector from two actors.
from_direction
(direction)Creates a vector from miniworlds direction.
from_actor_direction
(actor)Creates a vector from actor direction
from_actor_position
(actor)Creates a vector from actor position
rotate
(theta)rotates Vector by theta degrees
Returns miniworlds direction from vector.
sets length of vector to 1
length
()returns length of vector
neg
()returns -v for Vector v
multiply
(other)product self * other: * returns product, if
other
is scalar (return-type: Vector) :rtype:Union
[float
,Vector
]dot
(other)add_to_position
(position)__str__
()Return str(self).
__neg__
()__mul__
(other)__add__
(other)__sub__
(other)sub
(other)adds vector other from self.
add
(other)adds vector other to self.
limit
(value)limits length of vector to value
- add(other)[source]#
adds vector other to self.
Examples
Add two vectors:
v = Vector(3, 1) u = Vector(2, 5) print(u.add(v)) # (5, 6)
Alternative:
print(u + v)
- property angle#
describes the angle as miniworlds direction
- 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:
- 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:
- classmethod from_direction(direction)[source]#
Creates a vector from miniworlds direction.
- Return type:
- classmethod from_position(position)[source]#
Create a vector from actor and position
The vector describes is generated from: actor2.center - position
- Return type:
- classmethod from_positions(p1, p2)[source]#
Create a vector from actor and position
The vector describes is generated from: actor2.center - position
- Return type:
- length()[source]#
returns length of vector
Examples: :rtype:
float
Length of vector
w = Vector(4, 3) print(w.length()) # 5
- multiply(other)[source]#
product self * other: * returns product, if
other
is scalar (return-type: Vector) :rtype:Union
[float
,Vector
]returns dot-product, if
other
is vector (return-type: float)
- Parameters:
other – a scalar or vector
Examples
Product and dot-product:
a = 5 u1 = Vector(2, 4) u2 = Vector(2, 4) v = Vector(3, 1) print(u1.multiply(a)) # (10, 25) print(u2.multiply(v)) # 11
Alternative:
print(u1 * a) # 25 print(u1 * v) # 25
- neg()[source]#
returns -v for Vector v
Examples: :rtype:
Vector
Inverse of vector:
u = Vector(2, 4) print(u.neg()) # (-2, 5)
Alternative:
print(- u) # (-2, 5)
- normalize()[source]#
sets length of vector to 1
Examples: :rtype:
Vector
Normalized vector with length 1:
w = Vector(4, 3) print(w.length()) # 5 print(w.normalize()) # (0.8, 0.6)