Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-4427852

Polygon.contains() doesn't work after changing xpoints[] or ypoints[]

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Duplicate
    • Icon: P4 P4
    • None
    • 1.3.0
    • client-libs
    • 2d
    • generic
    • generic



      Name: boT120536 Date: 03/20/2001


      java version "1.2.2"
      Solaris VM (build Solaris_JDK_1.2.2_05a, native threads, sunwjit)

      /*
      There appears to be a bug in the way that Polygon.contains() works. This
      bug only surfaces if you manually change (by poking around in the
      xpoints/ypoints arrays) the vertices of the polygon.

      This program prints:

           true
           true
           false

      thereby illustrating that contains does not work properly if the
      array data in the polygon is changed manually!

      */

      import java.awt.*;

      public class Translate {

          public static void main(String[] args) {
              int dist = 2;
              Point center = new Point(10, 20);

              // make a triangle centered at center of height 2 * dist
              Polygon triangle = new Polygon();
              triangle.addPoint(center.x - dist, center.y - dist);
              triangle.addPoint(center.x + dist, center.y - dist);
              triangle.addPoint(center.x, center.y + dist);

              // test contains
              System.out.println(triangle.contains(center));

              // translate the triangle and the center and test contains again
              triangle.translate(dist, dist);
              center.translate(dist, dist);
              System.out.println(triangle.contains(center));

              // same thing, only this time do the translation manually
              for (int i = 0; i < triangle.npoints; i++) {
                  triangle.xpoints[i] += dist;
                  triangle.ypoints[i] += dist;
              }
              center.translate(dist, dist);
              System.out.println(triangle.contains(center));
          }

      }
      (Review ID: 119031)
      ======================================================================

            flar Jim Graham
            bonealsunw Bret O'neal (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: