[英]sorting PointF array
Could anyone help me with creating an algoritm, IComparer, or some method that would sort an array or list of PointF
elements. 任何人都可以帮助我创建算法,IComparer或某种对PointF
元素的数组或列表进行排序的方法。 Lets say I got following PointF
elements in my array: 可以说我在数组中跟随了PointF
元素:
What I would want to achieve is: 我想要实现的是:
So this sorted array would look something like this: 所以这个排序的数组看起来像这样:
Meaning, that at the end, if I were to draw these points using Graphics.DrawPolygon()
I would get a closed polygon(in this case a rectangle) with no lines crossing each other. 意思是,最后,如果我要使用Graphics.DrawPolygon()
绘制这些点,我会得到一个闭合的多边形(在这种情况下是一个矩形),没有相互交叉的线。
Thank you for your time 感谢您的时间
The algorithm you want is the Graham Scan. 您想要的算法是Graham Scan。 You can read about it here: 你可以在这里读到它:
http://en.wikipedia.org/wiki/Graham_scan http://en.wikipedia.org/wiki/Graham_scan
juharr's comment is correct; juharr的评论是正确的; you're not going to be able to do this with IComparable
because this is not a comparison sort problem . 你无法用IComparable
做到这一点,因为这不是一个比较排序问题 。 For a comparison sort to work, you need to be able to compare any two elements for relative size. 要使比较排序起作用,您需要能够比较任何两个元素的相对大小。
An easier-but-slower algorithm is the gift wrapping algorithm: 较容易但较慢的算法是礼物包装算法:
http://en.wikipedia.org/wiki/Gift_wrapping_algorithm http://en.wikipedia.org/wiki/Gift_wrapping_algorithm
FYI the shape you are looking for is called the convex hull . 仅供参考,您正在寻找的形状称为凸壳 。 That will help you when searching for algorithms. 这将在搜索算法时为您提供帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.