私のアプリの要素に適用する変換の正しい順序を理解するのに問題があります。アプリケーションには親子関係を持つ要素があり、各要素には変形があります(各要素はローカル空間に描画され、変形されます)。java Affine正しい順序を変換する
私がアーカイブしたいのは、私が親を変換すると、その子もすべて変形する必要があるということです(親を回転させると、子は親の周りを回転する必要があります)。
以下のコードはそれだけですが、問題は親を回転させて子を移動したい場合です。それは間違った方向に移動します(親変換のため)。私は変換のシーケンスを変更しようとしましたが、運はありません(私は最初に変換して変換する必要がありますが、子は親の代わりに独自の軸を中心に回転します)。
コード:
Element e;
AffineTransform t = new AffineTransform();
AffineTransform t3 = new AffineTransform();
for (i = 0; i < el.size(); i++)
{
e = el.get(i);
t3.setToIdentity();
t.setToIdentity();
tmp = e.getParent();
while(tmp != null)
{
t.translate(tmp.getX(), tmp.getY());
t3.rotate(Math.toRadians(tmp.getAngle()),tmp.getAnchorX(), tmp.getAnchorY());
tmp = tmp.getParent();
}
t.concatenate(t3);
t.translate(e.getX(), e.getY());
t.rotate(Math.toRadians(e.getAngle()),e.getAnchorX(), e.getAnchorY());
e.draw(g2d,t);
}
問題: - 二つの要素(他の1子) を与えられた - 親が(40度)回転 ある - 子供は10pxの どのようにすることができますによって移動し、その後で変換を連結して、子供を動かすと回転した方向に動かないようにします。
EDIT:Toriousは(まだ動作していません)投稿 コード:コードの
public AffineTransform getTransformTo(Element ancestor) {
AffineTransform t = (AffineTransform)getAffineTransform().clone();
Element parent = getParent();
while (parent != null && parent != ancestor) {
t.preConcatenate(parent.getAffineTransform());
parent = parent.getParent();
}
return t;
}
public void translateInAncestorSpace(Element ancestor, Point translation) {
AffineTransform fromAncestor = getTransformTo(ancestor); // to ancestor space
try
{
fromAncestor.invert();
} catch(Exception e)
{
e.printStackTrace();
}
translation = (Point)fromAncestor.transform(translation, new Point());
Transform t1 = new Transform();
t1.translate(translation.x,translation.y);
transform(t1);
}
出力:
Moving by [x=1,y=1] old position [x=22,y=40]
Moved by [x=-21,y=-39] new position [x=1,y=1]
Moving by [x=2,y=2] old position [x=1,y=1]
Moved by [x=1,y=1] new position [x=2,y=2]
Moving by [x=4,y=3] old position [x=2,y=2]
Moved by [x=2,y=1] new position [x=4,y=3]
すぐに役立つように、[SSCCE](http://sscce.org/)を投稿してください。ところで - あなたは質問がありますか? –