Results 1 to 6 of 6
- 10-25-2010, 04:36 AM #1
Member
- Join Date
- Jun 2010
- Posts
- 86
- Rep Power
- 0
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.
When I run this, I get the following output: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());
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.
-
- 10-25-2010, 08:28 AM #3
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
- 10-25-2010, 04:46 PM #4
Member
- Join Date
- Jun 2010
- Posts
- 86
- Rep Power
- 0
Here's a very simple SSCCE:
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.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(); } }
But should I have to reposition my boundingArea to x = 0 (or y = 0, or both) just to do a scale?
- 10-25-2010, 06:45 PM #5
It's a result of integer math. Set the left to 1800 and the width is 19, not 20. Better use 2D geometry.
dbJava 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()); } } } }
- 10-25-2010, 11:55 PM #6
Member
- Join Date
- Jun 2010
- Posts
- 86
- Rep Power
- 0
Similar Threads
-
how to scale correctly ?
By h9h in forum Java 2DReplies: 10Last Post: 10-29-2009, 07:06 AM -
The scale() method
By IamKira in forum Java 2DReplies: 0Last Post: 07-15-2009, 03:49 PM -
How to use Scale in SWT
By Java Tip in forum SWTReplies: 0Last Post: 07-07-2008, 04:49 PM -
Transform Scale
By Java Tip in forum java.awtReplies: 0Last Post: 06-21-2008, 08:53 PM -
Scale 2 or more pictures using a JSlider
By Panchitopro in forum AWT / SwingReplies: 4Last Post: 05-20-2008, 04:44 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks