# How to calculate 3D line-line interesction?

**URL:** <https://discourse.slicer.org/t/how-to-calculate-3d-line-line-interesction/26645>\
**Category:** Support\
**Created:** [December 8, 2022, 3:58am UTC](https://discourse.slicer.org/t/how-to-calculate-3d-line-line-interesction/26645 "2022-12-08T03:58:30Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![nnzzll](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/nnzzll/32/16350_2.png) [@nnzzll](https://discourse.slicer.org/u/nnzzll)\
**Post date:** [December 8, 2022, 3:58am UTC](https://discourse.slicer.org/t/how-to-calculate-3d-line-line-interesction/26645/1 "2022-12-08T03:58:30Z")

</div>

I got 2 infinite lines and each line was defined by two points.  
I’m trying to compute the intersection of the two lines with code below:

```auto
img1Line1P1 = [35.522341272816135, 244.0, -81.8808026069687]
img1Line1P2 = [36.1800944701046, 231.0, -81.31657002936069]
img2Line1P1 = [72.34037273903428, 244.0, -81.8925739257786]
img2Line1P2 = [70.43558942657907, 231.0, -81.32453925781688]

u = vtk.mutable(0)
v = vtk.mutable(0)

intersectType = vtk.vtkLine().Intersection(
img1Line1P1, img1Line1P2, img2Line1P1, img2Line1P2, u, v)
target11 = [img1Line1P1[i]+u*(img1Line1P2[i]-img1Line1P1[i]) for i in range(3)]
target12 = [img2Line1P1[i]+v*(img2Line1P2[i]-img2Line1P1[i]) for i in range(3)]
target1 = [(target11[i]+target12[i])/2 for i in range(3)]

print(intersectType)
print(u,v)
print(target11)
print(target12)
print(target1)

img1Line1P1 = [35.5223, 244, -81.8808]
img1Line1P2 = [36.4701, 231, -81.3736]
img2Line1P1 = [72.3401, 244, -81.8927]
img2Line1P2 = [70.4352, 231, -81.3244]

u = vtk.mutable(0)
v = vtk.mutable(0)

intersectType = vtk.vtkLine().Intersection(
img1Line1P1, img1Line1P2, img2Line1P1, img2Line1P2, u, v)
target11 = [img1Line1P1[i]+u*(img1Line1P2[i]-img1Line1P1[i]) for i in range(3)]
target12 = [img2Line1P1[i]+v*(img2Line1P2[i]-img2Line1P1[i]) for i in range(3)]
target1 = [(target11[i]+target12[i])/2 for i in range(3)]

print(intersectType)
print(u,v)
print(target11)
print(target12)
print(target1)

```

Output of code above goes like:

> 0  
> 14.367889142863488 14.367746083471086  
> [44.972866294820804, 57.217441142774646, -73.7739714811047]  
> [44.97292976164488, 57.21930091487587, -73.73119604989571]  
> [44.97289802823284, 57.21837102882526, -73.7525837655002]  
> 0  
> 12.902095599460948 12.899676454379204  
> [47.7509062091691, 76.27275720700769, -75.33685711195344]  
> [47.76750632205291, 76.30420609307035, -74.56181387097621]  
> [47.759206265611006, 76.28848165003902, -74.94933549146482]

As you can see, even with a tiny difference of the points coordinates, the intersection calculated was totally different, especially with the Y-axis.

I was wondering if my code is the correct way to calculate the intersection of two 3D lines? If not, is there a better way to calculate 3D line-line intersection?

---

<div class="post-metadata">

**Author:** ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)\
**Post date:** [December 8, 2022, 5:58pm UTC](https://discourse.slicer.org/t/how-to-calculate-3d-line-line-interesction/26645/2 "2022-12-08T17:58:06Z")

</div>

I used this function for intersection of finite line segments and it worked perfectly:

[http://paulbourke.net/geometry/pointlineplane/L3D.py](http://paulbourke.net/geometry/pointlineplane/L3D.py)

---

<div class="post-metadata">

**Author:** ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)\
**Post date:** [December 8, 2022, 6:02pm UTC](https://discourse.slicer.org/t/how-to-calculate-3d-line-line-interesction/26645/3 "2022-12-08T18:02:23Z")

</div>

The question was also asked on the VTK forum:

> **[How to calculate line line intersection](https://discourse.vtk.org/t/how-to-calculate-line-line-intersection/10108/4)**
>
> The question was also asked and answered on the Slicer forum:

@nnzzll please avoid cross-posting to prevent multiple people spending time with answering your question at the same time. Instead, post to the most relevant forum and give some time for people to respond.

If you post to multiple forums, e.g., because you are in some exceptional circumstance, like a very close deadline, then you must disclose every other location where you posted the question elsewhere so that people can check if the question was answered already, before taking the time to answer. Discourse warns me when I’m starting to answer a question and somebody else is already working on an answer within the same forum, so unnecessary duplicate efforts are minimized; but manual checking is needed if the question is asked at the same time on different forums.

---

<div class="post-metadata">

**Author:** ![esomjai](https://avatars.discourse-cdn.com/v4/letter/e/e274bd/32.png) [@esomjai](https://discourse.slicer.org/u/esomjai)\
**Post date:** [March 25, 2023, 12:15pm UTC](https://discourse.slicer.org/t/how-to-calculate-3d-line-line-interesction/26645/4 "2023-03-25T12:15:15Z")

</div>

Can you please advise on how to load this into slicer and what to modify if I would use the 4 control points of the two intersecting line in the codes? Thank you in advance.

---

<div class="post-metadata">

**Author:** ![jumbojing](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/jumbojing/32/10811_2.png) [@jumbojing](https://discourse.slicer.org/u/jumbojing)\
**Post date:** [March 25, 2023, 1:56pm UTC](https://discourse.slicer.org/t/how-to-calculate-3d-line-line-interesction/26645/5 "2023-03-25T13:56:55Z")

</div>

```auto
    def lines2P(p0, p01, p1, p11):
        '''lines2P 2线交点

        '''
        drt00 = np.array([p01[0]])
        drt01 = np.array([p01[1]])
        drt02 = np.array([p01[2]])
        drt10 = np.array([p11[0]])
        drt11 = np.array([p11[1]])
        p0x = np.array([p0[0]])
        p0y = np.array([p0[1]])
        p0z = np.array([p0[2]])
        p1x = np.array([p1[0]])
        p1y = np.array([p1[1]])
        t = (drt10 * (p1y - p0y) + drt11 * (p0x - p1x)) / ((drt10 * drt01) - (drt00 * drt11))
        x = p0x + drt00 * t
        y = p0y + drt01 * t
        z = p0z + drt02 * t
        return np.array([x[0], y[0], z[0]])

```

👆🏻我写了一个函数, 可以试一试啊
