[英]How to Sort Vector of Points Based on a Y-Axis?
I have a group of coordinates for example : 我有一组坐标,例如:
10,40; 10,40; 9,27; 9,27; 5,68; 5.68; 7,55; 7.55; 8,15; 8,15;
How do I sort those coordinates without losing the correct X-Axis of the sorted Y-Axis. 如何在不丢失已排序Y轴的正确X轴的情况下对这些坐标进行排序。
From the example above I want to sort the coordinates so the correct output will be: 从上面的例子我想要排序坐标,所以正确的输出将是:
8,15; 8,15; 9,27; 9,27; 10,40; 10,40; 7,55; 7.55; 5,68. 5.68。
Any suggestion will be greatly appreciated. 任何建议将不胜感激。 Thank you. 谢谢。
Documentation for std::sort std :: sort的文档
#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);
}
You need to specify how exactly you are storing your group of coordinates. 您需要指定存储坐标组的准确程度。
The easiest way is to store them as a new struct you create and apply a basic bubble sort algorithm over the top of it, using the Y value as your sort parameter. 最简单的方法是将它们存储为您创建的新结构,并在其顶部应用基本的冒泡排序算法,使用Y值作为排序参数。 Then when you "swap" the position of the structs, the X & Y stay together. 然后当你“交换”结构的位置时,X和Y会保持在一起。
struct Vector {
float x;
float y;
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.