Positions -> Vector
Contents
Positions -> Vector#
- class miniworldmaker.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 miniworldmaker import * board = Board(800, 800) mover = Circle() mover.velocity = Vector(0, 0) mover.topspeed = 10 @board.register def act(self): mouse_vec = Vector(board.get_mouse_x(), board.get_mouse_y()) location = Vector.from_token_position(mover) acceleration = mouse_vec - location acceleration.normalize() * 2 mover.velocity.add(acceleration) mover.velocity.limit(mover.topspeed) mover.move_vector(mover.velocity) board.run()
Public Data Attributes:
describes the angle as miniworldmaker direction
the x compoonent of the vector
the y component of the vector
Public Methods:
__init__
(x, y)__getitem__
(item)from_positions
(p1, p2)Create a vector from token and position
from_token_and_position
(t1, pos)Create a vector from token and position
from_tokens
(t1, t2)Create a vector from two tokens.
from_direction
(direction)Creates a vector from miniworldmaker direction.
from_token_direction
(token)Creates a vector from token direction
from_token_position
(token)Creates a vector from token position
rotate
(theta)rotates Vector by theta degrees
Returns miniworldmaker 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) * returns dot-product, ifother
is vector (return-type: float) :param other: a scalar or vectordot
(other)- rtype
add_to_position
(position)- rtype
__str__
()Return str(self).
__neg__
()__mul__
(other)- rtype
__add__
(other)- rtype
__sub__
(other)- rtype
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.
- Parameters
other (Vector) – other Vector
- Returns
self + other
Examples
Add two vectors:
v = Vector(3, 1) u = Vector(2, 5) print(u.add(v)) # (5, 6)
Alternative:
print(u + v)
- Return type
- property angle#
describes the angle as miniworldmaker direction
- classmethod from_direction(direction)[source]#
Creates a vector from miniworldmaker direction.
- Return type
- classmethod from_positions(p1, p2)[source]#
Create a vector from token and position
The vector desribes is generated from: token2.center - position
- Return type
- classmethod from_token_and_position(t1, pos)[source]#
Create a vector from token and position
The vector desribes is generated from: token2.center - position
- Return type
- classmethod from_token_direction(token)[source]#
Creates a vector from token direction
Examples
Creates rotating rectangle
from miniworldmaker import * board = Board() player = Rectangle((200,200),40, 40) player.speed = 1 player.direction = 80 @player.register def act(self): v1 = Vector.from_token_direction(self) v1.rotate(-1) self.direction = v1 board.run()
- Return type
- classmethod from_tokens(t1, t2)[source]#
Create a vector from two tokens.
The vector desribes is generated from: token2.center - token1.center
- Return type
- length()[source]#
returns length of vector
Examples
Length of vector
w = Vector(4, 3) print(w.length()) # 5
- Return type
- multiply(other)[source]#
product self * other: * returns product, if
other
is scalar (return-type: Vector) * returns dot-product, ifother
is vector (return-type: float) :param other: a scalar or vectorExamples
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
Inverse of vector:
u = Vector(2, 4) print(u.neg()) # (-2, 5)
Alternative:
print(- u) # (-2, 5)
- Return type
- normalize()[source]#
sets length of vector to 1
Examples
Normalized vector with length 1:
w = Vector(4, 3) print(w.length()) # 5 print(w.normalize()) # (0.8, 0.6)
- Return type