I'm trying to write a code that generates a visibility graph from a set of points and walls (obstacles). My algorithms is not correct and fails on some cases where there is more than one wall intersecting an edge between two points.
Here's kind of a pseudo-python code for my algorithm :
Intersect(wall, P, Q):
returns True if wall segment intersects with PQ segment
Cross(wall, P, Q):
returns True if wall segment crosses PQ segment
for i in range(len(nodes)):
for j in range(i + 1, len(nodes)):
flag = True
for wall in walls:
if (Cross(wall, nodes[i].pos, nodes[j].pos)):
flag = False
if (flag):
nodes[i].adj.append(nodes[j])
nodes[j].adj.append(nodes[i])
How can I fix my algorithm?
Here's one of the tests where it fails:
Walls :
w1 -> (1, 0),(2, 1)
w2 -> (2, 1),(3, 2)
Nodes to be checked:
node1 -> (0, 2)
node2 -> (4, 0)
There shouldn't be an edge but my algorithm generates an edge because the edge does not Cross any wall (it intersects but not cross).
For clarification, Cross
means that two segments intersect (share a point,) but they don't share any point that is either the start or end of any of the two segments.