# Module: `nupp.math.vec2` Two-dimensional vector operations over `(x, y)` number pairs. ## Functions ### `add` _function_ Adds two vectors. ```nupp local add: function(ax: number, ay: number, bx: number, by: number): (number, number) ``` #### Arguments | Name | Type | Description | | --- | --- | --- | | `ax` | `number` | the first vector's x component | | `ay` | `number` | the first vector's y component | | `bx` | `number` | the second vector's x component | | `by` | `number` | the second vector's y component | #### Returns | Type | Description | | --- | --- | | `number` | the sum's x component | | `number` | the sum's y component | ### `angle` _function_ Computes a vector's signed direction from the positive x-axis. ```nupp local angle: function(x: number, y: number): number ``` #### Arguments | Name | Type | Description | | --- | --- | --- | | `x` | `number` | the vector's x component | | `y` | `number` | the vector's y component | #### Returns | Type | Description | | --- | --- | | `number` | the angle in radians, or zero for the zero vector | ### `angleBetween` _function_ Computes the smallest unsigned angle between two vectors. ```nupp local angleBetween: function(ax: number, ay: number, bx: number, by: number): number ``` #### Arguments | Name | Type | Description | | --- | --- | --- | | `ax` | `number` | the first vector's x component | | `ay` | `number` | the first vector's y component | | `bx` | `number` | the second vector's x component | | `by` | `number` | the second vector's y component | #### Returns | Type | Description | | --- | --- | | `number` | the angle from zero through pi, or zero for a zero vector | ### `cross` _function_ Computes the signed scalar cross product of two vectors. ```nupp local cross: function(ax: number, ay: number, bx: number, by: number): number ``` #### Arguments | Name | Type | Description | | --- | --- | --- | | `ax` | `number` | the first vector's x component | | `ay` | `number` | the first vector's y component | | `bx` | `number` | the second vector's x component | | `by` | `number` | the second vector's y component | #### Returns | Type | Description | | --- | --- | | `number` | the scalar cross product | ### `distance` _function_ Computes the distance between two points. ```nupp local distance: function(ax: number, ay: number, bx: number, by: number): number ``` #### Arguments | Name | Type | Description | | --- | --- | --- | | `ax` | `number` | the first point's x coordinate | | `ay` | `number` | the first point's y coordinate | | `bx` | `number` | the second point's x coordinate | | `by` | `number` | the second point's y coordinate | #### Returns | Type | Description | | --- | --- | | `number` | the distance | ### `distanceSquared` _function_ Computes the squared distance between two points without a square root. ```nupp local distanceSquared: function(ax: number, ay: number, bx: number, by: number): number ``` #### Arguments | Name | Type | Description | | --- | --- | --- | | `ax` | `number` | the first point's x coordinate | | `ay` | `number` | the first point's y coordinate | | `bx` | `number` | the second point's x coordinate | | `by` | `number` | the second point's y coordinate | #### Returns | Type | Description | | --- | --- | | `number` | the squared distance | ### `dot` _function_ Computes the dot product of two vectors. ```nupp local dot: function(ax: number, ay: number, bx: number, by: number): number ``` #### Arguments | Name | Type | Description | | --- | --- | --- | | `ax` | `number` | the first vector's x component | | `ay` | `number` | the first vector's y component | | `bx` | `number` | the second vector's x component | | `by` | `number` | the second vector's y component | #### Returns | Type | Description | | --- | --- | | `number` | the dot product | ### `length` _function_ Computes a vector's magnitude. ```nupp local length: function(x: number, y: number): number ``` #### Arguments | Name | Type | Description | | --- | --- | --- | | `x` | `number` | the vector's x component | | `y` | `number` | the vector's y component | #### Returns | Type | Description | | --- | --- | | `number` | the magnitude | ### `lengthSquared` _function_ Computes a vector's squared magnitude without a square root. ```nupp local lengthSquared: function(x: number, y: number): number ``` #### Arguments | Name | Type | Description | | --- | --- | --- | | `x` | `number` | the vector's x component | | `y` | `number` | the vector's y component | #### Returns | Type | Description | | --- | --- | | `number` | the squared magnitude | ### `lerp` _function_ Linearly interpolates or extrapolates between two vectors. ```nupp local lerp: function(ax: number, ay: number, bx: number, by: number, t: number): (number, number) ``` #### Arguments | Name | Type | Description | | --- | --- | --- | | `ax` | `number` | the starting x component | | `ay` | `number` | the starting y component | | `bx` | `number` | the ending x component | | `by` | `number` | the ending y component | | `t` | `number` | the unclamped interpolation factor | #### Returns | Type | Description | | --- | --- | | `number` | the interpolated x component | | `number` | the interpolated y component | ### `moveTowards` _function_ Moves toward a target by at most a distance, without overshooting. ```nupp local moveTowards: function(ax: number, ay: number, bx: number, by: number, distance: number): (number, number) ``` #### Arguments | Name | Type | Description | | --- | --- | --- | | `ax` | `number` | the starting x component | | `ay` | `number` | the starting y component | | `bx` | `number` | the target x component | | `by` | `number` | the target y component | | `distance` | `number` | the maximum step; nonpositive values leave the start unchanged | #### Returns | Type | Description | | --- | --- | | `number` | the resulting x component | | `number` | the resulting y component | ### `normalize` _function_ Returns a unit vector in the same direction, or `(0, 0)` for zero. ```nupp local normalize: function(x: number, y: number): (number, number) ``` #### Arguments | Name | Type | Description | | --- | --- | --- | | `x` | `number` | the vector's x component | | `y` | `number` | the vector's y component | #### Returns | Type | Description | | --- | --- | | `number` | the normalized x component | | `number` | the normalized y component | ### `project` _function_ Projects a vector onto another vector. ```nupp local project: function(x: number, y: number, ontoX: number, ontoY: number): (number, number) ``` #### Arguments | Name | Type | Description | | --- | --- | --- | | `x` | `number` | the vector's x component | | `y` | `number` | the vector's y component | | `ontoX` | `number` | the projection axis's x component | | `ontoY` | `number` | the projection axis's y component | #### Returns | Type | Description | | --- | --- | | `number` | the projected x component, or zero for a zero axis | | `number` | the projected y component, or zero for a zero axis | ### `reflect` _function_ Reflects a vector across a normal. ```nupp local reflect: function(x: number, y: number, normalX: number, normalY: number): (number, number) ``` #### Arguments | Name | Type | Description | | --- | --- | --- | | `x` | `number` | the vector's x component | | `y` | `number` | the vector's y component | | `normalX` | `number` | the normal's x component | | `normalY` | `number` | the normal's y component | #### Returns | Type | Description | | --- | --- | | `number` | the reflected x component, or the input x for a zero normal | | `number` | the reflected y component, or the input y for a zero normal | ### `rotate` _function_ Rotates a vector around the origin. ```nupp local rotate: function(x: number, y: number, radians: number): (number, number) ``` #### Arguments | Name | Type | Description | | --- | --- | --- | | `x` | `number` | the vector's x component | | `y` | `number` | the vector's y component | | `radians` | `number` | the signed rotation in radians | #### Returns | Type | Description | | --- | --- | | `number` | the rotated x component | | `number` | the rotated y component | ### `scale` _function_ Multiplies a vector by a scalar. ```nupp local scale: function(x: number, y: number, factor: number): (number, number) ``` #### Arguments | Name | Type | Description | | --- | --- | --- | | `x` | `number` | the vector's x component | | `y` | `number` | the vector's y component | | `factor` | `number` | the scale factor | #### Returns | Type | Description | | --- | --- | | `number` | the scaled x component | | `number` | the scaled y component | ### `signedAngleBetween` _function_ Computes the smallest signed angle from the first vector to the second. ```nupp local signedAngleBetween: function(ax: number, ay: number, bx: number, by: number): number ``` #### Arguments | Name | Type | Description | | --- | --- | --- | | `ax` | `number` | the first vector's x component | | `ay` | `number` | the first vector's y component | | `bx` | `number` | the second vector's x component | | `by` | `number` | the second vector's y component | #### Returns | Type | Description | | --- | --- | | `number` | the angle in `[-pi, pi)`, or zero for a zero vector | ### `subtract` _function_ Subtracts the second vector from the first. ```nupp local subtract: function(ax: number, ay: number, bx: number, by: number): (number, number) ``` #### Arguments | Name | Type | Description | | --- | --- | --- | | `ax` | `number` | the first vector's x component | | `ay` | `number` | the first vector's y component | | `bx` | `number` | the second vector's x component | | `by` | `number` | the second vector's y component | #### Returns | Type | Description | | --- | --- | | `number` | the difference's x component | | `number` | the difference's y component |