Maintain a list of points around a center point, preserving CCW order

和自甴很熟 提交于 2019-12-24 20:18:53

问题


I have the following class:

 public class Vertex() {
    private double xCoord;
    private double yCoord;
    private ArrayList<Vertex> neighborList();
 }

And I want to support adding/removing vertices to the neighborList such that the points are listed in CCW order around this vertex (which point is first in the list doesn't matter). If points are collinear, nearer points to this should be first. I've tried several methods but so far have always been able to find a counter example that doesn't work for the given method.

Does anyone have a good idea of how to do this in a simple and efficient manner?


回答1:


Express the point coordinates in the polar form

t = atan2(Y-Yo, X-Xo)
r = sqrt((X-Xo)^2 + (Y-Yo)^2)

and use lexicographical ordering on angle then radius.



来源:https://stackoverflow.com/questions/9336682/maintain-a-list-of-points-around-a-center-point-preserving-ccw-order

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!