0

I am trying to use the contains method provided to do a detection of the coordinates. As researched, the codes I use seem to work fine for others. But in this case, I am unable to get a positive return from my boolean despite it being within the drawn Path2D polygon.

PolygonCreation : For loop to draw coordinates contains to detect the coordinates

Path2D prettyPoly =new Path2D.Double();
                //Shape drawn
                // for loop to retrieve x,y coordinates for each location 
                for (int i = 0; i < coord1.length; i++) {
                    // split to retrieve x and y 
                    String[] splitCoord = coord1[i].split(" ");
                    // 0 - long 1 - lat
                     y1 = Double.parseDouble(splitCoord[0]);
                     x1 = Double.parseDouble(splitCoord[1]);
                     // to plot the starting point
                     if(i == 0){
                     System.out.println("Move to x"+x1 + "y" +y1);
                         prettyPoly.moveTo(x1, y1);



                     }else{

                    //   continue from starting point
                    System.out.println("Line to x  "+x1 + "y  " +y1);
                         prettyPoly.lineTo(x1, y1);

                     }

                }

                 prettyPoly.closePath();

                // check if contain
                Boolean h = prettyPoly.contains(1.38201544874088, 103.95321635529909);
                if(h == true){
                    System.out.println("true"); 
                }else{
                    System.out.println("false");
                }

given this set of codes, my coordinates are the same as the starting coordinate of the first location and have been detected false.

Link to path 2d documentation :

https://docs.oracle.com/javase/7/docs/api/java/awt/geom/Path2D.html

https://docs.oracle.com/javase/7/docs/api/java/awt/geom/Path2D.Double.html

jera
  • 302
  • 4
  • 30
Cosq
  • 155
  • 1
  • 2
  • 12

1 Answers1

2

You can try to do the following, instead of

Boolean h = prettyPoly.contains(1.38201544874088, 103.95321635529909);

try to use

boolean h = prettyPoly.getBounds().contains(1.38201544874088, 103.95321635529909);

In my simple test with Path2D this worked as expected.

Tuchkata
  • 64
  • 3
  • hey man! thanks man you just saved me a ton of research since im new to this ! it really works! appreciate it alot ! – Cosq Dec 24 '15 at 03:13
  • hey man using this method works fine but for any coordinates i get a true it doesnt give me a false for any of it – Cosq Dec 24 '15 at 03:20
  • This code gets the integer value of the bound and thus any bound between 1-2 and 103 - 104 would be a true. Is there a way it would differentiate the points like are double. Example target coord is 1.1 and only its true for 1.1 and false for 1.2 ? Thanks in advance ! – Cosq Dec 24 '15 at 03:50