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.
Asked
Active
Viewed 272 times
-1

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 Answers
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.
-
Something doesn't work on my implementation: https://codepen.io/nuthinking/pen/8b9d12eca5c66011b5645d595c49ae82?editors=0011 – Nuthinking Jul 24 '18 at 17:59
-
@Nuthinking When I changed your code into y(x) dependence instead of weird mix, it became to work as needed – MBo Jul 24 '18 at 19:00
-
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