Skip to content

Commit

Permalink
add distance limits
Browse files Browse the repository at this point in the history
  • Loading branch information
langonginc committed Jan 4, 2025
1 parent 61c4363 commit 1f3834b
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 12 deletions.
4 changes: 3 additions & 1 deletion src/components/svg-canvas-graph.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,9 @@ const SvgCanvas = () => {
...selected,
...activePolylines.map(ap => ap.node),
]);
const flag = !activePolylines.some(ap => ap.a === l.a && ap.b === l.b);
const flag =
activePolylines.length === 0 ||
!activePolylines.some(ap => ap.a === l.a && ap.b === l.b);
if (d < 10 && activePolylines.length < 2 && flag) {
setActivePolylines([...activePolylines, l]);
}
Expand Down
3 changes: 3 additions & 0 deletions src/constants/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,14 @@ export type RuntimeMode =
export interface Polyline {
/**
* l: ax + by + c = 0
* node: (x, y)
*/
a: number;
b: number;
c: number;
node: StnId | MiscNodeId;
x: number;
y: number;
}

export interface Polylines {
Expand Down
28 changes: 17 additions & 11 deletions src/util/graph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,10 +136,10 @@ export const getPolylines = (graph: MultiDirectedGraph<NodeAttributes, EdgeAttri
.forEach(node => {
const x = graph.getNodeAttribute(node, 'x');
const y = graph.getNodeAttribute(node, 'y');
polylinesX.push({ a: 1, b: 0, c: -x, node } as Polyline);
polylinesY.push({ a: 0, b: 1, c: -y, node } as Polyline);
polylinesP.push({ a: 1, b: 1, c: -x - y, node } as Polyline);
polylinesN.push({ a: 1, b: -1, c: -x + y, node } as Polyline);
polylinesX.push({ a: 1, b: 0, c: -x, node, x, y } as Polyline);
polylinesY.push({ a: 0, b: 1, c: -y, node, x, y } as Polyline);
polylinesP.push({ a: 1, b: 1, c: -x - y, node, x, y } as Polyline);
polylinesN.push({ a: 1, b: -1, c: -x + y, node, x, y } as Polyline);
});
return {
x: polylinesX,
Expand All @@ -154,16 +154,22 @@ export const getPolylineDistance = (line: Polyline, x: number, y: number) => {
};

export const getNearestPolyline = (x: number, y: number, polylines: Polyline[], nodes: Id[]) => {
const pointDistance = (x1: number, y1: number, x2: number, y2: number) =>
Math.sqrt((x1 - x2) ** 2 + (y1 - y2) ** 2);

let minDistance = Infinity,
minLine = { a: 0, b: 0, c: 0, node: 'stn_null' } as Polyline;
retDistance = Infinity,
minLine = { a: 0, b: 0, c: 0, node: 'stn_null', x: 0, y: 0 } as Polyline;
polylines
.filter(l => !nodes.includes(l.node))
.forEach((line, index) => {
const distance = getPolylineDistance(line, x, y);
if (distance < minDistance) {
minDistance = distance;
.filter(l => !nodes.includes(l.node) && pointDistance(x, y, l.x, l.y) < 100)
.forEach(line => {
const lineDis = getPolylineDistance(line, x, y);
const pointDis = pointDistance(x, y, line.x, line.y);
if (lineDis + pointDis < minDistance) {
minDistance = lineDis + pointDis;
retDistance = lineDis;
minLine = line;
}
});
return { l: minLine, d: minDistance };
return { l: minLine, d: retDistance };
};

0 comments on commit 1f3834b

Please sign in to comment.