refactor: fix burgId issue

This commit is contained in:
max 2022-08-19 23:58:45 +03:00
parent ec6285a7a4
commit bc98757b96
8 changed files with 62 additions and 41 deletions

View file

@ -5,13 +5,28 @@ import {round} from "utils/stringUtils";
export function drawRoutes() {
routes.selectAll("path").remove();
const {cells, burgs} = pack;
const lineGen = d3.line().curve(d3.curveBasis);
const getBurgCoords = (burgId: number): TPoint => {
if (!burgId) throw new Error("burgId must be positive");
const burg = burgs[burgId] as IBurg;
return [burg.x, burg.y];
};
const getPathPoints = (cellIds: number[]): TPoints =>
cellIds.map(cellId => {
const burgId = cells.burg[cellId];
if (burgId) return getBurgCoords(burgId);
return cells.p[cellId];
});
const routePaths: Dict<string[]> = {};
for (const {i, type, cells: routeCells} of pack.routes) {
const points = routeCells.map(cellId => pack.cells.p[cellId]);
const path = round(lineGen(points)!);
const points = getPathPoints(routeCells);
const path = round(lineGen(points)!, 1);
if (!routePaths[type]) routePaths[type] = [];
routePaths[type].push(`<path id="${type}${i}" d="${path}"/>`);