With
Obviously the type for CW polygons is reversed.
enum EdgeType {TOP, BOTTOM, LEFT, RIGHT, EMPTY}
public EdgeType orthoEdgeTypeCCW(double x0, double y0, double x1, double y1)
{
if(x0 == x1) // vertical
{
return (y0 < y1) ? EdgeType.RIGHT :
(y0 > y1) ? EdgeType.LEFT :
EdgeType.EMPTY;
}
else if(y0 == y1) // horizontal
{
return (x0 < x1) ? EdgeType.BOTTOM :
(x0 > x1) ? EdgeType.TOP :
EdgeType.EMPTY;
}
else
{
throw new IllegalArgumentException("Edge not orthogonal");
}
}
Regarding your first question, there are a number of ways to find the orientation of a polygon. See the discussion on SO
here.As for comparing the points in a polygon, you can do something like the below:
This will compare each point with the next, including comparing the last point with the first to 'close the loop'. If this isn't needed, you can do a small change to stop as soon as the last point is reached:这是我可以用来测试组成两个点的边缘是北边、西边、东边还是南边的代码。
enum Orientation {CW, CCW}
public Orientation orientation(List<Point2D> points)
{
int minIdx = 0;
for(int i=1; i<points.size(); i++)
if(pointOrder(points.get(i), points.get(minIdx)) <= 0) minIdx = i;
int nextIdx = (minIdx+1) % points.size();
if(points.get(nextIdx).getY() == points.get(minIdx).getY())
return Orientation.CCW;
else
return Orientation.CW;
}
public int pointOrder(Point2D p1, Point2D p2)
{
if(p1.getY() < p2.getY()) return -1;
else if(p1.getY() > p2.getY()) return 1;
else if(p1.getX() < p2.getX()) return -1;
else if(p1.getX() > p2.getX()) return 1;
else return 0;
}
我有两个顾虑,没有找到解决方案。
for(int i=0, j=points.size()-1; i<points.size(); j=i++)
{
EdgeType edgeType = orthoEdgeTypeCCW(points.get(j), points.get(i));
System.out.format("%s -> %s : %s%n", points.get(j), points.get(i), edgeType);
}
第一...
public EdgeType orthoEdgeTypeCCW(Point2D p1, Point2D p2)
{
if(p1.getX() == p2.getX()) // vertical
{
return (p1.getY() < p2.getY()) ? EdgeType.RIGHT :
(p1.getY() > p2.getY()) ? EdgeType.LEFT :
EdgeType.EMPTY;
}
else if(p1.getY() == p2.getY()) // horizontal
{
return (p1.getX() < p2.getX()) ? EdgeType.BOTTOM :
(p1.getX() > p2.getX()) ? EdgeType.TOP :
EdgeType.EMPTY;
}
else
{
throw new IllegalArgumentException("Edge not orthogonal");
}
}
我想测试顶点是顺时针还是逆时针排序。因此,我必须修改边缘类型的代码。
第二种我有一个XML--定义多边形边缘的xy坐标(顶点)列表。我读取这个文件并将顶点保存在ArrayList中。现在,我想在完成的ArrayList上迭代,并 ...
List<Point2D.Float> points = new ArrayList<>(); //your initial set of points
for (int i = 0; i < points.size(); i++) {
Point2D.Float current = points.get(i);
Point2D.Float next = points.get((i + 1) % points.size());
//do your comparison between the two points here
}
对于一个没有自交点的简单正交多边形,你可以确定它的方向(CW)。