UNPKG

@technobuddha/library

Version:
25 lines (24 loc) 808 B
import { type Cartesian, type Polygon } from './@types/geometry.ts'; /** * Calculates the centroid (geometric center) of a polygon. * * The centroid is computed using the formula for the centroid of a non-self-intersecting closed polygon. * The vertices should be provided in order (either clockwise or counterclockwise). * @param vertices - An array of points representing the vertices of the polygon. * @returns The centroid as a Cartesian coordinate. * @example * ```typescript * centroid([ * { x: 0, y: 0 }, * { x: 0, y: 5 }, * { x: 10, y: 5 }, * { x: 10, y: 0 }, * ]); * // { x: 5, y: 2.5 } * ``` * @remarks * The function assumes the polygon is non-self-intersecting. * @group Geometry * @category Polygon */ export declare function centroid(vertices: Polygon): Cartesian;