Fastest way to sort vectors by angle without actually computing that angle

后端 未结 7 1348
长情又很酷
长情又很酷 2020-12-09 04:19

Many algorithms (e.g. Graham scan) require points or vectors to be sorted by their angle (perhaps as seen from some other point, i.e. using difference vectors). This order i

7条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-09 04:41

    The simpliest thing I came up with is making normalized copies of the points and splitting the circle around them in half along the x or y axis. Then use the opposite axis as a linear value between the beginning and end of the top or bottom buffer (one buffer will need to be in reverse linear order when putting it in.) Then you can read the first then second buffer linearly and it will be clockwise, or second and first in reverse for counter clockwise.

    That might not be a good explanation so I put some code up on GitHub that uses this method to sort points with an epsilion value to size the arrays.

    https://github.com/Phobos001/SpatialSort2D

    This might not be good for your use case because it's built for performance in graphics effects rendering, but it's fast and simple (O(N) Complexity). If your working with really small changes in points or very large (hundreds of thousands) data sets then this won't work because the memory usage might outweigh the performance benefits.

提交回复
热议问题