问题描述
什么是创建最简单的方法的AffineTransform
将映射从一个矩形坐标到另一个(浮点/双矩形给出)?
更新1
矩形可能是完全不同的。例如,[(0,0) - (1,1)]和[(150,-14) - ( - 1000,-14.1)。和改造应统一改造。例如,矩形角应转变一对一。例如坐标为(0,0)应该转向(150,-14)。
更新2
我需要的AffineTransform
对象,而不仅仅是计算。因为我想将它应用到图形
对象。此外,我想有简单的变换一些串联的形式。
更新3
下面所有的3测试失败:
公共类RectangleTransformTest { 私有静态最后的双三角翼= 0.001; 矩形R1 =新Rectangle2D.Double(-1,0,2,0.01);
矩形R2 =新Rectangle2D.Double(10,20,400,300); 的AffineTransform吨; SRC的Point2D;
DST1的Point2D,DST2; @测试
公共无效TEST1(){ T =新AffineTransform();
t.translate(+ r2.getMinX() - r1.getMinX(),+ r2.getMinY() - r1.getMinY());
t.scale(r2.getWidth()/ r1.getWidth(),r2.getHeight()/ r1.getHeight()); } @测试
公共无效TEST2(){ T =新AffineTransform(); t.scale(r2.getWidth()/ r1.getWidth(),r2.getHeight()/ r1.getHeight());
t.translate(+ r2.getMinX() - r1.getMinX(),+ r2.getMinY() - r1.getMinY()); } @测试
公共无效TEST3(){ T =新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()); } @后
公共无效comparePoints(){ SRC =新Point2D.Double(r1.getMinX(),r1.getMinY());
DST1 =新Point2D.Double();
t.transform(SRC,DST1);
DST2 =新Point2D.Double(r2.getMinX(),r2.getMinY()); 的assertEquals(dst2.getX(),dst1.getX(),三角洲);
的assertEquals(dst2.getY(),dst1.getY(),三角洲); SRC =新Point2D.Double(r1.getMinX(),r1.getMaxY());
DST1 =新Point2D.Double();
t.transform(SRC,DST1);
DST2 =新Point2D.Double(r2.getMinX(),r2.getMaxY()); 的assertEquals(dst2.getX(),dst1.getX(),三角洲);
的assertEquals(dst2.getY(),dst1.getY(),三角洲); SRC =新Point2D.Double(r1.getMaxX(),r1.getMinY());
DST1 =新Point2D.Double();
t.transform(SRC,DST1);
DST2 =新Point2D.Double(r2.getMaxX(),r2.getMinY()); 的assertEquals(dst2.getX(),dst1.getX(),三角洲);
的assertEquals(dst2.getY(),dst1.getY(),三角洲); SRC =新Point2D.Double(r1.getMaxX(),r1.getMaxY());
DST1 =新Point2D.Double();
t.transform(SRC,DST1);
DST2 =新Point2D.Double(r2.getMaxX(),r2.getMaxY()); 的assertEquals(dst2.getX(),dst1.getX(),三角洲);
的assertEquals(dst2.getY(),dst1.getY(),三角洲); }}
要从变换[(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
code:
T =新AffineTransform();
t.translate(r2.getMinX(),r2.getMinY());
t.scale(r2.getWidth()/ r1.getWidth(),r2.getHeight()/ r1.getHeight());
t.translate(-r1.getMinX(),-r1.getMinY());
What is the simplest way to create AffineTransform
which maps coordinates from one rectangle to another (float/double rectangles are given)?
UPDATE 1
Rectangles can be absolutely different. For example [(0,0)-(1,1)] and [(150,-14)-(-1000,-14.1)]. And transformation should transform uniformly. For example, rectangle corners should be transformed one to one. For example coordinates (0,0) should turn to (150,-14).
UPDATE 2
I need AffineTransform
object, not just calculation. Because I want to apply it to Graphics
object. Also I would like to have in the form of some concatenation of simple transforms.
UPDATE 3
All the following 3 tests are failed:
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);
}
}
To transform from [(a,b)-(c,d)] to [(e,f)-(g,h)] you can perform the following computation:
x' = e + (x - a) * (g - e) / (c - a);
y' = f + (y - b) * (h - f) / (d - b);
Here is the corresponding AffineTransform
code:
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());
这篇关于创建转变,从一个矩形到另一个地图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!