创建AffineTransform
将坐标从一个矩形映射到另一个(给出浮动/双矩形)的最简单方法是什么?
更新 1
矩形可以完全不同。例如 [(0,0)-(1,1)] 和 [(150,-14)-(-1000,-14.1)]。并且变换应该是均匀变换的。例如,矩形角应该一对一变换。例如坐标 (0,0) 应变为 (150,-14)。
更新 2
我需要AffineTransform
对象,而不仅仅是计算。因为我想将它应用于Graphics
对象。我也想以一些简单转换的串联形式。
更新 3
以下 3 个测试全部失败:
public class RectangleTransformTest {
private static final double delta = 0.001;
Rectangle2D r1 = new Rectangle2D.Double(-1, 0, 2, 0.01);
Rectangle2D r2 = new Rectangle2D.Double(10, 20, 400, 300);
AffineTransform t;
Point2D src;
Point2D dst1, dst2;
@Test
public void test1() {
t = new AffineTransform();
t.translate(+r2.getMinX()-r1.getMinX(), +r2.getMinY()-r1.getMinY());
t.scale(r2.getWidth()/r1.getWidth(), r2.getHeight()/r1.getHeight());
}
@Test
public void test2() {
t = new AffineTransform();
t.scale(r2.getWidth()/r1.getWidth(), r2.getHeight()/r1.getHeight());
t.translate(+r2.getMinX()-r1.getMinX(), +r2.getMinY()-r1.getMinY());
}
@Test
public void test3() {
t = new AffineTransform();
t.scale((r2.getMaxX()-r2.getMinX())/(r1.getMaxX()-r1.getMinX()), (r2.getMaxY()-r2.getMinY())/(r1.getMaxY()-r1.getMinY()));
t.translate(+r2.getMinX()-r1.getMinX(), +r2.getMinY()-r1.getMinY());
}
@After
public void comparePoints() {
src = new Point2D.Double(r1.getMinX(), r1.getMinY());
dst1 = new Point2D.Double();
t.transform(src, dst1);
dst2 = new Point2D.Double(r2.getMinX(), r2.getMinY());
assertEquals(dst2.getX(), dst1.getX(), delta);
assertEquals(dst2.getY(), dst1.getY(), delta);
src = new Point2D.Double(r1.getMinX(), r1.getMaxY());
dst1 = new Point2D.Double();
t.transform(src, dst1);
dst2 = new Point2D.Double(r2.getMinX(), r2.getMaxY());
assertEquals(dst2.getX(), dst1.getX(), delta);
assertEquals(dst2.getY(), dst1.getY(), delta);
src = new Point2D.Double(r1.getMaxX(), r1.getMinY());
dst1 = new Point2D.Double();
t.transform(src, dst1);
dst2 = new Point2D.Double(r2.getMaxX(), r2.getMinY());
assertEquals(dst2.getX(), dst1.getX(), delta);
assertEquals(dst2.getY(), dst1.getY(), delta);
src = new Point2D.Double(r1.getMaxX(), r1.getMaxY());
dst1 = new Point2D.Double();
t.transform(src, dst1);
dst2 = new Point2D.Double(r2.getMaxX(), r2.getMaxY());
assertEquals(dst2.getX(), dst1.getX(), delta);
assertEquals(dst2.getY(), dst1.getY(), delta);
}
}