Click here to Skip to main content
13,865,419 members
Click here to Skip to main content
Add your own
alternative version

Tagged as


Posted 19 Oct 2012
Licenced CPOL

Using Matrices to Flatten a TransformGroup

, 19 Oct 2012
Rate this:
Please Sign up or sign in to vote.
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.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.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

John Michael Hauck
Software Developer (Senior) LECO Corporation
United States 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.

You may also be interested in...


Comments and Discussions

-- There are no messages in this forum --
Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web06 | 2.8.190214.1 | Last Updated 19 Oct 2012
Article Copyright 2012 by John Michael Hauck
Everything else Copyright © CodeProject, 1999-2019
Layout: fixed | fluid