title: nearest intersection point to many lines in python
body: <p>I need a good algorithm for calculating the point that is closest to a collection of lines in python, preferably by using least squares. I found this post on a python implementation that doesn't work: </p>
  
  <p><a href="https://stackoverflow.com/questions/46563004/finding-the-centre-of-multiple-lines-using-least-squares-approach-in-python">Finding the centre of multiple lines using least squares approach in Python</a></p>
  
  <p>And I found this resource in Matlab that everyone seems to like... but I'm not sure how to convert it to python: </p>
  
  <p><a href="https://www.mathworks.com/matlabcentral/fileexchange/37192-intersection-point-of-lines-in-3d-space" rel="noreferrer">https://www.mathworks.com/matlabcentral/fileexchange/37192-intersection-point-of-lines-in-3d-space</a></p>
  
  <p>I find it hard to believe that someone hasn't already done this... surely this is part of numpy or a standard package, right? I'm probably just not searching for the right terms - but I haven't been able to find it yet. I'd be fine with defining lines by two points each or by a point and a direction. Any help would be greatly appreciated!</p>
  
  <p>Here's an example set of points that I'm working with: </p>
  
  <p>initial XYZ points for the first set of lines</p>
  
  <pre><code>array([[-7.07107037,  7.07106748,  1. ],
         [-7.34818339,  6.78264559,  1. ],
         [-7.61352972,  6.48335745,  1. ],
         [-7.8667115 ,  6.17372055,  1. ],
         [-8.1072994 ,  5.85420065,  1. ]])
  </code></pre>
  
  <p>the angles that belong to the first set of lines</p>
  
  <pre><code>[-44.504854, -42.029223, -41.278573, -37.145774, -34.097022]
  </code></pre>
  
  <p>initial XYZ points for the second set of lines</p>
  
  <pre><code>array([[ 0., -20. ,  1. ],
         [ 7.99789129e-01, -19.9839984,  1. ],
         [ 1.59830153e+00, -19.9360366,  1. ],
         [ 2.39423914e+00, -19.8561769,  1. ],
         [ 3.18637019e+00, -19.7445510,  1. ]])
  </code></pre>
  
  <p>the angles that belong to the second set of lines</p>
  
  <pre><code>[89.13244, 92.39087, 94.86425, 98.91849, 99.83488]
  </code></pre>
  
  <p>The solution should be the origin or very near it (the data is just a little noisy, which is why the lines don't perfectly intersect at a single point). </p>