rectangles
Version:
A function collection for working with rectangles
8 lines (7 loc) • 1.1 kB
JavaScript
/*
Rectangles.js v0.0.4 08/04/2016
Copyright (c) 2016, Robert Eisele (robert@xarg.org)
Dual licensed under the MIT or GPL Version 2 licenses.
*/
(function(d){var c={intersect:function(a,b){return a.x1<b.x2&&a.x2>b.x1&&a.y1<b.y2&&a.y2>b.y1},intersection:function(a,b){return{x1:Math.max(a.x1,b.x1),y1:Math.max(a.y1,b.y1),x2:Math.min(a.x2,b.x2),y2:Math.min(a.y2,b.y2)}},normalize:function(a){return{x1:Math.min(a.x1,a.x2),y1:Math.min(a.y1,a.y2),x2:Math.max(a.x1,a.x2),y2:Math.max(a.y1,a.y2)}},area:function(a){return Math.abs(a.x2-a.x1)*Math.abs(a.y2-a.y1)},height:function(a){return Math.abs(a.y2-a.y1)},width:function(a){return Math.abs(a.x2-a.x1)},
perimeter:function(a){return 2*(Math.abs(a.x2-a.x1)+Math.abs(a.y2-a.y1))},center:function(a){return{x:a.x1+(a.x2-a.x1)/2,y:a.y1+(a.y2-a.y1)/2}},insetBy:function(a,b){return{x1:a.x1+b,y1:a.y1+b,x2:a.x2-b-b,y2:a.y2-b-b}}};"function"===typeof define&&define.amd?define([],function(){return c}):"object"===typeof exports?(Object.defineProperty(exports,"__esModule",{value:!0}),c["default"]=c,c.Rectangles=c,module.exports=c):d.Rectangles=c})(this);