WPF - 2D Matrix Transformation

Video WPF - 2D Matrix Transformation

Matrix (Matrix) is the method used to perform all kinds of geometric transformations in 2D or 3D space. In WPF, you can use the class MatrixTransform an object with Matrix to make any kind of transform that I showed in the previous post: WPF - 2D Transformation .

A Matrix object includes the property:

 

Name

Description

M11 Gets or sets the value of the first row and first column of this Matrix structure.
M12 Gets or sets the value of the first row and second column of this Matrix structure.
M21 Gets or sets the value of the second row and first column of this Matrix structure.
M22 Gets or sets the value of the second row and second column of this Matrix structure.
OffsetX Gets or sets the value of the third row and first column of this Matrix structure.
OffsetY Gets or sets the value of the third row and second column of this Matrix structure.

Position representatives of this property is illustrated as follows:

WPF - 2D Matrix Transformation

For each transformation, you need to change the property of the Matrix on theoretical computer graphics. View: Transformation matrix (Wikipedia).

For example, you can perform two transformations rotated 90 degrees (rotate) and go 200 units shifted horizontally, vertically 10 units as follows:

< Image Source = "baby-girl.jpg" Name = "image1" >
    < Image.RenderTransform >
        < MatrixTransform >
            < MatrixTransform.Matrix >
                < Matrix M11 = "0" M12 = "1" M21 = "-1" M22 = "0" OffsetX = "200" OffsetY = "10" />
            </ MatrixTransform.Matrix >
        </ MatrixTransform >
    </ Image.RenderTransform >
</ Image >

WPF - 2D Matrix Transformation

However, instead of having to remember and calculate the value of each element in the matrix, the representative structure of .NET Matrix provides methods for each type of conversion. Considering the purpose, the methods are divided into two categories:

- The matrix processing method:

Method Description
Append Appends the rõ Matrix Matrix structure to this structure
Equals Determines Whether the two have the same structures rõ Matrix values
Invert Inverts this Matrix structure
Multiply Multiplies a Matrix structure by another Matrix structure
Prepend Prepends the rõ Matrix Onto this structure Matrix structure
SetIdentity Changes this Matrix structure Into an identity matrix

- The method used to create a matrix for converting geometry:

Method Description
Rotate Applies a rotation angle of the rõ about the origin of the Matrix structure
RotateAt Rotates the matrix about the rõ point
Scale Appends the rõ scale vector to the Matrix structure.
ScaleAt Scales the Matrix by the rõ amount about the rõ point
Skew Appends a skew to the Matrix structure
Translate Appends a translation to the Matrix structure

Would be more convenient if you use these methods in the code-behind to transform instead of XAML. Therefore the content behind will mainly focus on the introduction, through a simple example for you to see the behavior of the method.

First you create a WPF project named MatrixTransformation. Then edit the corresponding file with the following content:

MainWindow.xaml:

< Window x: Class = "MatrixTransformation.MainWindow"
        xmlns: x = " http://schemas.microsoft.com/winfx/2006/xaml "
        Title = "MatrixTransformation Demo" Height = "350" Width = "525" >
    < Window.Resources >
        < Style TargetType = "Image" >
            < Setter Property = "Width" Value = "200" />
            < Setter Property = "Height" Value = "200" />
            < Setter Property = "Stretch" Value = "Fill" />
            < Setter Property = "Effect" >
                < Setter.Value >
                    < DropShadowEffect Color = "Black" Direction = "-50" ShadowDepth = "10" Opacity = "0.5" />
                </ Setter.Value >
            </ Setter >
        </ style >
        < Style TargetType = "Button" >
            < Setter Property = "Margin" Value = "5" />
            < Setter Property = "Padding" Value = "5" />
        </ style >
    </ Window.Resources >
    < DockPanel >
        < StackPanel DockPanel.Dock = "Left" Width = "200" >
            < Button Content = "Reset" Click = "ResetButton_Click" />
            < Button Content = "Translate" Click = "TranslateButton_Click" />
            < Button Content = "Rotate" Click = "RotateButton_Click" />
            < Button Content = "Scale" Click = "ScaleButton_Click" />
            < Button Content = "Skew" Click = "SkewButton_Click" />
            < Button Content = "Invert" Click = "InvertButton_Click" />
        </ StackPanel >
            < Image Source = "baby-girl.jpg" Name = "image1" />
    </ DockPanel >
</ Window >

MainWindow.xaml.cs:

using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
 
namespace MatrixTransformation
{
    ///
<Summary> /// Interaction logic for MainWindow.xaml
 /// </ Summary>
    public partial class MainWindow: Window
    {
        _matrix Matrix = new Matrix ();
        public MainWindow ()
        {
            InitializeComponent ();
 
            this .DataContext = _matrix;
        }
        private void ResetButton_Click ( object sender, RoutedEventArgs e)
        {
            _matrix = new Matrix ();
            image1.RenderTransform = new MatrixTransform (_matrix);
        }
        private void TranslateButton_Click ( object sender, RoutedEventArgs e)
        {
            _matrix.Translate (100, -50);
            image1.RenderTransform = new MatrixTransform (_matrix);
        }
        private void RotateButton_Click ( object sender, RoutedEventArgs e)
        {
            _matrix.Rotate (45);
            image1.RenderTransform = new MatrixTransform (_matrix);
        }
        private void ScaleButton_Click ( object sender, RoutedEventArgs e)
        {
            _matrix.Scale (1.2, 1.2); // 120%
            image1.RenderTransform = new MatrixTransform (_matrix);
        }
        private void SkewButton_Click ( object sender, RoutedEventArgs e)
        {
            _matrix.Skew (10, 10);
            image1.RenderTransform = new MatrixTransform (_matrix);
        }
        private void InvertButton_Click ( object sender, RoutedEventArgs e)
        {
            _matrix.Invert ();
            image1.RenderTransform = new MatrixTransform (_matrix);
        }
    }
}

Illustration:

WPF - 2D Matrix Transformationhttps://yinyangit.wordpress.com

Related article:

How to enable / disable TouchDrive mode when playing Asphalt 9: Legends

How to enable / disable TouchDrive mode when playing Asphalt 9: Legends

TouchDrive is a new feature added in Asphalt 9: Legends racing game. This autopilot feature will help us play Asphalt 9 much easier and more convenient than previous versions. Players can optionally turn on, turn off TouchDrive to change

How to unlink Facebook accounts in Free Fire

How to unlink Facebook accounts in Free Fire

Detailed steps guide to unlinking from the Facebook account registered in Garena Free Fire.

Summary of FIFA Online 4 errors and how to fix them

Summary of FIFA Online 4 errors and how to fix them

Today 14/6, FIFA Online 4 officially Open Beta in Vietnam, with countless exciting events and a series of interesting features for players to experience to share the exciting atmosphere of the 2018 World Cup.

How to back up Adorable Home game data

How to back up Adorable Home game data

Backup data when playing Adorable Home game is very necessary, this will help you keep all game data when updating or encounter errors related to losing Adorable Home game account.

How to take screenshots of the game screen on GameLoop

How to take screenshots of the game screen on GameLoop

GameLoop supports players taking screenshots when playing games. This is a nice and useful feature when playing games on this Android emulator but is often overlooked.

How to switch from Survival to Creative mode in Mini World: Block Art

How to switch from Survival to Creative mode in Mini World: Block Art

How to switch between these two game modes will help you not have to create a new map once youve built your own work in Mini World: Block Art

Adorable Home: List of code and how to enter the code in the game

Adorable Home: List of code and how to enter the code in the game

Instructions on how to find and enter special Adorable Home code in the game to earn rewards from the game developer.

Quickly transfer your international PUBG Mobile account to PUBG Mobile VN so that it will not be reset before December 31

Quickly transfer your international PUBG Mobile account to PUBG Mobile VN so that it will not be reset before December 31

How to convert international PUBG Mobile accounts to PUBG Mobile VNG without data loss.

Dragon Raja: Instructions for logging in and linking accounts

Dragon Raja: Instructions for logging in and linking accounts

Login and link your account to save character data in the mobile game Dragon Raja - mobile game with very nice graphics.

GameDev - Intersection of Two Lines (intersection of two lines)

GameDev - Intersection of Two Lines (intersection of two lines)

From two straight lines (or vector) in a 2D plane, we can find their intersections to calculate the angle of reflection and direction of travel.

Should use Auto FIFA Online 4 to hack FO4 game

Should use Auto FIFA Online 4 to hack FO4 game

Besides the values ​​that Auto FIFA Online 4 brings, it is very difficult for gamers to "keep themselves", we will face unfortunate risks.

PUBG Mobile: 6 types of recovery props that help you survive longer

PUBG Mobile: 6 types of recovery props that help you survive longer

PUBG Mobile, the famous survival game adapted from the previous version of PlayerUnknowns Battleground on PC, just like the name of this game genre said, survival is a top goal, you need to survive and go through the attack from the enemy, then n

How to make automatic doors in Mini World: Block Art

How to make automatic doors in Mini World: Block Art

You can create automatic doors using widgets available in Mini World: Block Art

How to install the game Clash Royale for Vietnamese gamers

How to install the game Clash Royale for Vietnamese gamers

Clash Royale is a name that is not new to the World gaming community, however, in Vietnam, this game has not been officially launched. If you want to play Clash Royale, you can follow the following instructions to install Clash Ro game

PUBG Mobile: Top 5 best camp locations on Erangel map

PUBG Mobile: Top 5 best camp locations on Erangel map

Especially in this map we also have many ideal locations for you to land. So where is the most favorable camp location in the map Erangel in PUBG Mobile game

Instructions on how to play the AOE Empire on Garena

Instructions on how to play the AOE Empire on Garena

Garena is one of the world famous gaming software, and playing Empire on Garena is also a way for gamers to connect with other passionate players around the world.

How to play Garena Contra: Return using the keyboard

How to play Garena Contra: Return using the keyboard

In addition to downloading and installing Garena Contra Return on PC, we can also set up to play Garena Contra Return with the keyboard to improve the experience.

Fix Your system is low on memory on GameLoop

Fix Your system is low on memory on GameLoop

Your system is low on memory. Close some applications and restart the game is one of the common and quite common errors on the GameLoop emulator.

How to open an NPC store in Mini World: Block Art

How to open an NPC store in Mini World: Block Art

Shop in Mini World: Block Art will help you trade, exchange with other players a variety of items and useful items while playing this survival game.

MFC - Lession3: Tree Control and List Control

MFC - Lession3: Tree Control and List Control

Tree controls and list controls are two commonly used controls in the problem of displaying data, retrieve, manage resources, directories, ...