UNPKG

@js-draw/math

Version:
41 lines (40 loc) 1.49 kB
/** * An abstract base class for 2D shapes. */ export class Abstract2DShape { /** * @returns the distance from `point` to this shape. If `point` is within this shape, * this returns the distance from `point` to the edge of this shape. * * @see {@link signedDistance} */ distance(point) { return Math.abs(this.signedDistance(point)); } /** * Returns `true` if and only if the given `point` is contained within this shape. * * `epsilon` is a small number used to counteract floating point error. Thus, if * `point` is within `epsilon` of the inside of this shape, `containsPoint` may also * return `true`. * * The default implementation relies on `signedDistance`. * Subclasses may override this method to provide a more efficient implementation. */ containsPoint(point, epsilon = Abstract2DShape.smallValue) { return this.signedDistance(point) < epsilon; } /** * Returns a bounding box that **loosely** fits the content of this shape. * * The result of this call can be larger than the result of {@link getTightBoundingBox}, * **but should not be smaller**. Thus, a call to `getLooseBoundingBox` can be significantly * faster than a call to {@link getTightBoundingBox} for some shapes. */ getLooseBoundingBox() { return this.getTightBoundingBox(); } } // @internal Abstract2DShape.smallValue = 1e-12; export default Abstract2DShape;