C - 凸多边形 - 排序点(顺时针)
我有一个用点表示的凸多边形。点由x 坐标数组和y 坐标数组表示。
例如:
X = {6, 1, 5, 0, 3}
Y = {4, 0, 0, 4, 6}
我如何按顺时针排序这些点?点数并不总是相同,但多边形仍然是凸的。
是否有不使用 atan2 或 math.h 中的其他函数的解决方案?
I have a convex polygon expressed by points. Points are expressed by array of x-coordinates and array of y-coordinates.
For example:
X = {6, 1, 5, 0, 3}
Y = {4, 0, 0, 4, 6}
How can I sort this points by clockwise? Number of points is not always the same, but polygon is still convex.
Is there a solution without using atan2 or other function from math.h?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为您可以通过将它们转换为极坐标来摆脱困境。 C 有
atan2
所以你可以逃脱:获得各自的角度后,你可以使用它们对点进行排序。
I think you can get away by converting them to polar coordinates. C has
atan2
so you could get away with:After you obtain the respective angles you can use them to sort the points.
我建议您按极角对它们进行排序,但最好以凸多边形内部的一个点作为原点来执行此操作。要获得这样的点,您可以使用 poligon 的任何对角线的中点,例如 ( (x[0] + x[2])/2, (y[0]+y[2])/2 )。
I'd recommend you to sort them by polar angle, but it's better to have a point inside of convex polygon as an origin to do that. to get such a point, you can just use a middle point of any diagonal of your poligon, for example ( (x[0] + x[2])/2, (y[0]+y[2])/2 ).