distance between a point and a line
Suppose we have two points pt1, pt2 (for line) and one desired point pt3.
v1 = line vector (pt2-pt1)
v2 = vector between the first point of the line and the desired point pt3 (pt1-pt3)
v3 = perpendicular to the vector (swap v1.x, v1.y of v1, and multiply one of them to -1 to make v3)
h = distance between the pt3 and the line
dot(v2, v3) = |v2||v3|cos(theta + PI /2) ⟹
cos(theta + PI /2) = dot(v2, v3) / (|v2|| v3|) ⟹ sin(theta) = -dot (v2,v3) /(|v2||v3|)
and h = |v2| . sin(theta)
⟹ h = dot(v2,v3) / |v3| = dot(v2,norm(v3))
Code in GLSL:
float dist(vec2 pt1, vec2 pt2, vec2 pt3)
{
vec2 v1 = pt2 - pt1;
vec2 v2 = pt1 - pt3;
vec2 v3 = vec2(v1.y,-v1.x);
return abs(dot(v2,normalize(v3)));
}
See also https://www.ck12.org/book/ck-12-college-precalculus/section/9.6/ for "projecting one vector on another vector" as the other method.