简体   繁体   English

排序PointF数组

[英]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元素:

  • [0] { X = 50.0 Y = 0.0} [0] { X = 50.0 Y = 0.0}
  • [1] { X = 100.0 Y = 100.0} [1] { X = 100.0 Y = 100.0}
  • [2] { X = 0.0 Y = 100.0} [2] { X = 0.0 Y = 100.0}
  • [3] { X = 100.0 Y = 0.0} [3] { X = 100.0 Y = 0.0}
  • [4] { X = 0.0 Y = 0.0} [4] { X = 0.0 Y = 0.0}
  • [5] { X = 100.0 Y = 50.0} [5] { X = 100.0 Y = 50.0}
  • [6] { X = 50.0 Y = 100.0} [6] { X = 50.0 Y = 100.0}

What I would want to achieve is: 我想要实现的是:

  • first are elements with lowest Y and X 首先是YX最低的元素
  • still elements with lowest Y but X would get bigger 仍然具有最低YX会变大的元素
  • then as the highest possible X at this lowest Y get achieved it would go, all elements with this X , but with bigger and bigger Y 然后作为在这个最低Y得到的最高可能X得到它,所有元素都有这个X ,但是Y越来越大
  • as the top Y and top X get achieved, it would go from elements with top X and top Y , to elements still with top Y , but with lower and lower X 随着获得最高的Y和最高的X ,它将从具有最高的X和最高的Y的元素变为仍然具有最高的YX越来越低的元素

So this sorted array would look something like this: 所以这个排序的数组看起来像这样:

  • [4] { X = 0.0 Y = 0.0} [4] { X = 0.0 Y = 0.0}
  • [0] { X = 50.0 Y = 0.0} [0] { X = 50.0 Y = 0.0}
  • [3] { X = 100.0 Y = 0.0} [3] { X = 100.0 Y = 0.0}
  • [5] { X = 100.0 Y = 50.0} [5] { X = 100.0 Y = 50.0}
  • [1] { X = 100.0 Y = 100.0} [1] { X = 100.0 Y = 100.0}
  • [6] { X = 50.0 Y = 100.0} [6] { X = 50.0 Y = 100.0}
  • [2] { X = 0.0 Y = 100.0} [2] { X = 0.0 Y = 100.0}

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM