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:

angle

describes the angle as miniworldmaker direction

x

the x compoonent of the vector

y

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

to_direction()

Returns miniworldmaker direction from vector.

get_normal()

normalize()

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)

rtype:

Vector

add_to_position(position)

rtype:

Position

__str__()

Return str(self).

__neg__()

__mul__(other)

rtype:

Vector

__add__(other)

rtype:

Vector

__sub__(other)

rtype:

Vector

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.

Return type:

Vector

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)
add_to_position(position)[source]#
Return type:

Position

property angle#

describes the angle as miniworldmaker direction

dot(other)[source]#
Return type:

Vector

classmethod from_direction(direction)[source]#

Creates a vector from miniworldmaker direction.

Return type:

Vector

classmethod from_positions(p1, p2)[source]#

Create a vector from token and position

The vector desribes is generated from: token2.center - position

Return type:

Vector

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:

Vector

classmethod from_token_direction(token)[source]#

Creates a vector from token direction

Examples: :rtype: Vector

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()
classmethod from_token_position(token)[source]#

Creates a vector from token position

Return type:

Vector

classmethod from_tokens(t1, t2)[source]#

Create a vector from two tokens.

The vector desribes is generated from: token2.center - token1.center

Return type:

Vector

get_normal()[source]#
length()[source]#

returns length of vector

Examples: :rtype: float

Length of vector

w = Vector(4, 3)
print(w.length())     # 5
limit(value)[source]#

limits length of vector to value

Return type:

Vector

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)
rotate(theta)[source]#

rotates Vector by theta degrees

Return type:

Vector

sub(other)[source]#

adds vector other from self.

Return type:

Vector

Parameters:

other (Vector) – other Vector

Returns:

self + other

Examples

Subtracts two vectors:

v = Vector(3, 1)
u = Vector(2, 5)
print(u.sub(v)) # (1, -4)

Alternative:

print(u - v)
to_direction()[source]#

Returns miniworldmaker direction from vector.

Return type:

float

property x: float#

the x compoonent of the vector

property y: float#

the y component of the vector