---
title: "Vector2"
description: "A 2D vector with x and y components."
canonical_url: "https://www.rosette.dev/docs/api-reference/Vector2"
markdown_url: "https://www.rosette.dev/docs/api-reference/Vector2.md"
source_url: "https://github.com/PreFab-Photonics/rosette/blob/f086d7670645fd36c05362d696d442a2b2e74850/www/content/docs/api-reference/Vector2.mdx"
docs_channel: "main"
docs_revision: "f086d7670645fd36c05362d696d442a2b2e74850"
---

# Vector2

A 2D vector with x and y components.

Vectors represent directions and displacements in the layout plane. They
are used for translations, port directions, and geometric calculations.
Vectors are immutable -- all methods return new Vector2 instances.

```python
v = Vector2(3, 4)
v.length()      # 5.0
v.normalize()   # Vector2(0.6, 0.8)

# Unit vectors and angle construction
right = Vector2.unit_x()            # Vector2(1, 0)
diagonal = Vector2.from_angle(45)   # Vector2(0.707..., 0.707...)
```

## Attributes



### `x`

```python
x: float
```

X component.





### `y`

```python
y: float
```

Y component.



## Static methods



### `unit_x`

```python
unit_x() -> Vector2
```

Return the unit vector along the X axis: `Vector2(1, 0)`.



**Returns:** `Vector2`





### `unit_y`

```python
unit_y() -> Vector2
```

Return the unit vector along the Y axis: `Vector2(0, 1)`.



**Returns:** `Vector2`





### `from_angle`

```python
from_angle(angle_deg) -> Vector2
```

Create a unit vector from an angle in degrees. 0 degrees points along +X,
90 degrees points along +Y.



- **`angle_deg`** (`float`)

  Angle in degrees (counter-clockwise from +X).





**Returns:** `Vector2`

A unit vector pointing in the given direction.



## Methods



### `__init__`

```python
__init__(x=0.0, y=0.0) -> None
```

Create a new vector.



- **`x`** (`float`, default `0.0`)

  X component.





- **`y`** (`float`, default `0.0`)

  Y component.





**Returns:** `None`





### `length`

```python
length() -> float
```

Return the Euclidean length (magnitude) of the vector.



**Returns:** `float`





### `normalize`

```python
normalize() -> Vector2
```

Return a unit vector in the same direction. The original vector must be
non-zero.



**Returns:** `Vector2`

A new vector with length 1.





### `dot`

```python
dot(other) -> float
```

Compute the dot product with another vector.



- **`other`** (`Vector2`)

  The other vector.





**Returns:** `float`

The scalar dot product.





### `perpendicular`

```python
perpendicular() -> Vector2
```

Return a vector perpendicular to this one (rotated 90 degrees
counter-clockwise).



**Returns:** `Vector2`





### `rotate`

```python
rotate(angle_deg) -> Vector2
```

Rotate the vector by the given angle.



- **`angle_deg`** (`float`)

  Rotation angle in degrees (counter-clockwise).





**Returns:** `Vector2`

A new rotated vector.



## Supported operations

**Addition**: `v1 + v2` returns a new `Vector2` that is the component-wise sum.

```python
Vector2(1, 2) + Vector2(3, 4)  # Vector2(4, 6)
```

**Subtraction**: `v1 - v2` returns a new `Vector2` that is the component-wise difference.

```python
Vector2(5, 7) - Vector2(1, 2)  # Vector2(4, 5)
```

**Scalar multiplication**: `v * scalar` or `scalar * v` scales both components.

```python
Vector2(3, 4) * 2    # Vector2(6, 8)
2 * Vector2(3, 4)    # Vector2(6, 8)
```

**Negation**: `-v` returns a vector pointing in the opposite direction.

```python
-Vector2(3, -4)  # Vector2(-3, 4)
```