Q: Formula for finding distance to a line segment? I need some help with a formula or bit of code for finding the distance to the closest point in a line segment. Effectively, the "distance from the line segment" is going to be used as a sort of "bonding box", so that a user can click relatively close to a line, and it count as clicking on that line.

A: If you're using Swing, then you should be able to use Line2D.ptSegDist(Point2D) or its various overloads to do all the math for you.

A: This page explains how to find the closest point on a line.
It also includes a sample implementation in Java.

A: Java function
public static
double
distanceToSegment( final R3 v, final R3 a, final R3 b )

full gist:
https://gist.github.com/anonymous/6ca967360b5a6613604c416fe50d2d87
from open source library
https://sourceforge.net/projects/geokarambola/
