Create transform to map from one rectangle to another?

前端 未结 1 1207
没有蜡笔的小新
没有蜡笔的小新 2020-12-31 19:29

What is the simplest way to create AffineTransform which maps coordinates from one rectangle to another (float/double rectangles are given)?

UPD

1条回答
  •  别那么骄傲
    2020-12-31 20:18

    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, where r1 is being transformed to 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());
    

    0 讨论(0)
提交回复
热议问题