@rickosborne/hexgrid
Version:
Rick Osborne's collection of hexagonal grid-related code.
67 lines (66 loc) • 3 kB
JavaScript
;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var pixel_from_qrs_exports = {};
__export(pixel_from_qrs_exports, {
HEX_CORNERS: () => HEX_CORNERS,
hexCorners: () => hexCorners,
pixelFromFlatQRS: () => pixelFromFlatQRS,
pixelFromPointyQRS: () => pixelFromPointyQRS,
pixelFromQRS: () => pixelFromQRS,
pixelOffsetOfCorner: () => pixelOffsetOfCorner
});
module.exports = __toCommonJS(pixel_from_qrs_exports);
var import_foundation = require("@rickosborne/foundation");
var import_hex_system = require("./hex-system.cjs");
const MAG_3_2 = 3 / 2;
const pixelFromFlatQRS = /* @__PURE__ */ __name(({ q, r }, scale = 1) => {
const x = scale * MAG_3_2 * q;
const y = scale * (import_hex_system.SQRT_3_2 * q + import_hex_system.SQRT_3 * r);
return { x, y };
}, "pixelFromFlatQRS");
const pixelFromPointyQRS = /* @__PURE__ */ __name(({ q, r }, scale = 1) => {
const x = scale * (import_hex_system.SQRT_3 * q + import_hex_system.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 = (0, import_foundation.memoizeBinary)((corner, { orientation: { start60Deg }, size: { x: scaleX, y: scaleY } }) => {
const rad = import_hex_system.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: (0, import_foundation.roundTo)(cx + ox, resolution),
y: (0, import_foundation.roundTo)(cy + oy, resolution)
};
});
}, "hexCorners");
//# sourceMappingURL=pixel-from-qrs.cjs.map