How can I find the minimum distance between a point (X0, Y0) and a sets of points {P0, ..., Pn} with the gsl library in c++ ? I have a polygon containing a sets of points {P0 (X0, Y0), ..., P (Xn, Yn)} and a point C (X0, Y0), I Want to calculate the minimum distance between C and sets of points .
Thanks
double min_dist = DBL_MAX; // some large number
Point pointC; // set this to the point you want to compare
vector<Point> pointList; // push_back all your points
for (auto it = pointList.begin(); it != pointList.end(); ++it)
{
Point tempPoint = *it;
// Calculate distance between C and 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 some C++11 features, this can be 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); });