回転および翻訳したいキャンバスがあります。RotateTransform と TranslateTransform を TransformGroup に組み合わせて使用しています。
これらの変換で次の手順を実行したいと思います。
- キャンバスをある角度で回転させます。
- キャンバスを少し離れた場所に移動する
- キャンバスを回転します。
ステップ 1 とステップ 3 の回転中心は同じです。しかし、ステップ 2 を実行した後、回転の中心を親の中心にある新しい位置に変更したいと考えています。しかし、どういうわけかこれは機能しません。
Canvas myCanvas = null; //Canvas to rotate and move
RotateTransform RotTransform = new RotateTransform();
TranslateTransform trans = new TranslateTransform();
TransformGroup tgroup = new TransformGroup();
double RotationAngle = 0;
double speed = .50;
//This center point should remain same on the screen during all the transformations
Point Center = new Point(200, 200);
Initialize()
{
tgroup.Children.Add(RotTransform);
tgroup.Children.Add(trans);
}
//This function moves the canvas in vertical direction by up or down arrow key
public void MoveCanvas(Key key)
{
double speed = 0;
if (key == Key.Up)
{
speed = 1;
}
else
{
speed = -1;
}
trans.Y += speed;
myCanvas.RenderTransform = tgroup;
}
//This is the function to rotate the canvas
public void RotateCanvas(Key key)
{
if (key == Key.Right)
{
RotationAngle += speed;
}
else if (key == Key.Left)
{
RotationAngle -= speed;
}
//Relocate the center point to the old center point i.e should be in the center of the container
Point inv = tgroup.Inverse.Transform(Center);
RotTransform.CenterX = inv.X;
RotTransform.CenterY = inv.Y;
RotTransform.Angle = RotationAngle;
myCanvas.RenderTransform = tgroup;
}
問題は中心点です。回転するたびに古い点にリセットしています。しかし、数回の移動と回転の後、この点はコンテナーまたは画面の外側のどこかに移動します。それを修正するための助けが欲しいだけです。
「C」は赤い四角形の回転の中心であり、青い四角形は同じ赤い四角形ですが、平行移動されています。青い四角形を回転させる必要がありますが、回転の中心は「C」である必要があります。