/* Returns the closest distance between point pnt and the segment that goes from point a to b Idea by: http://local.wasp.uwa.edu.au/~pbourke/geometry/pointline/ */ double distance_point_to_segment(const point &a,const point &b, const point &pnt){ double u = ((pnt.x - a.x)*(b.x - a.x) + (pnt.y - a.y)*(b.y - a.y)) /distsqr(a, b); point intersection; intersection.x = a.x + u*(b.x - a.x); intersection.y = a.y + u*(b.y - a.y); if (u < 0.0 || u > 1.0){ return min(dist(a, pnt), dist(b, pnt)); } return dist(pnt, intersection); }
bool graham_cmp(point a, point b) { int t = cross(pivot, b, a); if(t < 0) return true; if(t == 0) return distsqr(pivot, a) < distsqr(pivot, b); return false; }