数学(在 WPF 中):平移后获取新的 x,y 坐标
我正在使用 WPF 为画布设置动画,并使用 BeginAnimation
方法在另一个画布上平移(移动)一个画布。
使用 BeginAnimation,我需要指定 x 和 y 的 From
和 To
坐标,这就是我使用的方法,如下所示:
//X
Animator_Body_X.From = Translate_Body.X; //Current x-coordinate
Animator_Body_X.To = //The end X-coordinate
Translate_Body.BeginAnimation(TranslateTransform.XProperty, Animator_Body_X);
//Y
Animator_Body_Y.From = Translate_Body.Y; //Current y-coordinate
Animator_Body_Y.To = //The end Y-coordinate
Translate_Body.BeginAnimation(TranslateTransform.YProperty, Animator_Body_Y);
现在画布需要使用给定角度进行翻译,我可以从方法中获得该角度。
所以我的问题是,给定画布当前旋转的角度 (0-359)、起始 x 和 y 坐标(画布当前所在的位置)和距离(以 px 为单位),我如何计算结束坐标? 即画布最终将被转换到的位置。
替代文本 http://img244.imageshack.us/img244/4794/canvastranspositionmi5.jpg< /a>
在上图中,我画了一个我想要实现的示例。
假设画布(实心边框框)的当前方向(角度)为 130 度,并且需要将其平移(沿着该角度的路径;即取决于它当前面向的位置)200 像素...什么将是画布的新坐标(它将停止动画:虚线边框框)? 我如何计算它将停止的新坐标?
[更新]解决方案:
这是工作代码:
double headingRadians = Heading * (Math.PI / 180);
Animator_Body_X.From = Translate_Body.X;
Animator_Body_X.To = Math.Sin(headingRadians) * pix + Translate_Body.X;
Translate_Body.BeginAnimation(TranslateTransform.XProperty, Animator_Body_X);
Animator_Body_Y.From = Translate_Body.Y;
Animator_Body_Y.To = ((Math.Cos(headingRadians) * pix) * -1) + Translate_Body.Y;
Translate_Body.BeginAnimation(TranslateTransform.YProperty, Animator_Body_Y);
With reference to this programming game I am currently building.
I am using WPF to animate canvases, and I am using the BeginAnimation
method to translate (move) a canvas across another canvas.
With the BeginAnimation, I need to specify the From
and To
coordinates for both x and y, and this is the method I am using this like such:
//X
Animator_Body_X.From = Translate_Body.X; //Current x-coordinate
Animator_Body_X.To = //The end X-coordinate
Translate_Body.BeginAnimation(TranslateTransform.XProperty, Animator_Body_X);
//Y
Animator_Body_Y.From = Translate_Body.Y; //Current y-coordinate
Animator_Body_Y.To = //The end Y-coordinate
Translate_Body.BeginAnimation(TranslateTransform.YProperty, Animator_Body_Y);
Now the canvas needs to be translated using a given angle, which I have available from the method.
So my question is, given the angle (0-359) the canvas is currently rotated at, starting x and y coordinates (of where the canvas is currently situated) and distance (in px), how do I calculate to end coordinates? ie to where the canvas will finally be translated to.
alt text http://img244.imageshack.us/img244/4794/canvastranspositionmi5.jpg
In the above image, I have drawn an example of what I want to achieve.
Suppose the canvas (solid-border box) has a current heading (angle) of 130 degrees, and it needs to be translated (following a path down that angle; ie depending on where it is currently facing) by 200 pixels...what will be the new coordinates (where it will stop animating: dashed-border box) of the canvas? How do I calculate these new coordinates of where it will stop?
[UPDATE] Solution:
Thanks to the help of both Andy and Cameron, it is finally working as intended.
And here is the working code:
double headingRadians = Heading * (Math.PI / 180);
Animator_Body_X.From = Translate_Body.X;
Animator_Body_X.To = Math.Sin(headingRadians) * pix + Translate_Body.X;
Translate_Body.BeginAnimation(TranslateTransform.XProperty, Animator_Body_X);
Animator_Body_Y.From = Translate_Body.Y;
Animator_Body_Y.To = ((Math.Cos(headingRadians) * pix) * -1) + Translate_Body.Y;
Translate_Body.BeginAnimation(TranslateTransform.YProperty, Animator_Body_Y);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
假设您从 12 点钟开始顺时针旋转,您的新 x 坐标将为:
新的 y 坐标将为
如下所示,请注意,在 C# 中,例如,
sin
和cos
采用弧度,而不是度数 - 乘以Math.PI/180
首先获得以弧度表示的值。Assuming you're rotating clockwise from 12 o'clock, your new x-coordinate will be:
And your new y-coordinate will be
As below, note that in C#, for example,
sin
andcos
take radians, not degrees - multiply byMath.PI/180
to get the value in radians first.您可以使用
cos
和sin
计算 x 和 y 坐标的移动量。 这是基于单位圈。单位圆有从右侧开始逆时针旋转的角度。 所以在单位圆中,130 度的角度实际上是 320 度。
另一件需要注意的事情是
cos
和sin
函数使用 弧度而不是度数。You can use
cos
andsin
to work out the amount of movement in the x and y coordinates. This is based on the unit circle.The unit circle has angles starting from the right side, going anti-clockwise. So in the unit circle your angle of 130 degrees is actually 320 degrees.
The other thing to note is that the
cos
andsin
functions are using radians instead of degrees.我将详细介绍如何在不使用数学的情况下做到这一点:
请注意,我没有尝试我所说的,所以可能会有一些错误。
我将您正在移动的正方形称为 Visual B,
将正方形的容器称为 Visual A。
现在,您希望能够将 B 从 B 原点转换为 B',并从 A 原点检索 B' 坐标。
如果你的平移是(200,0),那么B'相对于B原点的坐标就是(200,0)。
您需要来自 A 原点的这些坐标。 所以你必须这样做。
pointFromAOrigin 反映了你想要的。
I will detail how you can do without using Mathematics :
Note that, I don't try what I'm saying, so maybe there is some mistakes.
I will call the square you are moving Visual B,
I will call the container of the square Visual A.
Now, you want to be able to do a translation of B from B origin to B', and retrieve B' coordinate from A origin.
If your translation is (200,0), then the coordinate of B' from B origin is (200,0).
You want these coordinates from A origin. So you have to do.
pointFromAOrigin reflect what you want.
如果您有以下需求,请查看 TransformToVisual拥有来自 Visual A 原点的对象坐标,并且想要来自 Visual B 原点的对象坐标,那么您可以
根据需要将转换从 GeneralTransform 转换为 Transform。
然后您可以使用 GeneralTransform 对象的 Transform 方法。
look at the TransformToVisual if you have the coordinate of your object from Visual A origin and you want the coordinates of your object from Visual B origin, then you do
you can cast transformation from GeneralTransform to Transform if needed.
Then you can use Transform method of your GeneralTransform object.