0

I am trying to see if a point is contained within a polygon using Path2D.

The last line, System.out.println(poly.contains(lat1, lon1)), prints "false" even though I know the coordinates(lat1, lon1) are within the polygon sapecified in "testBound". Is the ".contain()" not working? Am i missing something?

    package poly;

import java.awt.Polygon;
import java.awt.geom.Path2D;
import java.util.ArrayList;
import java.util.Arrays;

public class Polygon3 {
public static final double lat1 = 40.1032946;
public static final double lon1 = -84.5110052;
public static final String testBound = "40.203294,-84.521005;40.203294,-84.501005;40.003294,-84.521005;40.003294,-84.501005";




public static void main(String[] args) {
    String[] test = testBound.split(";");
    ArrayList<Double> latList = new ArrayList<Double>();
    ArrayList<Double> lonList = new ArrayList<Double>();
    for(String t : test) {
        String[] latlng = t.split(",");
        latList.add(Double.parseDouble(latlng[0]));
        lonList.add(Double.parseDouble(latlng[1]));
    }
    System.out.println(latList);
    System.out.println(lonList);

    Double latpoints[] = latList.toArray(new Double[latList.size()]);
    Double lonpoints[] = lonList.toArray(new Double[lonList.size()]);
    System.out.println(latpoints);
    System.out.println(lonpoints);
    Path2D poly = new Path2D.Double();
    for(int i = 0; i < latpoints.length; i++) {
        poly.moveTo(latpoints[i], lonpoints[i]);
    }
    poly.closePath();
    String testing = poly.toString();
    System.out.println(testing);

    System.out.println(poly.contains(lat1, lon1));
}


}
GBlodgett
  • 12,704
  • 4
  • 31
  • 45
javanewbie14
  • 13
  • 1
  • 5

2 Answers2

3

How to create a polygon?

To create a closed polygon you need to:

  1. use moveTo to move to the first point
  2. use lineTo to connect all the other points
  3. use closePath to connect the last point with the first one

For example:

for (int i = 0; i < latpoints.length; i++) {
    if (i == 0) {
        poly.moveTo(latpoints[i], lonpoints[i]);
    }
    else {
        poly.lineTo(latpoints[i], lonpoints[i]);
    }
}
poly.closePath();

Why contains returns false?

contains returns false because, due to the points order, you are not creating a square but an hourglass shape:

3   1
| X |
4   2

Since the point you are testing is in the center, it might not be considered inside the shape.

If you swap the order of the third and forth point you are then creating a square:

4 - 1
|   |
3 - 2

and the point will be considered inside.

Loris Securo
  • 7,538
  • 2
  • 17
  • 28
  • Thanks for this answer. The answer below worked for my test case, but if I have issues with more advanced polygons, this will help. Appreciate the time you spent to answer – javanewbie14 Apr 18 '18 at 23:04
  • @javanewbie14 I'm sorry but my answer created triangle, I don't know how I was thinking yesterday, but this one is correct. – Kermi Apr 19 '18 at 07:07
0

First of all, you do not create polygon correctly. moveTo moves every time first point of this polygon to different locations, but does not draw lines. You need to do something like this to create polygon:

    poly.moveTo(latpoints[0], lonpoints[0]);
    for(int i = 1; i < latpoints.length - 1; i++) {
        poly.lineTo(latpoints[i], lonpoints[i]);
    }

Please notice, that we draw 2 lines from beginning, by closePath() we are able to finish this shape as closePath() draws straight line from last point to the first one.

Kermi
  • 234
  • 1
  • 7
  • I'm sorry but this answer is not correct, I don't know what was wrong with me yesterday but here we create traingle. Please look at @Loris Securo answer which is correct. – Kermi Apr 19 '18 at 07:04