9
\$\begingroup\$

Given a point and a line segment in the plane, output the Euclidian distance between them, that is the distance between the given point and the nearest point on the line segment. Because the line is not infinitely long, This nearest point might lie inside the line segment or be an endpoint. The two cases look like this, with \$A\$ and \$B\$ defining the line segment, and \$P\$ being the third point. \$d\$ is the the desired distance:

The line segment is given by its two endpoints. So, the input consists of three points in \$\mathbb{R}^2\$: the two endpoints of the segment, and the point. Since each point is given by its x- and y-coordinate, the input consists of six real numbers. Your code can accept these six coordinates or three points in any reasonable format. You can also accept the coordinates of the line segment as a pair or array of coordinates.

You may assume that the endpoint of the line segment are distinct, and that the point does not lie on the line segment itself.

This is , so fewest bytes to achieve this wins. Standard loopholes are forbidden by default.

Test cases

A         B         P                      d
0,0       0,2       1,1                    1
0,2       0,0       1,1                    1
0,0       0,2       -1,1                   1
0,2       0,0       -1,1                   1
0,0       2,2       0,2                    1.4142135623730951
0,0       2,2       -3,5                   1.4142135623730951
0.9,0.9   1.1,1.1   -3,5                   1.4142135623730951
0,0       2,2       0,1                    1
10,10     11,11     0,0                    14.142135623730951
0,0       4,6       5,1                    3.605551275463989
0,0       4,6       5.6666666666666666,2   3.605551275463989
\$\endgroup\$
  • 4
    \$\begingroup\$ Can we not take the line as e.g. [[x1,y1],[x2,y2]]? \$\endgroup\$ – Adám Oct 27 at 20:59
  • 3
    \$\begingroup\$ I like this challenge idea, but it needs some clarification and cleanup. When you talk about the starting and ending positions of the points defining the line, does this mean the line is actually a line segment that ends there? Or does it still go forever through these points? \$\endgroup\$ – xnor Oct 27 at 21:00
  • 1
    \$\begingroup\$ @Adám yes, this is not an infinite line. \$\endgroup\$ – forever Oct 27 at 21:01
  • 1
    \$\begingroup\$ OK, thanks. Btw, welcome to, and very nice first challenge. You may want to use the sandbox for future challenges, though. \$\endgroup\$ – Adám Oct 27 at 21:03
  • 8
    \$\begingroup\$ Hello, and welcome to the site. I think this challenge is interesting. I am going to close it to prevent any answers while the details from above are ironed out. This is by no means permanent. You can still hammer it out here or in the sandbox. When these have been fixed users can vote to reopen or you can flag it for moderator attention and we can have a check to open it back up. \$\endgroup\$ – Wheat Wizard Oct 27 at 21:39

Browse other questions tagged or ask your own question.