Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Using Matrices to Flatten a TransformGroup

0.00/5 (No votes)
19 Oct 2012 1  
How to use matrices to flatten a TransformGroup

There is a better way to flatten (merge) multiple transforms into a single transform than I explained in Flattening a TransformGroup. This better way involves matrix math – but luckily, we do not have to really know anything about matrix math to use it.

Posts in this series:

M

Read Flattening a TransformGroup for a painful introduction to this topic.

Here is the scenario: The RenderTransform of your window is bound to a TransformGroup. The TransformGroup contains two transformations:

  • _previousTransformations – represents all the user’s previous manipulations.
  • _currentTransformation – represents the user’s current manipulation.

As the user starts to manipulate your window, your program responds by placing those manipulations into _currentTransformation. The resulting GroupTransformation perfectly represents the sum of all of the user’s manipulations. This works well even if the center of rotation is different from previous centers of rotation. Once the user completed the manipulation, nothing else needs to be done.

However, before starting yet another manipulation, you will need to collapse (or combine) the two transformations into _previousTransformations, then reset _currentTransformation to be ready to receive the new manipulation.

Here is the code that performs the combining and reset at the start of the next manipulation:

void OnManipulationStarted(object sender, ManipulationStartedRoutedEventArgs args)

{
   _previousTransformations.Matrix = TransformGroup.Value;
   _currentTransformation.Reset();
   _currentTransformation.CenterX = args.Position.X;
   _currentTransformation.CenterY = args.Position.Y;
   args.Handled = true;
}

TransformGroup.Value is the matrix that represents the sum total of both _previousTransformations and _currentTransformation. Overwriting the matrix of _previousTransformations with this sum total is the magic step. Of course, you still need to reset _currentTransformation and you are done.

Read PanView - The Design for more information.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here