UNPKG

svgdom

Version:

Straightforward DOM implementation for SVG, HTML and XML

51 lines (41 loc) 967 B
import { Box, NoBox } from '../other/Box.js' export class PointCloud extends Array { constructor (...args) { if (args.length === 1 && typeof args[0] === 'number') { super(args.shift()) } else { super() } // except multiple point arrays as input and merge them into one args.reduce((last, curr) => { last.push(...curr) return this }, this) } bbox () { if (!this.length) { return new NoBox() } let xMin = Infinity let xMax = -Infinity let yMin = Infinity let yMax = -Infinity this.forEach(function (p) { xMin = Math.min(xMin, p.x) xMax = Math.max(xMax, p.x) yMin = Math.min(yMin, p.y) yMax = Math.max(yMax, p.y) }) return new Box( xMin, yMin, xMax - xMin, yMax - yMin ) } merge (cloud) { return new PointCloud(this, cloud) } transform (m) { return new PointCloud(this.map((p) => p.transform(m))) } }