I am writing a Typescript game where there are vertexes drawn to a canvas, and some edges coming from them. I want to determine if some of edges from different vertexes intersect. For this, I am using a generic doLinesIntersect
function to determine if two edges intersect.
However, the problem is that it shows all lines as intersecting, because if two edges share a vertex, they compute as always intersecting, but I do not want this. I was able to get it “working” by checking manually to see if they share a vertex like so:
function doLinesTrulyIntersect(aX1: number, aY1: number, aX2: number, aY2: number, bX1: number, bY1: number, bX2: number, bY2: number): boolean {
const sharesVertexes = (aX1 === bX1 && aY1 === bY1) ||
(aX1 === bX2 && aY1 === bY2) ||
(aX2 === bX1 && aY2 === bY1) ||
(aX2 === bX2 && aY2 === bY2);
return !sharesVertexes && doLinesIntersect(
aX1, aY1, aX2, aY2,
bX1, bY1, bX2, bY2
);
}
Basically, if two edges share a vertex, then they do not intersect.
But, will this always work? The logic is that since if two edges have the same vertex, then (because they’re the same point), floating point equality testing should work, right? It’s the exact same bits under the hood, after all.
However, I know that in general testing two floating points for equality is incorrect and inconsistent, but does that apply in this case?