computational geometry – What is the best approach to find the nearest line segment (x1,y1),(x2,y2) from an point (x,y)?

The distance from a point to a line is given by the projection of a line from the point to some point on the line onto a unit vector orthogonal to the line.

First we need same data.

lines = Partition(RandomReal({-1, 1}, {6, 2}), 2)
point = RandomReal({-1, 1}, 2)
Graphics({Line(lines), PointSize(0.02), Red, Point(point)}, 
 Axes -> True)

enter image description here

Then we need unit vectors orthogonal to our lines:

ortho = Normalize /@ (Subtract @@@ dat).{{0, 1}, {-1, 0}};

The distance from the point to the lines is now given by the absolute value of the projection of the line from point to the start of the lines onto the orthogonal unit vector:

dist = MapThread((Abs((point - #1).#2)) &, {lines((All, 1)), ortho})
(*{0.0292868, 0.260295, 0.347937}*)

It is obvious, for our example, that the first line is closest to the point.