c++ - gls library,Find the minimum distance between a point and a sets of points? -
how can find minimum distance between point (x0, y0) , sets of points {p0, ..., pn} gsl library in c++ ? have polygon containing sets of points {p0 (x0, y0), ..., p (xn, yn)} , point c (x0, y0), want calculate minimum distance between c , sets of points .
thanks
double min_dist = dbl_max; // large number point pointc; // set point want compare vector<point> pointlist; // push_back points (auto = pointlist.begin(); != pointlist.end(); ++it) { point temppoint = *it; // calculate distance between c , current point double newdist = sqrt(pow((temppoint.x - pointc.x),2) + pow((temppoint.y - pointc.y),2)) // update min_dist if necessary min_dist = min(min_dist, newdist) }
using c++11 features, can written as
double min_dist = *std::min_element(begin(pointlist), end(pointlist), [pointc](const point& lhs, const point& rhs){ return std::hypot(lhs.x - pointc.x, lhs.y - pointc.y) < std::hypot(rhs.x - pointc.x, rhs.y - pointc.y); });
Comments
Post a Comment