简体   繁体   中英

Scale and translate transformations on a matrix

I am currently trying to port the following Java or Swift code in C# (in a Windows Phone library) :

Here the Java code :

public Matrix zoom(float scaleX, float scaleY, float x, float y) 
{
 final Matrix save = new Matrix();
 save.set(anotherMatrix);
 save.postScale(scaleX, scaleY, x, y);
 return save;
}

And here the Swift code :

public func zoom(#scaleX: CGFloat, scaleY: CGFloat, x: CGFloat, y: CGFloat) -> CGAffineTransform
{
  var matrix = CGAffineTransformTranslate(_touchMatrix, x, y);
  matrix = CGAffineTransformScale(matrix, scaleX, scaleY);
  matrix = CGAffineTransformTranslate(matrix, -x, -y);
  return matrix;
}

Basically, in C# for Windows Phone I have the following classes :

  • System.Windows.Media.Matrix
  • System.Windows.Media.TranslateTransform
  • System.Windows.Media.ScaleTransform

But... the Transform method of the TranslateTransform and ScaleTransform classes work with Point and not with Matrix .

How can I apply scale and translate transformation to a matrix in C# ?

Here the solution :

var tt1 = new TranslateTransform(x,y);
var matrix=_touchMatrix* tt1.Value;

var sc=new ScaleTransform(scaleX, scaleY);
matrix = matrix *sc.Value;

var tt2 = new TranslateTransform(-x,-y);
matrix =matrix*tt2.Value ;

What @john-odom is trying to say is that you need to multiply two transforms together in order to apply their combined effect on a Point. The Matrix.Multiply method will help you do what you need, the link to the XNA-based answer wasn't to point you to those specific types/libraries but make you aware of the principle behind it, instead.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM