UNPKG

@dartbot/dartboard

Version:

Dartboard implemented as a vanilla web component

60 lines 1.88 kB
/** * Gets a Cartesian point (x, y) from a polar point */ export const getPoint = (point) => ({ x: getX(point), y: getY(point), }); /** * Gets a polar point from Cartesian coordinates * @param x Cartesian X coordinate * @param y Cartesian Y coordinate * @returns Polar point representation of the cartesian coordinate */ export const getPolar = (x, y) => { let radius = Math.sqrt(x * x + y * y); let angle = Math.atan2(y, x); if (radius < 0) { radius = Math.abs(radius); angle += Math.PI; } if (angle < 0) { angle += Math.PI * 2; } return { radius, angle }; }; /** * Get the X value of the Cartesian point * @param point Polar point to convert to cartesian * @returns X value of the cartesian point */ export const getX = (point) => point.radius * Math.cos(point.angle); /** * Get the Y value of the Cartesian point * @param point Polar point to convert to cartesian * @returns Y value of the cartesian point */ export const getY = (point) => point.radius * Math.sin(point.angle); /** * Returns a polar point that is the vector of the two other points */ export const addPolar = (p1, p2) => { const c1 = getPoint(p1); const c2 = getPoint(p2); const x = c1.x + c2.x; const y = c1.y + c2.y; return getPolar(x, y); }; /** * Returns true if the point is valid. A valid point is a point * where the radius and angle are both finite numbers * @param p Polar point with radius and angle */ export const isValidPolar = (p) => Number.isFinite(p?.radius) && Number.isFinite(p?.angle); /** * Returns true if the point is valid. A valid point is * one where the x and y coordinates are both finite numbers * @param p Cartesian point with x and y coordinates */ export const isValidPoint = (p) => Number.isFinite(p?.x) && Number.isFinite(p?.y); //# sourceMappingURL=polar-point.js.map