13,799,764 members
Technical Blog
alternative version

#### Stats

4.7K views
Posted 19 Oct 2012
Licenced CPOL

# Using Matrices to Flatten a TransformGroup

, 19 Oct 2012
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:

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.

## Share

 Software Developer (Senior) LECO Corporation United States
John Hauck has been developing software professionally since 1981, and focused on Windows-based development since 1988. For the past 17 years John has been working at LECO, a scientific laboratory instrument company, where he manages software development. John also served as the manager of software development at Zenith Data Systems, as the Vice President of software development at TechSmith, as the lead medical records developer at Instrument Makar, as the MSU student who developed the time and attendance system for Dart container, and as the high school kid who wrote the manufacturing control system at Wohlert. John loves the Lord, his wife, their three kids, and sailing on Lake Michigan.

 Pro