创建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);
}
}
答案 0 :(得分:13)
要从[(a,b) - (c,d)]转换为[(e,f) - (g,h)],您可以执行以下计算:
x' = e + (x - a) * (g - e) / (c - a);
y' = f + (y - b) * (h - f) / (d - b);
以下是相应的AffineTransform
代码,其中r1
正在转换为r2
:
t = new AffineTransform();
t.translate(r2.getMinX(), r2.getMinY());
t.scale(r2.getWidth()/r1.getWidth(), r2.getHeight()/r1.getHeight());
t.translate(-r1.getMinX(), -r1.getMinY());