C-lara
C-lara

Reputation: 115

How to use the interior area?

I have a question regarding the edges of an orthogonal polygon. My goal is to decide whether an edge is a top edge (this means the interior of the polygon lies below), a bottom edge (this means the interior of the polygon lies above), a right edge (interior of the polygon is left) or a left edge (interior of the polygon is right).

I have the coordinates of the polygon and sorted them counterclockwise (as this is the usual recommended way).

I wrote some code to calculate the interior of the polygon in Java.

[...]

   public static double polygonArea(double X[], double Y[], int n)
   {
     // Initialze area double area = 0.0; `
    // Calculate value of shoelace formula 
    int j = n - 1; 
    for (int i = 0; i < n; i++) 
    { 
        area += (X[j] + X[i]) * (Y[j] - Y[i]); 

        // j is previous vertex to i 
        j = i;  
    } 

    // Return absolute value 
    return Math.abs(area / 2.0); 
} 

... what is working well. However I have absolutely no clue how I can use this to calculate or decide whether an edge (e.g. a vertical edge defined by its top point and bottom point) is a left edge (interior is right) or a right edge (interior left).

Maybe there is also a way more easier solution. However, I would appreciate if someone could give me some advice to approach my problem.

Upvotes: 0

Views: 53

Answers (1)

RaffleBuffle
RaffleBuffle

Reputation: 5455

If I've understood your question correctly (a diagram would help) you can get the position (TOP, BOTTOM, LEFT, RIGHT) of each edge relative to a simple (no self-intersections), orthogonal polygon by comparing the ordering of the x (for horizontal) or y (for vertical) coordinates.

Obviously the ordering of the points matters, so for a counter-clockwise polygon you could use something like this:

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");
    }
}

Upvotes: 1

Related Questions