File Exchange

## Line-Line Intersection (N lines, D space)

version 1.0.0.0 (2.86 KB) by Serge

### Serge (view profile)

Find intersection of N lines in D-dimensional space, in least squares sense.

8 Downloads

Updated 29 Oct 2016

View License

Find intersection of N lines in D-dimensional space, in least squares sense.
X = lineXline(A,B) -line starts & ends as N*D
X = lineXline({x y..}) -lines as D long cell of starts & ends as 2*N
[X,P,R] = lineXline(..) -extra outputs
[X,P,R,x,t,l] = lineXline(..) -plot outputs
X: Intersection point, in least squares sense, as 1*D
P: Nearest point to the intersection on each line, as N*D
R: Distance from intersection to each line, as N*1
x: Intersection point X as D-long cell of coordinates {x y..}
p: Nearest points P as D-long cell of coordinates {x y..} as N*1
l: Initial lines A-to-B as D-long cell of coordinates {x y..} as 2*N

Remarks:
-Lines are assumed to be infinite in both directions.
-Finds point nearest to all lines using minimum sum of squared distances.
-For parallel lines returns an arbitrary point and a warning.
-For lines of length zero returns NaNs.
-Comments/issues/corrections email Serge: s3rg3y@hotmail.com

Example: (3 lines, 2 dimensions)
[X,P,R,x,p,l] = lineXline([0 0;3 0;0 4],[5 5;0 5;5 2]);
plot(x{:},'*k',p{:},'.k',l{:})
X2 = lineXline(l) %alternate input form, same results

Example: (2 lines, 3 dimensions)
[X,P,R,x,p,l] = lineXline(rand(2,3),rand(2,3));
plot3(x{:},'*k',p{:},'.k',l{:})

Example: (4 lines, 5 dimensions)
[X,P,R] = lineXline(rand(4,5),rand(4,5))

Richard Adams

Soyoung Park

Olivier Arm

Serge

Please rate

### Updates

 29 Oct 2016 1.0.0.0 Changed description 21 Oct 2016 1.0.0.0 Changed 5th output, minor changes 18 Oct 2016 1.0.0.0 Minor changes 18 Oct 2016 1.0.0.0 Minor changes 18 Oct 2016 1.0.0.0 Minor changes 18 Oct 2016 1.0.0.0 Minor changes 18 Oct 2016 1.0.0.0 Minor changes 18 Oct 2016 1.0.0.0 Minor changes 18 Oct 2016 1.0.0.0 Minor changes 18 Oct 2016 1.0.0.0 Minor changes 18 Oct 2016 1.0.0.0 Minor changes
##### MATLAB Release Compatibility
Created with R2010b
Compatible with any release
##### Platform Compatibility
Windows macOS Linux