Menu

problems with SimplePolygon2D contains()

Bryan
2011-10-10
2012-12-12
  • Bryan

    Bryan - 2011-10-10

    I am using the contains() method with SimplyPolygon2D objects, and am getting inconsistent results.

    Here's an example:

          SimplePolygon2D p = new SimplePolygon2D();
          p.addVertex(new Point2D(161.297,104.032));
          p.addVertex(new Point2D(121.514,105.052));
          p.addVertex(new Point2D(123.676,144.829));
          p.addVertex(new Point2D(163.459,140.749));
          boolean z = p.contains(136.0,124.0);

    When I run this code I get z = false, which seems clearly wrong.

    Any suggestions? Has anyone else had similar issues? Thank you.

     
  • David

    David - 2011-10-10

    Hi,

    this is because your polygon is oriented Clockwise (CW). The convention in the library is to consider point within polygon by taking into account polygon orientation. This allows working with polygon with holes.

    You can solve your problem by several ways:
    * check both contains and getSignedArea method. The point is isinside the enclosing area if the condition is met:
    boolean z = p.contains(136.0,124.0) ^ p.getSignedArea()<0;
    * use the complement of the polygon (method p.complement())

    regards,

    David

     
  • Bryan

    Bryan - 2011-10-16

    Thanks David!

    The problem is solved when I make sure the polygon is oriented counter-clockwise:

    if (p.getSignedArea()<0)
       p = p.complement();

     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.