diginext-utils
Version:
README.md
29 lines • 973 B
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.angleBetweenPoints = angleBetweenPoints;
/**
* Calculates the angle in degrees between two points.
* The angle is measured from the positive x-axis.
* Range: (-180, 180]
*
* @param x1 - X coordinate of first point (center)
* @param y1 - Y coordinate of first point (center)
* @param x2 - X coordinate of second point
* @param y2 - Y coordinate of second point
* @returns The angle in degrees, or 0 if not finite
*
* @example
* ```ts
* angleBetweenPoints(0, 0, 1, 0); // 0 (pointing right)
* angleBetweenPoints(0, 0, 0, 1); // 90 (pointing up)
* angleBetweenPoints(0, 0, -1, 0); // 180 or -180 (pointing left)
* ```
*/
function angleBetweenPoints(x1, y1, x2, y2) {
const dy = y2 - y1;
const dx = x2 - x1;
let theta = Math.atan2(dy, dx);
theta *= 180 / Math.PI;
return Number.isFinite(theta) ? theta : 0;
}
//# sourceMappingURL=angleBetweenPoints.js.map