@technobuddha/library
Version:
A large library of useful functions
20 lines (18 loc) • 650 B
text/typescript
import { type LineSegment, type Polygon } from './@types/geometry.ts';
import { modulo } from './modulo.ts';
import { toLineSegment } from './to-line-segment.ts';
/**
* Generate line segments for each side of the polygon.
* @param polygon - The polygon to extract sides from
* @returns Generator that yields line segments for each edge
* @group Geometry
* @category Polygon
* @category Line Segment
*/
export function* polygonSides(polygon: Polygon): Generator<LineSegment> {
for (let i = 0; i < polygon.length; i++) {
const p1 = polygon[i];
const p2 = polygon[modulo(i + 1, polygon.length)];
yield toLineSegment(p1, p2);
}
}