Comments (3)
Thinking about this more I understand why it happens now.
- Line 2 intersects Line 1: Nodes 1, 2 and 4 are connected
- Line 3 intersects Line 1: Nodes 1, 3 and 4 are connected
Therefore Nodes 2 and 3 never get connected.
I don't suppose any of your extensive libraries have a way to find the adjacent nodes here?
from isect.
I came up with a simple solution to resolve this, it's not optimal in that it adds redundant work to the path-finding as it creates overlapping connections.
But I store a separate Map
that maps lines to all discovered intersections so far. Each new intersection on that line I map to all the other intersections.
const lineNodeMapping = new Map<string, Set<string>>();
const intersections = detectIntersections.run();
const graph = createGraph<{ point: { x: number; y: number } }>();
intersections.forEach((intersection) => {
const intersectionId = isectPointToId(intersection.point);
graph.addNode(intersectionId, { point: intersection.point });
intersection.segments.forEach((isectSegment) => {
const fromId = isectPointToId(isectSegment.from);
const toId = isectPointToId(isectSegment.to);
const lineId = `${fromId}-${toId}`;
const lineNodes = lineNodeMapping.get(lineId) ?? new Set();
graph.addNode(fromId, { point: isectSegment.from });
graph.addNode(toId, { point: isectSegment.to });
graph.addLink(fromId, intersectionId);
graph.addLink(toId, intersectionId);
// link this intersection with other intersections on that line
lineNodes.forEach((nodeId) => {
graph.addLink(nodeId, intersectionId);
});
lineNodes.add(intersectionId);
lineNodeMapping.set(lineId, lineNodes);
});
});
from isect.
I found a solution for generating the optimal graph like so, points
are a list of all the line points and the intersections.
function pointsToGraph(points: Point[]) {
const xValues: Set<number> = new Set();
const yValues: Set<number> = new Set();
const graph = createGraph<{ point: Point }>();
points.forEach((point) => {
const [x, y] = point;
xValues.add(x);
yValues.add(y);
graph.addNode(pointId(x, y), { point });
});
function pointId(x: number, y: number) {
return `${x},${y}`;
}
const sortedXValues = Array.from(xValues).sort((a, b) => a - b);
const sortedYValues = Array.from(yValues).sort((a, b) => a - b);
for (let i = 0; i < sortedYValues.length; i++) {
for (let j = 0; j < sortedXValues.length; j++) {
const x = sortedXValues[j];
const y = sortedYValues[i];
const currentId = pointId(x, y);
// eslint-disable-next-line no-continue
if (!graph.hasNode(currentId)) continue;
for (let k = j - 1; k >= 0; k--) {
const previousXId = pointId(sortedXValues[k], y);
if (graph.hasNode(previousXId)) {
graph.addLink(previousXId, currentId);
break;
}
}
for (let k = i - 1; k >= 0; k--) {
const previousYId = pointId(x, sortedYValues[k]);
if (graph.hasNode(previousYId)) {
graph.addLink(previousYId, currentId);
break;
}
}
}
}
return graph;
}
from isect.
Related Issues (7)
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from isect.