# 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.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. Creates a vector from token direction Creates a vector from token position `rotate`(theta) rotates Vector by theta degrees Returns miniworldmaker direction from vector. sets length of vector to 1 returns length of vector returns -v for Vector v `multiply`(other) product self * other: * returns product, if `other` is scalar (return-type: Vector) * returns dot-product, if `other` is vector (return-type: float) :param other: a scalar or 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

Parameters

other (Vector) – other Vector

Returns

self + other

Examples

```v = Vector(3, 1)
u = Vector(2, 5)
```

Alternative:

```print(u + v)
```
Return type

`Vector`

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

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

`Vector`

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

Length of vector

```w = Vector(4, 3)
print(w.length())     # 5
```
Return type

`float`

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) * returns dot-product, if `other` is vector (return-type: float) :param 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
```
Return type
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

`Vector`

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

`Vector`

rotate(theta)[source]#

rotates Vector by theta degrees

Return type

`Vector`

sub(other)[source]#

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)
```
Return type

`Vector`

to_direction()[source]#

Returns miniworldmaker direction from vector.

Return type

`float`

property x: float#

the x compoonent of the vector

Return type

`float`

property y: float#

the y component of the vector

Return type

`float`