0

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# ?

rolandl
  • 1,769
  • 1
  • 25
  • 48
  • Possible duplicate of [http://stackoverflow.com/questions/636081/how-to-rotate-scale-and-translate-a-matrix-all-at-once-in-c#](http://stackoverflow.com/questions/636081/how-to-rotate-scale-and-translate-a-matrix-all-at-once-in-c). – John Odom Apr 30 '15 at 22:32
  • @JohnOdom : Thx for your help. It it not a duplicate. In fact, The `System.Windows.Media.Matrix` class does not provide `Scaling` or `Translation` method. But the `Matrix` from the Xna namespace seems to has these methods. Should I use this class ? Xna is deprecated isn't it ? – rolandl Apr 30 '15 at 22:39

2 Answers2

1

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 ;
rolandl
  • 1,769
  • 1
  • 25
  • 48
0

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.

Ani
  • 10,826
  • 3
  • 27
  • 46
  • Thx for your help. Unfortunately, I do not have access to all the methods describe in this documentation : https://msdn.microsoft.com/fr-fr/library/system.windows.media.matrix%28v=vs.110%29.aspx Basically, after instantiate a matrix like this `var matrix = new Matrix()` the auto-completion does not provide the `scale` method... – rolandl Apr 30 '15 at 23:01
  • That's what I'm saying. You can't use `System.Windows.Media.TranslateTransform` and `System.Windows.Media.ScaleTransform`. You need to use `System.Windows.Media.Matrix` along with the [Translate](https://msdn.microsoft.com/en-us/library/system.windows.media.matrix.translate(v=vs.110).aspx) and [Scale](https://msdn.microsoft.com/en-us/library/system.windows.media.matrix.scale(v=vs.110).aspx) methods. – Ani May 01 '15 at 18:35
  • But In Windows Phone these methods do not exist : https://msdn.microsoft.com/en-us/library/windows/apps/system.windows.media.matrix%28v=vs.105%29.aspx Anyway, I have post the solution. – rolandl May 02 '15 at 17:53