I have a canvas which i want to rotate and translate. I am using RotateTransform and TranslateTransform combined in a TransformGroup.
I want to do the following steps with these transforms:
- Rotate canvas with some angle.
- Translate my canvas to some distance
- Rotate canvas.
The center of rotation in step 1 and step 3 is same. But after performing step 2 i want to change center of rotation to a new position which should be in the center of the parent. But somehow this does not work.
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;
}
The problem is the center point, which i am resetting to the old point before every rotate. But after few moves and rotates this point goes somewhere outside the container or screen. Just want help to fix that one.
Where "C" the center of rotation for red rectangle, blue rectangle is the same red rectangle but translated. Now i need to rotate the blue rectangle but the center of rotation should be "C".