-1

Let's assume I have a (infinite) line defined as y = z * x how can I find the closest point in this line to any given coordinate? Technically I seek the intersection between the initial line and its perpendicular passing on the given coordinate.

Nuthinking
  • 1,211
  • 2
  • 13
  • 32
  • I found libraries which check for possible intersections between segments, but I would like to find a simpler formula since I know the intersection exists. – Nuthinking Jul 24 '18 at 16:18
  • `y =z * x` is not a line, but a ruled surface. Setting a plane (e.g. z= k) gives a line and the problem becomes a 2D case, very documented. – Ripi2 Jul 24 '18 at 18:09

2 Answers2

1

Let (x, zx) be a point on the given line, and (u, v) the outside point.

The squared distance is

(x - u)² + (zx - v)² = (z² + 1) x² - 2 (u + zv) x + u² + v²

and the minimum of this quadratic expression is achieved by

x = (u + zv) / (z² + 1)

giving you the orthogonal projection of the point onto the line.

0

I did a line intersection sample a few weeks a go. You could try that:

https://github.com/feldhaus/math-geometry-playground/blob/master/line-intersection/index.html

Maicon Feldhaus
  • 226
  • 2
  • 8