Skip to content

Instantly share code, notes, and snippets.

@rabssm
rabssm / closestpoint2dlines.py
Last active April 3, 2018 13:30
Find a point that is mutually closest to two or more lines in a least-squares sense. Python code.
# Find a point that is mutually closest to two or more lines in a least-squares sense.
# Based on: https://en.wikipedia.org/wiki/Line%E2%80%93line_intersection#Nearest_point_to_non-intersecting_lines
import numpy as np
import matplotlib.pyplot as plt
# Create two or more lines in the form of a matrix [[x1,y1], [x2,y2]]
lines = []
lines.append(np.asmatrix([[4, 4], [4, -3]]))
lines.append(np.asmatrix([[3, 2], [10, 2]]))
lines.append(np.asmatrix([[3, 3], [6, 4]]))