Search code examples
javaandroidpolygons

Point Inside A Polygon Is Returning That It Is Outside The Polygon


I am trying to determine if a phone is located in this polygon using longitude and latitude.

enter image description here

It worked on my first test point. My second test point (the green dot) returns that it is outside of the polygon. Since Android cannot use the Polygon class, I created my own using the code I found from a few different sites:

public class Polygon {

    private double[] polyY, polyX;
    private int polySides;

    public Polygon( double[] px, double[] py, int ps ) {
        polyX = px;
        polyY = py;
        polySides = ps;
    }
    public boolean contains( double x, double y ) {
        boolean oddTransitions = false;
        for( int i = 0, j = polySides -1; i < polySides; j = i++ ) {
            if( ( polyY[ i ] < y && polyY[ j ] >= y ) || ( polyY[ j ] < y && polyY[ i ] >= y ) ) {
                if( polyX[ i ] + ( y - polyY[ i ] ) / ( polyY[ j ] - polyY[ i ] ) * ( polyX[ j ] - polyX[ i ] ) < x ) {
                    oddTransitions = !oddTransitions;          
                }
            }
        }
        return oddTransitions;
    }
}

I query the database to get the longitudes and latitudes from this table:

db.execSQL("insert into " + DATABASE_TABLE_POLYGON  + " select null as " + KEY_ROWID
                + ", 0 as " + KEY_BUILDING
                + ", -95.929635 as " + KEY_LONG
                + ", 41.223459 as " + KEY_LAT  
                + " union select null,0,-95.931544, 41.223491"
                + " union select null,0,-95.931587, 41.224734"
                + " union select null,0,-95.929484, 41.224782"
                + " union select null,0,-95.929227, 41.227655"
                + " union select null,0,-95.929184, 41.229656"
                + " union select null,0,-95.926437, 41.23006"
                + " union select null,0,-95.92633, 41.228478"
                + " union select null,0,-95.924141, 41.227413"
                + " union select null,0,-95.920944, 41.22617"
                + " union select null,0,-95.921073, 41.225299"
                + " union select null,0,-95.922811, 41.224008"
                + " union select null,0,-95.92706, 41.22099"
                + " union select null,0,-95.927553, 41.221022"
                + " union select null,0,-95.927811, 41.222943"
                + " union select null,0,-95.929334, 41.222991";

Here is the code that is failing:

Polygon p = new Polygon(xArray,yArray,numPoints);
if(p.contains(-95.927714, 41.225281)){
    textView.setText("in polygon");
}else{
    textView.setText("outside polygon");
}

Output: outside polygon

Other info:
It seems to be working in another app I created.
I am using an emulator and mock locations.
I also created a normal java class so I could use the built in Polygon class, but that
also says that it is outside the polygon.
I used an online polygon grapher just to be totally sure the point was inside the polygon and it was.
The points go in a clockwise direction.

Why is this point returning "outside polygon"?


Solution

  • I figured out the problem. I finally noticed that all the points were sorted by longitude. So either the query (shown above) where I insert the data sorted it, or the query where I select longitude and latitude. The points must be in the correct order or the contains() method will not work. My solution will be to add another column to the table so I can number each point. Then whenever I query for the points I will sort by this new column so they are always in the correct order.