Click here to Skip to main content
15,885,278 members
Articles / Programming Languages / C#

Using Matrices to Flatten a TransformGroup

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
19 Oct 2012CPOL1 min read 5.4K  
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:

C#
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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
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.

Comments and Discussions

 
-- There are no messages in this forum --