refactor: drawCultures

This commit is contained in:
max 2022-08-02 00:36:02 +03:00
parent 6a3ef0f790
commit cbe6da0949
4 changed files with 70 additions and 33 deletions

View file

@ -1,41 +1,22 @@
import {connectVertices} from "scripts/connectVertices";
import * as d3 from "d3";
import {getPaths} from "./utilts";
export function drawCultures() {
/* uses */ const {cells, vertices, cultures} = pack;
cults.selectAll("path").remove(); // cleanup
const getType = (cellId: number) => cells.culture[cellId];
const paths = getPaths(cells.c, cells.v, vertices, getType);
const used = new Uint8Array(cells.i.length);
const paths = new Array(cultures.length).fill("");
const getColor = (i: number) => i && (cultures[i] as ICulture).color;
for (const i of cells.i) {
if (!cells.culture[i]) continue;
if (used[i]) continue;
used[i] = 1;
const cultureId = cells.culture[i];
const onborder = cells.c[i].some(n => cells.culture[n] !== cultureId);
if (!onborder) continue;
const startingVertex = cells.v[i].find(v => vertices.c[v].some(i => cells.culture[i] !== cultureId));
if (startingVertex === undefined)
throw new Error(`Draw cultures: starting vertex for culture ${cultureId} is not found`);
const ofSameType = (cellId: number) => cells.culture[cellId] === cultureId;
const chain = connectVertices({vertices, startingVertex, ofSameType});
if (chain.length < 3) continue;
const points = chain.map(v => vertices.p[v]);
paths[cultureId] += "M" + points.join("L") + "Z";
}
const data = paths.map((p, i) => [p, i]).filter(d => d[0].length > 10);
cults
d3.select("#cults")
.selectAll("path")
.data(data)
.remove()
.data(Object.entries(paths))
.enter()
.append("path")
.attr("d", d => d[0])
.attr("fill", d => cultures[d[1]].color)
.attr("id", d => "culture" + d[1]);
.attr("d", ([, path]) => path)
.attr("fill", ([i]) => getColor(Number(i)))
.attr("id", ([i]) => "culture" + i);
}