#include <iostream>
#include <algorithm>
#include<vector>
#include<opencv2/opencv.hpp>
using namespace std;
using namespace cv;
struct Closer {Point v;
Closer(Point n) :v(n){} //构造函数
bool opeerator ()(const Point & a1, const Point & a2) { //排序要引用
double d1 = Distance(a1, v);
double d2 = Distance(a2, v);
if (d1 < d2)
return true;
return false;
}
double Distance(cv::Point n1, cv::Point n2) {
return cv::norm(n1 - n2);
}
};
int main()
{
TickMeter time;
time.start();
vector<Point> a;
a.push_back(Point(10, 10));
a.push_back(Point(50, 50));
a.push_back(Point(80, 80));
a.push_back(Point(100, 100));
Point c(70, 70);
sort(a.begin(), a.end(), Closer(c));
time.stop();
cout << time.getAvgTimeSec() << endl;
for ( auto it = a.begin(); it != a.end(); it++)
cout << *it << ",";
cout << endl;
return 0;
}
c++之按距离某点的距离排序
最新推荐文章于 2022-09-17 16:17:06 发布