2014-04-14 49 views

回答

0
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) 
} 

使用一些C++ 11层的功能,这可以写成

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); }); 
+0

谢谢网络 – user3532163

相关问题