如何排序基于Y轴的点的矢量? [英] How to Sort Vector of Points Based on a Y-Axis?
本文介绍了如何排序基于Y轴的点的矢量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一组坐标,例如:
10,40; 9,27; 5,68; 7,55; 8,15;
10,40; 9,27; 5,68; 7,55; 8,15;
如何排序这些坐标,而不会丢失排序Y轴的正确X轴。
How do I sort those coordinates without losing the correct X-Axis of the sorted Y-Axis.
从上面的例子中,我想对坐标进行排序,使正确的输出为:
From the example above I want to sort the coordinates so the correct output will be:
8,15; 9,27; 10,40; 7,55; 5,68。
8,15; 9,27; 10,40; 7,55; 5,68.
任何建议将非常感激。
谢谢。
Any suggestion will be greatly appreciated. Thank you.
推荐答案
#include "opencv2/core/core.hpp"
#include <algorithm> // std::sort
// This defines a binary predicate that,
// taking two values of the same type of those
// contained in the list, returns true if the first
// argument goes before the second argument
struct myclass {
bool operator() (cv::Point pt1, cv::Point pt2) { return (pt1.y < pt2.y);}
} myobject;
int main () {
// input data
std::vector<cv::Point> pts(5);
pts[0] = Point(10,40);
pts[1] = Point(9,27);
pts[2] = Point(5,68);
pts[3] = Point(7,55);
pts[4] = Point(8,15);
// sort vector using myobject as comparator
std::sort(pts.begin(), pts.end(), myobject);
}
这篇关于如何排序基于Y轴的点的矢量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文