# Query re Line-Line Intersection

**URL:** <https://discourse.mcneel.com/t/query-re-line-line-intersection/62768>\
**Category:** Grasshopper Developer\
**Created:** [May 14, 2018, 4:01pm UTC](https://discourse.mcneel.com/t/query-re-line-line-intersection/62768 "2018-05-14T16:01:03Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![jjsolly](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/jjsolly/32/632251_2.png) [@jjsolly](https://discourse.mcneel.com/u/jjsolly)\
**Post date:** [May 14, 2018, 4:01pm UTC](https://discourse.mcneel.com/t/query-re-line-line-intersection/62768/1 "2018-05-14T16:01:03Z")

</div>

I’m trying to find the minimum distance between two line segments (specifically I want the actual line that exists between the points of minimum separation)

So I’m using the LineLine intersection but when you say it should use finite lengths it does not appear to be playing correctly. See the attached example where the closest point between the two infinite lines is outside the bounds of the red segment.

It appears that when one parameter is outside the bound 0\>1 this parameter alone is just snapped to the closest bound. But that’s not correct if the function should return the closest parameters as described in [http://developer.rhino3d.com/api/RhinoCommon/html/M\_Rhino\_Geometry\_Intersect\_Intersection\_LineLine\_1.htm](http://developer.rhino3d.com/api/RhinoCommon/html/M_Rhino_Geometry_Intersect_Intersection_LineLine_1.htm)

Any help appreciated.

James

 ![2018-05-14%2017_58_23-Grasshopper%20-%20unnamed](https://global.discourse-cdn.com/mcneel/original/3X/0/4/040b0eb93eb3e9634fa18b89cbb5e63eb3eac8c0.png)

---

<div class="post-metadata">

**Author:** ![AndersDeleuran](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/andersdeleuran/32/476683_2.png) [@AndersDeleuran](https://discourse.mcneel.com/u/AndersDeleuran)\
**Post date:** [May 16, 2018, 7:55am UTC](https://discourse.mcneel.com/t/query-re-line-line-intersection/62768/2 "2018-05-16T07:55:45Z")

</div>

Hi James, maybe [Line.MinimumDistanceTo](http://developer.rhino3d.com/5/api/RhinoCommonWin/html/M_Rhino_Geometry_Line_MinimumDistanceTo.htm) is more appropriate here.

**Edit 1:** That doesn’t actually return the params, so maybe not, my bad.

**Edit 2:** I’m getting the behaviour I would expect here, maybe upload your file for further inspection:

 ![0%20Educational%20-%20%5BPerspective%5D](https://global.discourse-cdn.com/mcneel/original/3X/3/9/3905a7bdf7a89a794336f156879f33306b78b272.png)

[180516\_ClosestLine\_00.gh](https://global.discourse-cdn.com/mcneel/uploads/default/original/3X/6/c/6c9cdb077c3fefaf8b084e7ab2a419fc5eeb7088.gh) (6.2 KB)

---

<div class="post-metadata">

**Author:** ![dsonntag](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/dsonntag/32/529361_2.png) [@dsonntag](https://discourse.mcneel.com/u/dsonntag)\
**Post date:** [May 16, 2018, 8:51am UTC](https://discourse.mcneel.com/t/query-re-line-line-intersection/62768/3 "2018-05-16T08:51:18Z")

</div>

I am not sure, but I would say the red line is clearly not the shortest distance between lineA and lineB, as it is supposed to be according to the API. In particular Point A is not the closest Point to lineB, but the intersection point of the infinite lines, which is not the behaviour I would expect according to the API.

 ![image](https://global.discourse-cdn.com/mcneel/original/3X/6/8/6852b845b61895c9a1f72907b1ed1d5c88c99f57.png)

[shortestDistance.gh](https://global.discourse-cdn.com/mcneel/uploads/default/original/3X/c/6/c6c1b4e4d11412d698f536464992e3c0b5bed147.gh) (7.1 KB)

---

<div class="post-metadata">

**Author:** ![AndersDeleuran](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/andersdeleuran/32/476683_2.png) [@AndersDeleuran](https://discourse.mcneel.com/u/AndersDeleuran)\
**Post date:** [May 16, 2018, 9:20am UTC](https://discourse.mcneel.com/t/query-re-line-line-intersection/62768/4 "2018-05-16T09:20:45Z")

</div>

> [@dsonntag](#):
>
> is clearly not the shortest distance between lineA and lineB

Its the intersection points of the finite lines within tolerance, which is the expected behaviour (I would say). There might be other ways around this, but one approach would be to check/catch these cases by evaluating whether the length of the resulting minimum line is equal to the `MinimumDistance` (as in my example above). If not, use [Line.ClosestPoint](http://developer.rhino3d.com/5/api/RhinoCommonWin/html/M_Rhino_Geometry_Line_ClosestPoint.htm) from the two intersection points onto the their opposing lines, and return the shorter of these two (which should result in what we’re looking for).

**Edit:** Like so:

 ![0%20Educational%20-%20%5BPerspective%5D](https://global.discourse-cdn.com/mcneel/original/3X/5/f/5fdb3c3ee0f6a6ab02e4fea2401436a53689b73c.png)

[180516\_ClosestLine\_01.gh](https://global.discourse-cdn.com/mcneel/uploads/default/original/3X/0/5/05b52f938cb7c59b6bc5b16ded1ee0d708bd529a.gh) (3.5 KB)

---

<div class="post-metadata">

**Author:** ![jjsolly](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/jjsolly/32/632251_2.png) [@jjsolly](https://discourse.mcneel.com/u/jjsolly)\
**Post date:** [May 16, 2018, 9:27am UTC](https://discourse.mcneel.com/t/query-re-line-line-intersection/62768/5 "2018-05-16T09:27:57Z")

</div>

> Its the intersection points of the finite lines within tolerance, which is the expected behaviour (I would say).

That’s actually a fair point but it’s not what the API says as far as I can tell, which is why I posted here.

_I actually already implemented something similar to your suggestion to solve my specific problem._ EDIT - my method was a lot less elegant than the one you just posted. So thanks! 🙂

The API says the following, which suggests the output points a and b should give the “shortest distance between the lines” which would then not be the projected intersection point, but the shortest link.

#### Parameters

lineA

Type: [Rhino.Geometry.Line](http://developer.rhino3d.com/api/RhinoCommon/html/T_Rhino_Geometry_Line.htm)  
First line for intersection.

lineB

Type: [Rhino.Geometry.Line](http://developer.rhino3d.com/api/RhinoCommon/html/T_Rhino_Geometry_Line.htm)  
Second line for intersection.

a

Type: [System.Double](http://msdn2.microsoft.com/en-us/library/643eft0t)  
Parameter on lineA that is closest to LineB. The shortest distance between the lines is the chord from lineA.PointAt(a) to lineB.PointAt(b)

b

Type: [System.Double](http://msdn2.microsoft.com/en-us/library/643eft0t)  
Parameter on lineB that is closest to LineA. The shortest distance between the lines is the chord from lineA.PointAt(a) to lineB.PointAt(b)

tolerance

Type: [System.Double](http://msdn2.microsoft.com/en-us/library/643eft0t)  
If tolerance \> 0.0, then an intersection is reported only if the distance between the points is \<= tolerance. If tolerance \<= 0.0, then the closest point between the lines is reported.

finiteSegments

Type: [System.Boolean](http://msdn2.microsoft.com/en-us/library/a28wyd50)  
If true, the input lines are treated as finite segments. If false, the input lines are treated as infinite lines.

---

<div class="post-metadata">

**Author:** ![AndersDeleuran](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/andersdeleuran/32/476683_2.png) [@AndersDeleuran](https://discourse.mcneel.com/u/AndersDeleuran)\
**Post date:** [May 16, 2018, 9:38am UTC](https://discourse.mcneel.com/t/query-re-line-line-intersection/62768/6 "2018-05-16T09:38:55Z")

</div>

Ah I see, yes that can surely be confusing. I think this maybe simply boils down to the particular semantics used here and what expectations one comes pre-loaded with in terms of [line-line-intersections](https://en.wikipedia.org/wiki/Line%E2%80%93line_intersection) then. That said, I feel like there is a method does actually return the shortest line between two curves in RhinoCommon 🤔

**Edit:** God damn it:

 ![0%20Educational%20-%20%5BPerspective%5D](https://global.discourse-cdn.com/mcneel/original/3X/7/9/79d8d8c528063778148aac943e1634f0669057bb.png)

---

<div class="post-metadata">

**Author:** ![jjsolly](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/jjsolly/32/632251_2.png) [@jjsolly](https://discourse.mcneel.com/u/jjsolly)\
**Post date:** [May 16, 2018, 9:48am UTC](https://discourse.mcneel.com/t/query-re-line-line-intersection/62768/7 "2018-05-16T09:48:15Z")

</div>

Ah. Yes. Good catch that man. Rhino has a way!

Also: Curve.GetDistancesBetweenCurves() would work and gives ALL THE INFOS regarding the parameters for mindistances etc. That just requires the lines to be cast to curves.

Anyways - to any McNeel people reading this, I do believe the API description specifically notes “shortest distance between the lines” which perhaps needs to be considered for an update, even if the method itself does what is wanted.
