Skip to content

Vector

The Vector module provides utility functions for working with 2D vectors, commonly used in physics, game development, and mathematical computations.

A vector represents a direction and magnitude in a two-dimensional space. You can learn more about vectors here.

Type Definition

Vec2D

type Vec2D = {
x: number;
y: number;
};

Methods

normalize

export function normalize(v: Vec2D): Vec2D;

Normalizes a vector, making its length equal to 1 while preserving its direction.

scale

export function scale(v: Vec2D, scalar: number): Vec2D;

Scales a vector by a given factor, rounding values to three decimal places.

add

export function add(v1: Vec2D, v2: Vec2D): Vec2D;

Adds two vectors component-wise.

lerp

export function lerp(v1: Vec2D, v2: Vec2D, t: number): Vec2D;

Performs linear interpolation between two vectors based on parameter t (0 to 1).

angleBetween

export function angleBetween(v1: Vec2D, v2: Vec2D): number;

Calculates the angle between two vectors in radians.

equals

export function equals(v1: Vec2D, v2: Vec2D): boolean;

Checks if two vectors are exactly equal.

perpendicular

export function perpendicular(v: Vec2D): Vec2D;

Returns a perpendicular vector by rotating 90 degrees counterclockwise.

subtract

export function subtract(v1: Vec2D, v2: Vec2D): Vec2D;

Subtracts one vector from another.

dot

export function dot(v1: Vec2D, v2: Vec2D): number;

Computes the dot product of two vectors.

length

export function length(v: Vec2D): number;

Returns the magnitude (length) of a vector.

distance

export function distance(v1: Vec2D, v2: Vec2D): number;

Computes the Euclidean distance between two vectors.

angle

export function angle(v: Vec2D): number;

Computes the angle of a vector relative to the positive x-axis in degrees.

generateVectorFromAngle

export function generateVectorFromAngle(magnitude: number, angle: number): Vec2D;

Creates a vector from a given magnitude and angle (in degrees).

maxLength

export function maxLength(v: Vec2D, maxLength: number): Vec2D;

Limits a vector’s magnitude to a maximum length.

round

export function round(v: Vec2D): Vec2D;

Rounds the components of a vector to the nearest integer.

distanceToLine

export function distanceToLine(lineVec: Vec2D, pointVec: Vec2D): number;

Computes the perpendicular distance between a point and a line represented by a vector.

cosineSimilarity

export function cosineSimilarity(a: Vec2D, b: Vec2D): number;

Calculates the cosine similarity between two vectors, returning a value between -1 and 1.

subtractFromLength

export function subtractFromLength(v: Vec2D, amount: number): Vec2D;

Reduces the length of a vector by a given amount, without changing its direction.

degreeBetweenVectors

export function degreeBetweenVectors(a: Vec2D, b: Vec2D): number;

Computes the angle between two vectors in degrees.

Usage Example

import { Vector } from "@/types/physics";
const v1: Vec2D = { x: 3, y: 4 };
const v2: Vec2D = { x: 1, y: 2 };
const normalized = Vector.normalize(v1);
const sum = Vector.add(v1, v2);
const angle = Vector.angleBetween(v1, v2);
console.log(normalized, sum, angle);