Implementation and tests for vectorGrid() helper.
parent
1cf8a3c76d
commit
d979ee6381
|
@ -0,0 +1,37 @@
|
|||
import { vectorGrid } from "../generate_grid";
|
||||
|
||||
describe("vectorGrid", () => {
|
||||
it("handles zeros (edge case)", () => {
|
||||
const results = vectorGrid({
|
||||
startX: 0,
|
||||
startY: 0,
|
||||
spacingH: 0,
|
||||
spacingV: 0,
|
||||
numPlantsH: 0,
|
||||
numPlantsV: 0,
|
||||
});
|
||||
expect(results).toEqual([]);
|
||||
});
|
||||
|
||||
it("generates a grid", () => {
|
||||
const p = { // Prime numbers yeah
|
||||
startX: 11,
|
||||
startY: 31,
|
||||
spacingH: 5,
|
||||
spacingV: 7,
|
||||
numPlantsH: 2,
|
||||
numPlantsV: 3
|
||||
};
|
||||
const results = vectorGrid(p);
|
||||
expect(results.length).toEqual(p.numPlantsH * p.numPlantsV);
|
||||
const expected = JSON.stringify([
|
||||
[11, 31],
|
||||
[11, 38],
|
||||
[11, 45],
|
||||
[16, 31],
|
||||
[16, 38],
|
||||
[16, 45]
|
||||
]);
|
||||
expect(JSON.stringify(results)).toEqual(expected);
|
||||
});
|
||||
});
|
|
@ -1,9 +1,11 @@
|
|||
export type PlantGridKey =
|
||||
"startX" | "startY" | "spacingH" | "spacingV" | "numPlantsH" | "numPlantsV";
|
||||
|
||||
export type PlantGridData = Record<PlantGridKey, number>;
|
||||
|
||||
export interface PlantGridState {
|
||||
status: "clean" | "dirty",
|
||||
grid: Record<PlantGridKey, number>;
|
||||
grid: PlantGridData;
|
||||
}
|
||||
|
||||
export interface PlantGridProps {
|
||||
|
|
|
@ -0,0 +1,24 @@
|
|||
import { TaggedPlant } from "../../map/interfaces";
|
||||
import { PlantGridData } from "./constants";
|
||||
import { range } from "lodash";
|
||||
|
||||
export function vectorGrid(params: PlantGridData): [number, number][] {
|
||||
const rows = range(params.startX,
|
||||
params.startX + (params.numPlantsH * params.spacingH),
|
||||
params.spacingH);
|
||||
|
||||
const cols = range(params.startY,
|
||||
params.startY + (params.numPlantsV * params.spacingV),
|
||||
params.spacingV);
|
||||
|
||||
const results: [number, number][] = [];
|
||||
|
||||
rows.map(x => cols.map(y => results.push([x, y])));
|
||||
|
||||
return results;
|
||||
}
|
||||
|
||||
export function initPlantGrid(_params: PlantGridData): TaggedPlant[] {
|
||||
const output: TaggedPlant[] = [];
|
||||
return output;
|
||||
}
|
Loading…
Reference in New Issue