I have an XML - list of xy-coordinates (vertices) that define the edge of a polygon. I read this file and save the vertices in an ArrayList. Now I would like to iterate over the finished ArrayList and compare two vertices with each other to decide whether the edge connecting both vertices is a north, west, south, or east edge of the simple polygon.
This is the code I can use to test whether the edge that makes up two points is a north, west, east, or south edge.
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");
}
}
I have two concerns for which I don't find a solution:
First I would like to test whether the vertices are sorted clockwise or counterclockwise. Accordingly, I would have to change the code for the edge types.
Second I don't know how I can iterate over the ArrayList of vertices in order to compare two of the vertices at each step. For example in the first step v1 with v2, in the second v2 with v3, in the third v3 with v4 and so on.. Can I perhaps address the vertices in the ArrayList with their indices?