2

So I'm using coordinates to create a Polygon using path2d. I need to find out if a point is exactly on the line of the polygon I created.

Here's my code right now, which allows me to find out whether specific coordinates are within the polygon, but it returns false if the point is exactly on the line.

import java.awt.geom.Path2D;

public class Path2dMain {
public static void main(String[] args) {
    Path2D.Double d = new Path2D.Double();
    d.moveTo(40.6522526, -105.1398466);
    d.lineTo(40.627764, -105.16559600000001);
    d.lineTo(40.6232377, -105.13491200000001);
    d.lineTo(40.6489967, -105.1317787);
    d.lineTo(40.6522526, -105.1398466);
    d.closePath();
    System.out.println(d.contains(40.63, -105.1417787));
  }
}

If anyone can see a solution to this that would be great, also it would be ok if it just retruned true if the point was on the line. Thanks in advance.

dochsner
  • 249
  • 2
  • 8
  • 25
  • Be careful: "exactly" and floating-point arithmetic don't mix well. – Mike Harris May 16 '16 at 14:50
  • Take a look at this question and its answers: http://stackoverflow.com/questions/217578/how-can-i-determine-whether-a-2d-point-is-within-a-polygon?rq=1 and this article https://www.ecse.rpi.edu/Homepages/wrf/Research/Short_Notes/pnpoly.html – jira May 16 '16 at 15:03
  • From the documentation for `.contains()`: Note that the definition of insideness can lead to situations where points on the defining outline of the shape may not be considered contained in the returned bounds object, but only in cases where those points are also not considered contained in the original shape. – Jake H May 16 '16 at 15:44

0 Answers0