**1**

# Intersection of two line segment in 3 dimensions

###
#1
Members - Reputation: **100**

Posted 20 May 2012 - 06:21 AM

I have a simple question for more of us.

I am developing a project and I need to calculate if two line segment intersect.

is there any mathematical way to do that?

With more details:

Segment_1 is from point A to point B, where A=(X_a, Y_a, Z_a) and B=(X_b, Y_b, Z_b)

Segment_2 is from point C to point D, where C=(X_c, Y_c, Z_c) and D=(X_d, Y_d, Z_d)

Is there any condition between the points coordinates that determines if the two segments intersect?

Thanks in advance

###
#2
Members - Reputation: **1839**

Posted 20 May 2012 - 08:20 AM

1. Compute the closest pair of points on both lines. See e.g. MathGeoLib. (link to code).

2. If their distance is smaller than some arbitrary small threshold value (e.g. 0.001f), consider the lines intersecting, false otherwise. Vector distance.

The threshold value effectively transforms the two lines into infinitely long cylinders, and tests them for collision.

Edit: As pointed out below, just noticed the question was for determining intersection between a pair of line <b>segments</b>. For that case, the same method above is used, but computing the closest point pairs in #1 requires clamping the points onto the line segment range. See LineSegment::ClosestPoint(LineSegment) (code) in MathGeoLib.

**Edited by clb, 20 May 2012 - 11:47 PM.**

###
#3
Crossbones+ - Reputation: **2655**

Posted 20 May 2012 - 08:46 AM

T1 = B - A

M1 = A x B

T2 = D - C

M2 = C x D

Then the two lines intersect if and only if

T1 * M2 + T2 * M1 = 0

where * means the dot product.

Of course, in floating-point calculations, you're unlikely to see a value of exactly zero, so you'd want to instead check for values very close to zero.

If you'd like to know more about where the above stuff comes from, look into Grassmann algebra and homogeneous coordinates. Here's a presentation:

http://www.terathon.com/gdc12_lengyel.pdf

I work on this stuff: C4 Engine | The 31st | Mathematics for 3D Game Programming and Computer Graphics | Game Engine Gems | OpenGEX

**Kickstarter in progress** for our Halloween FPS game, *The 31st*.

###
#5
Crossbones+ - Reputation: **2655**

Posted 20 May 2012 - 10:19 AM

Aren't those both line-line tests and not line segment-line segment tests?

Yes, you're right. I must have been to sleepy to see the word "segment".

I work on this stuff: C4 Engine | The 31st | Mathematics for 3D Game Programming and Computer Graphics | Game Engine Gems | OpenGEX

**Kickstarter in progress** for our Halloween FPS game, *The 31st*.

###
#6
Members - Reputation: **366**

Posted 20 May 2012 - 11:20 AM

Two instances of primitives overlap where their two sets of solutions overlap (ie. literally, intersect).

That said, do you really think that two line segments in 3D are likely to ever intersect? They really wouldn't be likely to do so, if you think about it, which is why clb gave you the hint to go a different route and instead get the shortest "co-perpendicular" line segment that spans between them. Here's a nice link explaining the general idea, in English (and codelish), for two line segments in 3D:

http://local.wasp.uw...try/lineline3d/

Bourke has some seriously great things on his website. Note how he explains that both a line and a line segment can be defined parametrically by two points and a scalar, in the exact same form. Like Bourke says, the scalar has an interval of [0, 1] for the line segment, and an interval of for the line. Same difference.

In general, just remember that it's the set of solutions that defines where a specific instance of a primitive exists, and that the overlap of solutions is where two specific instances of primitives intersect. If you still don't quite get what I mean, then know that you'd likely get a better sense of it if you work through a line-circle/sphere intersection test . Don't be confused by the fact that they define the line using a point and a unit vector; it's basically the same thing that Bourke did, but in a slightly different dressing. You'd be very well off to figure out how to convert between the two forms. Tinker.

Once you really get the idea in general, you'd likely be able to write your own intersection tests on the fly.

**Edited by taby, 20 May 2012 - 11:49 AM.**

###
#7
Crossbones+ - Reputation: **2419**

Posted 20 May 2012 - 03:23 PM

###
#8
Members - Reputation: **366**

Posted 21 May 2012 - 01:06 PM

Indeed, as other posters said the exact intersection between two lines is very easy (simple algebra), but due to floating point errors etc two lines in 3D are very unlikely to intersect exactly.

That's true regarding floating point precision, and thanks for adding that, because it is technically important. That said, it could go both ways, because sometimes there may be non-intersecting lines that are erroneously found to be intersecting because the non-zero shortest distance between them was whittled down to zero due to limited precision. What I'm saying is that you bring up a good point, and I thank you for that.

I should be more specific a lot of times: I was more referring to how the parallel postulate doesn't really apply in 3D as it does in 2D. Most cases in 3D are skew lines, where the lines are not parallel but yet still fail to intersect. It's the extra, third spatial dimension that makes the critical difference here. Now, two non-parallel planes in 3D do intersect, and it's pretty much because the extra, third spatial dimension is accompanied by the extra, second dimension inherent to the primitive in question (ie. the plane is 2D, not 1D like the line is). I honestly wish I knew the name of the theorem / postulate / axiom behind this, but it's escaping me. Maybe it's just called the generalized parallel postulate for Euclidean space, or something, I dunno. I guess the general rule of thumb is: For a pair of nD "plane" primitives, you need more than 2n embedding dimensions for the primitives to be able to be skew. So, for a pair of 1D lines you'd need a 3D space for them to be skew, and for a pair of 2D planes you'd need a 5D space for them to be skew, and for a pair of 3D hyperplanes you'd need a 7D space for them to be skew, etc, etc.

Sorry, just going around clarifying stuff that I wrote earlier. It was my fault that I didn't do so earlier.

**Edited by taby, 21 May 2012 - 01:57 PM.**