![](/img/trans.png)
[英]How to align the Y-Axis of an object with a vector eminating from the centre point of their parent
[英]How to Sort Vector of Points Based on a Y-Axis?
我有一組坐標,例如:
10,40; 9,27; 5.68; 7.55; 8,15;
如何在不丟失已排序Y軸的正確X軸的情況下對這些坐標進行排序。
從上面的例子我想要排序坐標,所以正確的輸出將是:
8,15; 9,27; 10,40; 7.55; 5.68。
任何建議將不勝感激。 謝謝。
#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值作為排序參數。 然后當你“交換”結構的位置時,X和Y會保持在一起。
struct Vector {
float x;
float y;
};
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.