Implementation and tests for vectorGrid() helper.

pull/1575/head
Rick Carlino 2019-11-14 09:59:25 -06:00
parent 1cf8a3c76d
commit d979ee6381
3 changed files with 64 additions and 1 deletions

View File

@ -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);
});
});

View File

@ -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 {

View File

@ -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;
}