数学(在WPF中):翻译后获取新的x,y坐标 [英] Math (in WPF): Getting new x,y coordinates after a translation

查看:119
本文介绍了数学(在WPF中):翻译后获取新的x,y坐标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

参考此我正在开发的编程游戏.

我正在使用WPF对画布进行动画处理,并且正在使用BeginAnimation方法在另一个画布上平移(移动)画布.

I am using WPF to animate canvases, and I am using the BeginAnimation method to translate (move) a canvas across another canvas.

对于BeginAnimation,我需要为x和y指定FromTo坐标,这是我正在使用的方法,如下所示:

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.

所以我的问题是,鉴于画布当前旋转的角度(0-359),以x和y坐标(画布当前所在的位置)和距离(以px为单位)开始,我该如何计算终点坐标?,即画布最终将转换到的位置.

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.

替代文字http://img244.imageshack.us/img244/4794/canvastranspositionmi5 .jpg

在上图中,我画了一个我要实现的示例.

In the above image, I have drawn an example of what I want to achieve.

假设画布(实线框)的当前航向(角度)为130度,需要将其平移(沿着该角度向下的路径;即,取决于当前面对的位置)200像素. ..画布的新坐标是什么(它将停止动画显示:虚线框)?如何计算这些位置的新坐标?

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]解决方案:

感谢两个安迪(Andy) Cameron ,它终于可以按预期工作了.

Thanks to the help of both Andy and Cameron, it is finally working as intended.

这是工作代码:

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);

推荐答案

假设您从12点开始顺时针旋转,则新的x坐标将为:

Assuming you're rotating clockwise from 12 o'clock, your new x-coordinate will be:

sin(130) * 200 = 153 + original x-coordinate

您的新y坐标将为

cos(130) * 200 = -129 + original y-coordinate (assuming negative 'y' is down)

如下所示,请注意,例如,在C#中,sincos采用弧度而不是度-乘以Math.PI/180首先获得弧度值.

As below, note that in C#, for example, sin and cos take radians, not degrees - multiply by Math.PI/180 to get the value in radians first.

这篇关于数学(在WPF中):翻译后获取新的x,y坐标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆