-
Notifications
You must be signed in to change notification settings - Fork 2
/
geometry2d.js
2 lines (2 loc) · 7.34 KB
/
geometry2d.js
1
2
"use strict";function NULL_VEC(){return{x:0,y:0}}function pointInAARect(t,e){return t.x>=e.x&&t.x<=e.x+e.width&&t.y>=e.y&&t.y<=e.y+e.height}function pointInAABox(t,e){return t.x>=e.x-e.halfDimX&&t.x<=e.x+e.halfDimX&&t.y>=e.y-e.halfDimY&&t.y<=e.y+e.halfDimY}function pointInCircle(t,e){return distanceSq(t,e)<=e.radius*e.radius}function aaRectToRectOverlap(t,e){return t.x<e.x+e.width&&t.x+t.width>e.x&&t.y<e.y+e.height&&t.height+t.y>e.y}function aaBoxToBoxOverlap(t,e){return Math.abs(t.x-e.x)<t.halfDimX+e.halfDimX&&Math.abs(t.y-e.y)<t.halfDimY+e.halfDimY}function circleToCircleOverlap(t,e){var r=t.x-e.x,o=t.y-e.y;return r*r+o*o<=(t.radius+e.radius)*(t.radius+e.radius)+exports.EPSILON}function circleToAARectOverlap(t,e){var r=t.x-Math.max(e.x,Math.min(t.x,e.x+e.width)),o=t.y-Math.max(e.y,Math.min(t.y,e.y+e.height));return r*r+o*o<=t.radius*t.radius+exports.EPSILON}function circleToAABoxOverlap(t,e){var r=t.x-Math.max(e.x-e.halfDimX,Math.min(t.x,e.x+e.halfDimX)),o=t.y-Math.max(e.y-e.halfDimY,Math.min(t.y,e.y+e.halfDimY));return r*r+o*o<=t.radius*t.radius+exports.EPSILON}function translatePoint(t,e){return addVectors(t,e)}function translateAARect(t,e){return{x:t.x+e.x,y:t.y+e.y,width:t.width,height:t.height}}function translateAABox(t,e){return{x:t.x+e.x,y:t.y+e.y,halfDimX:t.halfDimX,halfDimY:t.halfDimY}}function translateCircle(t,e){return{x:t.x+e.x,y:t.y+e.y,radius:t.radius}}function aaRectToBox(t){var e=t.width/2,r=t.height/2;return{x:t.x+e,y:t.y+r,halfDimX:e,halfDimY:r}}function aaRectToCircle(t){var e=t.width/2,r=t.height/2;return{x:t.x+e,y:t.y+r,radius:Math.max(e,r)}}function aaBoxToRect(t){return{x:t.x-t.halfDimX,y:t.y-t.halfDimY,width:2*t.halfDimX,height:2*t.halfDimY}}function aaBoxToCircle(t){return{x:t.x,y:t.y,radius:Math.max(t.halfDimX,t.halfDimY)}}function circleToAARect(t){return{x:t.x-t.radius,y:t.y-t.radius,width:2*t.radius,height:2*t.radius}}function circleToAABox(t){return{x:t.x,y:t.y,halfDimX:t.radius,halfDimY:t.radius}}function distPointToAARect(t,e){var r=Math.max(Math.abs(t.x-e.x)-e.width/2,0),o=Math.max(Math.abs(t.y-e.y)-e.height/2,0);return Math.sqrt(r*r+o*o)}function distPointToCircle(t,e){return distance(t,e)-e.radius}function distanceSq(t,e){return(t.x-e.x)*(t.x-e.x)+(t.y-e.y)*(t.y-e.y)}function distance(t,e){return Math.sqrt(distanceSq(t,e))}function lengthSqVector(t){return t.x*t.x+t.y*t.y}function lengthVector(t){return Math.sqrt(lengthSqVector(t))}function oppositeVector(t){return{x:-t.x,y:-t.y}}function addVectors(t,e){return{x:t.x+e.x,y:t.y+e.y}}function subVectors(t,e){return{x:t.x-e.x,y:t.y-e.y}}function multVectorByScalar(t,e){return{x:t.x*e,y:t.y*e}}function scaleVector(t,e){return multVectorByScalar(normalizeVector(t),e)}function divVectorByScalar(t,e){return{x:t.x/e,y:t.y/e}}function normalizeVector(t){var e=lengthVector(t);return e>exports.EPSILON?{x:t.x/e,y:t.y/e}:NULL_VEC()}function limitVector(t,e){return lengthVector(t)>e?scaleVector(t,e):t}function dotProduct(t,e){return t.x*e.x+t.y*e.y}function detVectors(t,e){return t.x*e.y-t.y*e.x}function rotateVector(t,e){var r={x:0,y:0};return r.x=t.x*Math.cos(e)-t.y*Math.sin(e),r.y=t.x*Math.sin(e)+t.y*Math.cos(e),r}function tangentUnitVectorCircle(t,e){var r=subVectors(e,t);return normalizeVector({x:r.y,y:-r.x})}function tangentUnitVectorAABox(t,e){var r=new Array(4);r[0]={x:t.x-t.halfDimX,y:t.y-t.halfDimY,distanceSq:0},r[1]={x:t.x+t.halfDimX,y:t.y-t.halfDimY,distanceSq:0},r[2]={x:t.x+t.halfDimX,y:t.y+t.halfDimY,distanceSq:0},r[3]={x:t.x-t.halfDimX,y:t.y+t.halfDimY,distanceSq:0};for(var o=0;o<4;o++){var n=r[o];n.distanceSq=distanceSq(n,e)}return r.sort(function(t,e){return t.distanceSq-e.distanceSq}),normalizeVector(subVectors(r[1],r[0]))}function azimuthToAngle(t){var e=2.5*Math.PI-t;return e>2*Math.PI?e-2*Math.PI:e}function angleToAzimuth(t){return Math.PI/2-t}function vectorToAngle(t){return Math.atan2(t.y,t.x)}function vectorToAzimuth(t){return angleToAzimuth(vectorToAngle(t))}function angleToVector(t,e){return void 0===e&&(e=1),multVectorByScalar(angleToUnitVector(t),e)}function angleToUnitVector(t){return{x:Math.cos(t),y:Math.sin(t)}}function azimuthToVector(t,e){return void 0===e&&(e=1),multVectorByScalar(azimuthToUnitVector(t),e)}function azimuthToUnitVector(t){var e=azimuthToAngle(t);return{x:Math.cos(e),y:Math.sin(e)}}function angleUnitVectors(t,e){return Math.atan2(detVectors(t,e),dotProduct(t,e))}function getRandomPoint(t,e,r,o){return{x:Math.random()*e+t,y:Math.random()*o+r}}function isPoint(t){return"x"in t&&"y"in t}function isVector(t){return"x"in t&&"y"in t}function isAARect(t){return"x"in t&&"y"in t&&"width"in t&&"height"in t}function isAABox(t){return"x"in t&&"y"in t&&"halfDimX"in t&&"halfDimY"in t}function isCircle(t){return"x"in t&&"y"in t&&"radius"in t}function isNullVector(t){return Math.abs(t.x)<=exports.EPSILON&&Math.abs(t.y)<=exports.EPSILON}function isOrthogonalVectors(t,e,r){return void 0===r&&(r=0),Math.abs(dotProduct(t,e))<=exports.EPSILON+r}function isColinearVectors(t,e,r){return void 0===r&&(r=0),Math.abs(detVectors(t,e))<=exports.EPSILON+r}function isAlignedPoints(t){var e=t.length;if(e<=2)return!0;for(var r=0;r<e-2;r++)if(!isColinearVectors(subVectors(t[r+1],t[r]),subVectors(t[r+2],t[r+1])))return!1;return!0}Object.defineProperty(exports,"__esModule",{value:!0}),exports.EPSILON=1e-4,exports.COSPI4=Math.cos(Math.PI/4),exports.NULL_VEC=NULL_VEC,exports.pointInAARect=pointInAARect,exports.pointInAABox=pointInAABox,exports.pointInCircle=pointInCircle,exports.aaRectToRectOverlap=aaRectToRectOverlap,exports.aaBoxToBoxOverlap=aaBoxToBoxOverlap,exports.circleToCircleOverlap=circleToCircleOverlap,exports.circleToAARectOverlap=circleToAARectOverlap,exports.circleToAABoxOverlap=circleToAABoxOverlap,exports.translatePoint=translatePoint,exports.translateAARect=translateAARect,exports.translateAABox=translateAABox,exports.translateCircle=translateCircle,exports.aaRectToBox=aaRectToBox,exports.aaRectToCircle=aaRectToCircle,exports.aaBoxToRect=aaBoxToRect,exports.aaBoxToCircle=aaBoxToCircle,exports.circleToAARect=circleToAARect,exports.circleToAABox=circleToAABox,exports.distPointToAARect=distPointToAARect,exports.distPointToCircle=distPointToCircle,exports.distanceSq=distanceSq,exports.distance=distance,exports.lengthSqVector=lengthSqVector,exports.lengthVector=lengthVector,exports.oppositeVector=oppositeVector,exports.addVectors=addVectors,exports.subVectors=subVectors,exports.multVectorByScalar=multVectorByScalar,exports.scaleVector=scaleVector,exports.divVectorByScalar=divVectorByScalar,exports.normalizeVector=normalizeVector,exports.limitVector=limitVector,exports.dotProduct=dotProduct,exports.detVectors=detVectors,exports.rotateVector=rotateVector,exports.tangentUnitVectorCircle=tangentUnitVectorCircle,exports.tangentUnitVectorAABox=tangentUnitVectorAABox,exports.azimuthToAngle=azimuthToAngle,exports.angleToAzimuth=angleToAzimuth,exports.vectorToAngle=vectorToAngle,exports.vectorToAzimuth=vectorToAzimuth,exports.angleToVector=angleToVector,exports.angleToUnitVector=angleToUnitVector,exports.azimuthToVector=azimuthToVector,exports.azimuthToUnitVector=azimuthToUnitVector,exports.angleUnitVectors=angleUnitVectors,exports.getRandomPoint=getRandomPoint,exports.isPoint=isPoint,exports.isVector=isVector,exports.isAARect=isAARect,exports.isAABox=isAABox,exports.isCircle=isCircle,exports.isNullVector=isNullVector,exports.isOrthogonalVectors=isOrthogonalVectors,exports.isColinearVectors=isColinearVectors,exports.isAlignedPoints=isAlignedPoints;
//# sourceMappingURL=geometry2d.js.map