
# tecs.math


Angle math and the two-dimensional geometry beneath it.

`wrapAngle` and `deltaAngle` operate on angles rather than vectors, so they
remain directly on `tecs.math`. Vector and point operations live under
`tecs.math.vec2`; native procedural fields live under `tecs.math.noise`.

```teal
local correction <const> = tecs.math.deltaAngle(
    rotation, targetRotation
)
rotation = tecs.math.wrapAngle(rotation + correction * response)

local directionX, directionY = tecs.math.vec2.normalize(
    targetX - x, targetY - y
)
```

Every angle uses radians. Both functions return a value in `[-pi, pi)`, so an
exact positive half-turn becomes negative pi.

Naming `tecs.math` does not load either subordinate module. Reading `vec2` or
`noise` loads that child once and returns its module table.

## Module contents

### Submodules

| Submodule | Description |
| --- | --- |
| [`tecs.math.noise`](/modules/math/noise/) | Native procedural scalar fields with configurable algorithms and fractal composition |
| [`tecs.math.vec2`](/modules/math/vec2/) | Allocation-free two-dimensional vector and point operations over separate coordinates |

### Functions

| Function | Kind | Description |
| --- | --- | --- |
| [`deltaAngle`](/modules/math/#tecs.math.deltaAngle) | <span class="tealdoc-kind-badge tealdoc-kind-static">Static</span> | Computes the shortest signed turn from one angle to another. |
| [`wrapAngle`](/modules/math/#tecs.math.wrapAngle) | <span class="tealdoc-kind-badge tealdoc-kind-static">Static</span> | Wraps an angle to one turn centered on zero. |

## Functions

<a id="tecs.math.deltaAngle"></a>
### tecs.math.deltaAngle <span class="tealdoc-kind-badge tealdoc-kind-static">Static</span>

Computes the shortest signed turn from one angle to another.



```teal
function tecs.math.deltaAngle(from: number, to: number): number
```

#### Arguments

| Name | Type | Description |
| --- | --- | --- |
| `from` | `number` | Starting angle in radians. |
| `to` | `number` | Destination angle in radians. |

#### Returns

| Type | Description |
| --- | --- |
| `number` | `to - from` wrapped to `[-pi, pi)`. An exact half-turn is negative pi. |

#### Examples


Finds the shortest turn toward a target rotation.


```teal
assert(tecs.math.deltaAngle(0, math.pi) == -math.pi)
```

<a id="tecs.math.wrapAngle"></a>
### tecs.math.wrapAngle <span class="tealdoc-kind-badge tealdoc-kind-static">Static</span>

Wraps an angle to one turn centered on zero.



```teal
function tecs.math.wrapAngle(radians: number): number
```

#### Arguments

| Name | Type | Description |
| --- | --- | --- |
| `radians` | `number` | A finite angle in radians. |

#### Returns

| Type | Description |
| --- | --- |
| `number` | The equivalent angle in `[-pi, pi)`, so positive pi becomes negative pi. |


#### Examples


Keeps a rotation within one turn before storing it.


```teal
assert(tecs.math.wrapAngle(math.pi) == -math.pi)
```