double Distance (double p1, double p2, double q1, double q2) {
double d1 = p1-q1, d2 = p2-q2;
return sqrt(d1*d1+d2*d2);
}
double PLSdistance(double a1, double a2, double b1, double b2, double c1, double c2) {
double ab1 = b1-a1, ab2 = b2-a2, ac1 = c1-a1, ac2 = c2-a2;
double abac = ab1*ac1 + ab2*ac2;
if(abac < 0) return Distance(a1, a2, c1, c2);
double abab = ab1*ab1 + ab2*ab2;
if(abac > abab) return Distance(b1, b2, c1, c2);
abac /= abab;
double d1 = a1+(b1-a1)*abac, d2 = a2+(b2-a2)*abac;
return Distance(c1, c2, d1, d2);
}
double PLdistance(double a1, double a2, double b1, double b2, double c1, double c2) {
double ab1 = b1-a1, ab2 = b2-a2, ac1 = c1-a1, ac2 = c2-a2;
double abxac = ab1*ac2 - ab2*ac1, abab = ab1*ab1 + ab2*ab2;
if(abxac < 0) abxac = -abxac;
return abxac / sqrt(abab);
}