UNPKG

@core-graphics/rect

Version:
45 lines (39 loc) 1.1 kB
import { R as Rect } from '../../dist/lite-3fdd983e.esm.js'; import '@core-graphics/point'; import '@core-graphics/size'; /* ----------------------------------------------------------------------------- * Intersection methods * -----------------------------------------------------------------------------*/ /** * Checks if a Rect intersects another Rect */ function intersects(a, b) { return a.x < b.maxX && a.y < b.maxY && a.maxX > b.x && a.maxY > b.y; } /** * Returns a new Rect that represents the intersection between two Rects */ function intersection(a, b) { var x = Math.max(a.x, b.x); var y = Math.max(a.y, b.y); var x2 = Math.min(a.x + a.width, b.x + b.width); var y2 = Math.min(a.y + a.height, b.y + b.height); return new Rect({ x: x, y: y, width: x2 - x, height: y2 - y }); } /** * Returns whether two rects collide along each edge */ function collisions(a, b) { return { top: a.minY <= b.minY, right: a.maxX >= b.maxX, bottom: a.maxY >= b.maxY, left: a.minX <= b.minX }; } export { collisions, intersection, intersects };