[RESOLVED] Intersecting Lines
I have to admit that I haven't even thought about this problem, so it may be SERIOUSLY easy, but I'm designing something different, and know this place is a source for good suggestions, so here it is:
I have two objects in a database. These objects could be defined down to two points, and I almost certainly will do it that way, though the objects are 3D. Therefore, there is a line segment defined by the points (X1,Y1) - (X2,Y2).
I will also have another table of line segments. Without any thought, I can query the database to find that set of line segments that are near the target line segment, but I'm looking for a way to determine whether one of the line segments in the table intersect the target line segment.
Here's the reason: One piece of a robot brain will be examining gaps in the map it is creating of the world. A gap is an unexplored area between two objects (which have been identified by other modules based on touch and ultrasonic echo location). One thing about echo location is that I only get single points from an object, so I can't tell without some kind of investigation whether two echoes are separate objects, or the same (there's MUCH more going into it than that, but I don't want to write a book here). Therefore, this module will figure out which gaps should be investigated to see whether or not they are real, or just blind spots. To do this, the robot will have to figure out whether or not it has traveled through the gaps that have been found. Thus, the points mentioned are the objects, and the line segment table will hold the past travels of the bot. Of course, gap detection is something the bot will be doing only when it doesn't have something more important to do, but it will go into building a more accurate world map.
Re: Colinear Line Segment Overlap
Quote:
Originally Posted by krtxmrtz
For example, to test if x
3 lies in between x
1 and x
2,
VB Code:
s = sgn(x2 - x1)
If x1*s <= x3*s AND x2*s >= x3*s Then
'...
Another way to make the same check:
Code:
If (x3 - x1) * (x3 - x2) <= 0 Then '...
Of course, you still have to make several checks to determine if there is any overlap of two colinear line segments. I have come up with a simpler check by comparing the length of the segments to the distance between their midpoints. If their combined length is at least twice the distance between their midpoints, then an overlap must occur, given that the segments are colinear.
Code:
If Abs(x1 - x2) + Abs(x3 - x4) >= Abs(x1 + x2 - x3 - x4) Then '...
Conversely, if their combined length is less than twice the distance between their midpoints, than an overlap cannot occur. This is certainly true even when the segments are not colinear. As such, the above test of the segments' horizontal projections can be combined with a similar test of their vertical projections to rule out the possibility of an intersection when the rectangular regions defined by the segments do not overlap.
Re: Colinear Line Segment Overlap
Quote:
Originally Posted by Logophobic
Another way to make the same check:
...
This is much easier, indeed!