UNPKG

romgrk-2d-geometry

Version:

Javascript library for 2d geometry

77 lines (76 loc) 2.62 kB
import { Box } from '../classes/Box'; import { IntervalTree } from '../data_structures/interval-tree'; /** * Class representing a planar set - a generic container with ability to keep and retrieve shapes and * perform spatial queries. Planar set is an extension of Set container, so it supports * Set properties and methods */ export class PlanarSet extends Set { /** * Create new instance of PlanarSet * @param shapes - array or set of geometric objects to store in planar set * Each object should have a <b>box</b> property */ constructor(shapes) { super(shapes); this.index = new IntervalTree(); this.forEach(shape => this.index.insert(shape)); } /** * Add new shape to planar set and to its spatial index.<br/> * If shape already exist, it will not be added again. * This happens with no error, it is possible to use <i>size</i> property to check if * a shape was actually added.<br/> * Method returns planar set object updated and may be chained * @param shape - shape to be added, should have valid <i>box</i> property */ add(shape) { const size = this.size; super.add(shape); // size not changed - item not added, probably trying to add same item twice if (this.size > size) { this.index.insert(shape.box, shape); } return this; // in accordance to Set.add interface } /** * Delete shape from planar set. Returns true if shape was actually deleted, false otherwise * @param shape - shape to be deleted */ delete(shape) { const deleted = super.delete(shape); if (deleted) { this.index.remove(shape.box, shape); } return deleted; } /** * Clear planar set */ clear() { super.clear(); this.index = new IntervalTree(); } /** * 2d range search in planar set.<br/> * Returns array of all shapes in planar set which bounding box is intersected with query box * @param box - query box */ search(box) { return this.index.search(box); } /** * Point location test. Returns array of shapes which contains given point * @param point - query point */ hit(point) { const box = new Box(point.x - 1, point.y - 1, point.x + 1, point.y + 1); return this.index.search(box).filter((shape) => point.on(shape)); } /** * Returns svg string to draw all shapes in planar set */ svg() { return [...this].reduce((acc, shape) => acc + shape.svg(), ''); } }