@rickosborne/hexgrid
Version:
Rick Osborne's collection of hexagonal grid-related code.
79 lines (78 loc) • 3.72 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 covering_grid_exports = {};
__export(covering_grid_exports, {
coverRect: () => coverRect,
svgForHexGrid: () => svgForHexGrid
});
module.exports = __toCommonJS(covering_grid_exports);
var import_foundation = require("@rickosborne/foundation");
var import_cube_round = require("./cube-round.cjs");
var import_cube = require("./cube.cjs");
var import_hex_contains_point = require("./hex-contains-point.cjs");
var import_pixel_from_qrs = require("./pixel-from-qrs.cjs");
var import_qrs_from_pixel = require("./qrs-from-pixel.cjs");
const coverRect = /* @__PURE__ */ __name((width, height, layout) => {
const rectCorners = [
{ x: 0, y: 0 },
{ x: width, y: 0 },
{ x: width, y: height },
{ x: 0, y: height }
];
const cubeCorners = rectCorners.map((xy) => (0, import_cube_round.cubeRound)((0, import_qrs_from_pixel.qrsFromPixel)(xy, layout, import_cube.cubeFromQR)));
const points = [];
const [rMin, rMax] = (0, import_foundation.minMax)(cubeCorners.map(({ r }) => r));
const [qMin, qMax] = (0, import_foundation.minMax)(cubeCorners.map(({ q }) => q));
const originCube = cubeCorners[0];
const hexContains = (0, import_hex_contains_point.hexContainsChecker)((0, import_pixel_from_qrs.hexCorners)(originCube, layout));
const inRect = /* @__PURE__ */ __name((p) => p.x >= 0 && p.x <= width && p.y >= 0 && p.y <= height, "inRect");
const rectIn = /* @__PURE__ */ __name(({ x: px, y: py }) => rectCorners.some(({ x: rcx, y: rcy }) => hexContains({ x: rcx - px, y: rcy - py })), "rectIn");
for (let q = qMin; q <= qMax; q++) {
for (let r = rMin; r <= rMax; r++) {
const px = (0, import_pixel_from_qrs.pixelFromQRS)({ q, r }, layout);
const corners = (0, import_pixel_from_qrs.hexCorners)({ q, r }, layout);
if (corners.some(inRect) || rectIn(px)) {
points.push({ corners, q, r, x: px.x, y: px.y });
}
}
}
return points;
}, "coverRect");
const svgForHexGrid = /* @__PURE__ */ __name((grid, layout) => {
const { height, hexes, width } = grid;
const { resolution } = layout;
const lines = [
`<svg width="${width}" height="${height}" viewBox="0 0 ${width} ${height}" xmlns="http://www.w3.org/2000/svg">`,
`<style>path { fill: #FFFFFF; stroke: #000000; stroke-width: 1; }</style>`
];
for (const hex of hexes) {
const path = [
"M",
(0, import_foundation.roundTo)(hex.corners[0].x, resolution),
(0, import_foundation.roundTo)(hex.corners[0].y, resolution),
...hex.corners.slice(1).map(({ x, y }) => `L ${(0, import_foundation.roundTo)(x, resolution)},${(0, import_foundation.roundTo)(y, resolution)}`),
"Z"
].join(" ");
lines.push(`<path d="${path}" id="q-${hex.q}-r-${hex.r}" />`);
}
lines.push("</svg>");
return lines.join("\n");
}, "svgForHexGrid");
//# sourceMappingURL=covering-grid.cjs.map