Results 1 to 6 of 6
  1. #1
    gib65 is offline Member
    Join Date
    Jun 2010
    Posts
    86
    Rep Power
    0

    Default scale transform giving inaccurate results

    Please have a look at my code snippet below. I'm trying to do a scale transform on an Area object (called boundingArea) and I'm not getting the results I'm expecting.

    Java Code:
    System.out.println("initial width: " + getBounds().getWidth());
    System.out.println("1/width = " + (1.0d / getBounds().getWidth()));
    System.out.println("width * (1 + 1/width) = " + (getBounds().getWidth() * (1.0d + 1.0d / getBounds().getWidth())));
    boundingArea.transform(AffineTransform.getScaleInstance(
    		1.0d + (1.0d / getBounds().getWidth()), 1.0d));
    System.out.println("final width: " + getBounds().getWidth());
    When I run this, I get the following output:

    initial width: 18.0
    1/width = 0.055555555555555555
    width * (1 + 1/width) = 19.0
    final width: 20.0

    Why does the transform operation give me the wrong value? I'm assume that what the transform operation is doing is taking 1 + 1/width and multiplying the width by that. But according to my calculations above, it should end up with 19, but it gives me 20 instead.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,189
    Rep Power
    19

    Default

    You're calling getBounds() on the current instance of whatever class that code is in, and applying a transform to a variable boundingArea.

    I second the recommendation to post a SSCCE.

    db

  4. #4
    gib65 is offline Member
    Join Date
    Jun 2010
    Posts
    86
    Rep Power
    0

    Default

    Here's a very simple SSCCE:

    Java Code:
    import java.awt.Rectangle;
    import java.awt.geom.Area;
    import java.awt.geom.AffineTransform;
    
    public class TransformTest {
    
        private Area boundingArea;
    
        public TransformTest() {
    	boundingArea = new Area(new Rectangle(1, 0, 18, 18));
        }
    
        public void doTest() {
    
    	System.out.println("initial width: " + getBounds().getWidth());
    	System.out.println("1/width = " + (1.0d / getBounds().getWidth()));
    	System.out.println("width * (1 + 1/width) = " + 
    		(getBounds().getWidth() * (1.0d + 1.0d / getBounds().getWidth())));
    	boundingArea.transform(AffineTransform.getScaleInstance(
    		1.0d + (1.0d / getBounds().getWidth()), 1.0d));
    	System.out.println("final width: " + getBounds().getWidth());
        }
    
        private Rectangle getBounds() {
    	return boundingArea.getBounds();
        }
    
        public static void main(String[] args) {
    
    	TransformTest tranTest = new TransformTest();
    	tranTest.doTest();
        }
    }
    Interestingly, I found that the problem disappears if I initialize boundingArea with an X value of 0. Any other value, it seems, gives me a result of 20 rather than the 19 one would expect. This includes values from 1 to 1000 and -1 to -1000.

    But should I have to reposition my boundingArea to x = 0 (or y = 0, or both) just to do a scale?

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,189
    Rep Power
    19

    Default

    It's a result of integer math. Set the left to 1800 and the width is 19, not 20. Better use 2D geometry.
    Java Code:
    import java.awt.geom.*;
    
    public class ScaleTransformTest {
    
      public static void main(String[] args) {
        for (int x = 0; x < 1000; x++) {
          Area area = new Area(new Rectangle2D.Double(x, 0, 18, 18));
          area.transform(AffineTransform.getScaleInstance(
                  1.0d + (1.0d / area.getBounds2D().getWidth()), 1.0d));
          Rectangle2D bounds = area.getBounds2D();
          if (bounds.getWidth() != 19.0) {
            System.out.println("x = " + x + "   width = " + bounds.getWidth());
          }
        }
      }
    }
    db

  6. #6
    gib65 is offline Member
    Join Date
    Jun 2010
    Posts
    86
    Rep Power
    0

    Default

    Yep, that worked. I guess I'm going to make use of the 2D classes from now on (at least, when it's a 2D application). Thanks for pointing me in the right direction.

Similar Threads

  1. how to scale correctly ?
    By h9h in forum Java 2D
    Replies: 10
    Last Post: 10-29-2009, 07:06 AM
  2. The scale() method
    By IamKira in forum Java 2D
    Replies: 0
    Last Post: 07-15-2009, 03:49 PM
  3. How to use Scale in SWT
    By Java Tip in forum SWT
    Replies: 0
    Last Post: 07-07-2008, 04:49 PM
  4. Transform Scale
    By Java Tip in forum java.awt
    Replies: 0
    Last Post: 06-21-2008, 08:53 PM
  5. Scale 2 or more pictures using a JSlider
    By Panchitopro in forum AWT / Swing
    Replies: 4
    Last Post: 05-20-2008, 04:44 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •