排序坐标点 c++
在应用程序中,我测量了很多二维坐标(x,y) 图案。该图案由网格上的一组点组成,具有固定的 x 和 y 方向的节距。这些坐标都有一个分数 质量并根据此分数进行排序。我想做的是排序 这些坐标首先在 x 上并定义组(区域) 属于一起的 x 坐标。完成这一步后,我想对 y 区域中的不同 x 区域。
之后我可以将坐标标记为相应的 图案(网格)标签。
示例:测量坐标 (x,y)= (2,2),(2,3),(1,2),(1,3),(2,1),(1,1),(3,2),(3 ,3),(3 ,1)
步骤 1 后: (x,y)= (1,2),(1,3),(1,1) (2,2),(2,3),(2,1) (3,2),(3,3 ),(3,1)
步骤 2 后: (x,y)= (1,1),(1,2),(1,3),(2,1),(2,2),(2,3),(3,1),(3 ,2),(3 ,3)
是否有一个排序例程已经执行此任务?例行公事 如果未测量图案的某些坐标,也应该起作用。
有人可以给我一些线索吗,我不是经验丰富的 c++ 程序员,但也许通过一些提示我可以完成这项工作!
in an application I measure a lot of 2d coordinates (x,y) of a
pattern. This pattern consists of a set of points on grid with fixed
pitches in x and y direction. These coordinates all have a score for
quality and are sorted on this score. What I want to do is to sort
these coordinates first on x and define groups (regions) of
x-coordinates that belong together. After this step I want to sort the
different x-regions in y-regions.
After this I am able to label the coordinates to the corresponding
pattern (grid) label.
Example: Measured coordinates
(x,y)= (2,2),(2,3),(1,2),(1,3),(2,1),(1,1),(3,2),(3,3),(3 ,1)
after step 1:
(x,y)= (1,2),(1,3),(1,1) (2,2),(2,3),(2,1) (3,2),(3,3),(3,1)
after step 2:
(x,y)= (1,1),(1,2),(1,3),(2,1),(2,2),(2,3),(3,1),(3,2),(3 ,3)
Is there a sort routine that already performs this task? The routine
should also work if some coordinates of the pattern are not measured.
Can somebody give me some clues, I'm not an experienced c++
programmer, but maybe with some hints I can do the job!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您需要一个稳定的排序算法(巫婆不会改变相等元素的顺序)。首先按
y
坐标排序,然后按x
排序以获得所需结果:例如:
之前: (x,y)= (2,2),(2,3),(1,2),(1,3),(2,1),(1,1),(3,2), (3,3),(3,1)
按 y 排序:(x,y)= (2,1),(1,1),(3,1),(2,2),(1,2),(3,2),(2,3 ),(1,3),(3,3)
按 x 排序:(x,y)= (1,1),(1,2),(1,3),(2,1),(2,2),(2,3),(3,1 ),(3,2),(3,3)
You need a stable sort algorithm (witch does not change order of equal elements). First make sorting by
y
coordinate and next sort byx
to get desired result:For example:
before: (x,y)= (2,2),(2,3),(1,2),(1,3),(2,1),(1,1),(3,2),(3,3),(3,1)
sorted by y: (x,y)= (2,1),(1,1),(3,1),(2,2),(1,2),(3,2),(2,3),(1,3),(3,3)
sorted by x: (x,y)= (1,1),(1,2),(1,3),(2,1),(2,2),(2,3),(3,1),(3,2),(3,3)
您可以使用
std::sort
和自定义operator<
来完成此操作,例如:如果您不希望将“质量”存储在结构中,您可以随时直接在
operator<
中调用一些函数来计算它,例如:You can do this using
std::sort
and a customoperator<
, e.g.:If you don't want the 'quality' to be stored in the structure you can always call some function to compute it in
operator<
directly, e.g.:如果您知道数字的范围,您可以将 X 乘以某个大数字,然后将 y 添加到该数字。现在您可以简单地对单个数字进行排序,或者您可以使用 stl 库来完成它,就像其他人所描述的那样。
If you know the range of you numbers you could multiply the X by some large number and then add y to that number. Now you can simply just sort that single number or you can use stl library to do it as others have described.