@rickosborne/hexgrid
Version:
Rick Osborne's collection of hexagonal grid-related code.
48 lines (47 loc) • 1.85 kB
JavaScript
var __defProp = Object.defineProperty;
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
import { memoizeBinary, roundTo } from "@rickosborne/foundation";
import { SQRT_3, SQRT_3_2, TWO_PI } from "./hex-system.mjs";
const MAG_3_2 = 3 / 2;
const pixelFromFlatQRS = /* @__PURE__ */ __name(({ q, r }, scale = 1) => {
const x = scale * MAG_3_2 * q;
const y = scale * (SQRT_3_2 * q + SQRT_3 * r);
return { x, y };
}, "pixelFromFlatQRS");
const pixelFromPointyQRS = /* @__PURE__ */ __name(({ q, r }, scale = 1) => {
const x = scale * (SQRT_3 * q + SQRT_3_2 * r);
const y = scale * MAG_3_2 * r;
return { x, y };
}, "pixelFromPointyQRS");
const pixelFromQRS = /* @__PURE__ */ __name(({ q, r }, { orientation: { f0, f1, f2, f3 }, origin: { x: cx, y: cy }, size: { x: scaleX, y: scaleY } }) => {
const x = cx + (f0 * q + f1 * r) * scaleX;
const y = cy + (f2 * q + f3 * r) * scaleY;
return { x, y };
}, "pixelFromQRS");
const HEX_CORNERS = Object.freeze([0, 1, 2, 3, 4, 5]);
const pixelOffsetOfCorner = memoizeBinary((corner, { orientation: { start60Deg }, size: { x: scaleX, y: scaleY } }) => {
const rad = TWO_PI * (start60Deg + corner) / 6;
const x = scaleX * Math.cos(rad);
const y = scaleY * Math.sin(rad);
return Object.freeze({ x, y });
});
const hexCorners = /* @__PURE__ */ __name((qrs, layout) => {
const { x: cx, y: cy } = pixelFromQRS(qrs, layout);
const resolution = layout.resolution;
return HEX_CORNERS.map((corner) => {
const { x: ox, y: oy } = pixelOffsetOfCorner(corner, layout);
return {
x: roundTo(cx + ox, resolution),
y: roundTo(cy + oy, resolution)
};
});
}, "hexCorners");
export {
HEX_CORNERS,
hexCorners,
pixelFromFlatQRS,
pixelFromPointyQRS,
pixelFromQRS,
pixelOffsetOfCorner
};
//# sourceMappingURL=pixel-from-qrs.mjs.map