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

Rectangle.outcode() result for a point 1 unit right or below is incorrect

XMLWordPrintable

    • 2d
    • generic

      FULL PRODUCT VERSION :
      java version "1.7.0_40"
      Java(TM) SE Runtime Environment (build 1.7.0_40-b43)
      Java HotSpot(TM) 64-Bit Server VM (build 24.0-b56, mixed mode)

      ADDITIONAL OS VERSION INFORMATION :
      Windows 7 Ultimate 64 bit SP 1

      A DESCRIPTION OF THE PROBLEM :
      Take this code:

      new Rectangle(0,0,20,20).outcode(new Point(20, 20));

      The result is 0 which is invalid to my opinion. It seems 0 should be returned if the point is inside the rectangle but it is not since the lower right Point of this rectangle has 19 for it's x and y value. Rectangle.contains(point) confirms this.

      The bug is caused by these two lines of code:
      Rectangle line 1145: } else if (x > this.x + (double) this.width) {
      Rectangle line 1152: } else if (y > this.y + (double) this.height) {

      In both lines the > operator should be a >= to solve this problem. This same code is also used in the Rectangle2D.Double and Rectangle2D.Float subclasses of Rectangle2D. And After checking it's source code it seems JDK 8 has this problem too.

      REGRESSION. Last worked in version 7u40

      REGRESSION : Additional Information
      java version "1.7.0_40"
      Java(TM) SE Runtime Environment (build 1.7.0_40-b43)
      Java HotSpot(TM) 64-Bit Server VM (build 24.0-b56, mixed mode)

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Execute this code in a main and check the variable output.
      int outcode = new Rectangle(0,0,20,20).outcode(new Point(20, 20));
      boolean inside = new Rectangle(0,0,20,20).contains(new Point(20,20));


      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      outcode = 12
      The result of Rectangle2D.OUT_BOTTOM | Rectangle2D.OUT_RIGHT

      Variable 'inside' is false
      Since the point is not inside the rectangle
      ACTUAL -
      outcode = 0
      This means there was no bitmask applicable and the point should be inside the rectangle.

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      public void testCase() {
         int outcode = new Rectangle(0,0,20,20).outcode(new Point(20, 20));
         boolean inside = new Rectangle(0,0,20,20).contains(new Point(20,20));
         System.out.println(String.format("Outcode is %d and inside is %s"
            , outcode, inside);
      }
      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      In my case I can decrease the width and height of the rectangle by 1 or move the point 1 position in both x and y direction to solve the problem.

      But I am in the process of cleaning up some of the code and will probably let my custom classes extend Rectangle and Point. This will force me to create a new temporary rectangle or point and do the same trick in the code that gives the problem. Result is a small piece of workaround code. I can live with that.

            aghaisas Ajit Ghaisas
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            6 Start watching this issue

              Created:
              Updated: