简体   繁体   English

在矩阵上缩放和转换转换

[英]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) : 我目前正在尝试在C#中(在Windows Phone库中)移植以下Java或Swift代码:

Here the Java code : 这是Java代码:

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 : 这是Swift代码:

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 : 基本上,在Windows Phone的C#中,我具有以下类:

  • 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 . 但是... TranslateTransformScaleTransform类的Transform方法适用于Point而不适用于Matrix

How can I apply scale and translate transformation to a matrix in C# ? 如何在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. @ john-odom想要说的是,您需要将两个变换相乘才能将它们的组合效果应用于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. Matrix.Multiply方法将帮助您完成所需的工作,基于XNA的答案的链接并不是指向您特定的类型/库,而是让您了解其背后的原理。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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