uzb-d3-map
Version:
A flexible, customizable SVG map component for Uzbekistan built with D3 and React
46 lines • 1.64 kB
JavaScript
import { MAP_W2, MAP_H2 } from "../constants";
export const deg2rad = (d) => (d * Math.PI) / 180;
export const unitFromAngle = (aDeg) => ({
ux: Math.cos(deg2rad(aDeg)),
uy: Math.sin(deg2rad(aDeg)),
});
export function rotateUnit(ux, uy, turnDeg) {
const th = deg2rad(turnDeg);
const rx = ux * Math.cos(th) - uy * Math.sin(th);
const ry = ux * Math.sin(th) + uy * Math.cos(th);
const len = Math.hypot(rx, ry) || 1;
return { ux: rx / len, uy: ry / len };
}
export function unitFromSide(side, tiltDeg = 0) {
let base = { ux: 1, uy: 0 };
if (side === "left")
base = { ux: -1, uy: 0 };
if (side === "top")
base = { ux: 0, uy: -1 };
if (side === "bottom")
base = { ux: 0, uy: 1 };
return rotateUnit(base.ux, base.uy, tiltDeg);
}
export function distanceToRectEdgeAlongDir(hw, hh, ux, uy) {
const tx = ux !== 0 ? hw / Math.abs(ux) : Infinity;
const ty = uy !== 0 ? hh / Math.abs(uy) : Infinity;
return Math.min(tx, ty);
}
export function clampToView(x, y, w, h, margin = 6) {
const hw = w / 2 + margin, hh = h / 2 + margin;
return {
x: Math.max(hw, Math.min(MAP_W2 - hw, x)),
y: Math.max(hh, Math.min(MAP_H2 - hh, y)),
};
}
export function borderPointForSide(center, size, side) {
const hw = size.w / 2, hh = size.h / 2;
if (side === "right")
return { x: center.x - hw, y: center.y };
if (side === "left")
return { x: center.x + hw, y: center.y };
if (side === "top")
return { x: center.x, y: center.y + hh };
return { x: center.x, y: center.y - hh };
}
//# sourceMappingURL=geometry.js.map