# Vector2D¶

class `pygamelib.base.``Vector2D`(row=0.0, column=0.0)

Bases: `object`

A 2D vector class.

New in version 1.2.0.

Contrary to the rest of the library Vector2D uses floating point numbers for its coordinates/direction/orientation. However since the rest of the library uses integers, the numbers are rounded to 2 decimals. You can alter that behavior by increasing or decreasing the rounding_precision parameter (if you want integer for example).

Vector2D use the row/column internal naming convention as it is easier to visualize for developers that are still learning python or the pygamelib. If it is a concept that you already understand and are more familiar with the x/y coordinate system you can also use x and y.

• x is equivalent to column
• y is equivalent to row

Everything else is the same.

Vectors can be printed and supports basic operations:

• substraction
• multiplication

Let’s elaborate a bit more on the multiplication. The product behaves in 2 different ways:

If you multiply a vector with a scalar (int or float), the return value is a Vector2D with each vector component multiplied by said scalar.

If you multiply a Vector2D with another Vector2D you ask for the the cross product of vectors. This is an undefined mathematical operation in 2D as the cross product is supposed to be perpendicular to the 2 other vectors (along the z axis in our case). Since we don’t have depth (z) in 2D, this will return the magnitude of the signed cross product of the 2 vectors.

Example of products:

```v1 = base.Vector2D(1,2)
v2 = base.Vector2D(3,4)
# This returns -2
mag = v1 * v2
# This returns a Vector2D with values (-1, -2)
inv = v1 * -1
# This return a Vector2D with values (2.85, 3.8) or 95% of v2
dim = v2 * 0.95
```
Parameters: row (int) – The row/y parameter. column (int) – The column/x parameter.

Example:

```gravity = Vector2D(9.81, 0)
# Remember that minus on row is up.
speed = Vector2D(-0.123, 0.456)
# In that case you might want to increase the rounding precision
speed.rounding_precision = 3
```
`__init__`(row=0.0, column=0.0)

Initialize self. See help(type(self)) for accurate signature.

Methods

 `__init__`([row, column]) Initialize self. `from_direction`(direction, step) Build and return a Vector2D from a direction. `length`() Returns the length of a vector. `unit`() Returns a normalized unit vector.

Attributes

 `column` The column component of the vector. `row` The row component of the vector. `x` x is an alias for column. `y` y is an alias for row.
`column`

The column component of the vector.

classmethod `from_direction`(direction, step)

Build and return a Vector2D from a direction.

Directions are from the constants module.

Parameters: direction (int) – A direction from the constants module. step (int) – The number of cell to cross in one movement.

Example:

```v2d_up = Vector2D.from_direction(constants.UP, 1)
```
`length`()

Returns the length of a vector.

Return type: float

Example:

```if speed.length() == 0.0:
print('We are not moving... at all...')
```
`rounding_precision` = None

The rounding_precision attributes is used when vectors values are calculated and the result rounded for convenience. It can be changed anytime to increase or decrease the precision anytime.

`row`

The row component of the vector.

`unit`()

Returns a normalized unit vector.

Returns: A unit vector `Vector2D`

Example:

```gravity = Vector2D(9.81, 0)
next_position = item.position_as_vector() + gravity.unit()
```
`x`

x is an alias for column.

`y`

y is an alias for row.