@core-graphics/rect
Version:
JS utilities for managing rects
51 lines (43 loc) • 1.29 kB
JavaScript
;
Object.defineProperty(exports, '__esModule', { value: true });
var lite_dist_coreGraphicsRectLite = require('../../dist/lite-6b05303c.cjs.dev.js');
require('@core-graphics/point');
require('@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 lite_dist_coreGraphicsRectLite.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
};
}
exports.collisions = collisions;
exports.intersection = intersection;
exports.intersects = intersects;