数学(在 WPF 中):平移后获取新的 x,y 坐标

发布于 2024-07-13 15:16:16 字数 2074 浏览 6 评论 0原文

参考这个编程游戏< /a> 我当前正在构建。

我正在使用 WPF 为画布设置动画,并使用 BeginAnimation 方法在另一个画布上平移(移动)一个画布。

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

//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 像素...什么将是画布的新坐标(它将停止动画:虚线边框框)? 我如何计算它将停止的新坐标?

[更新]解决方案:

感谢安迪Cameron,它终于按预期工作了。

这是工作代码:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

滴情不沾 2024-07-20 15:16:16

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

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 首先获得以弧度表示的值。

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

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

And your new y-coordinate will be

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

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.

挽清梦 2024-07-20 15:16:16

您可以使用 cossin 计算 x 和 y 坐标的移动量。 这是基于单位圈

单位圆有从右侧开始逆时针旋转的角度。 所以在单位圆中,130 度的角度实际上是 320 度。

actualAngle = 90 - 130 = -40 
(which is the same as 320 degrees when used in sin/cos)

另一件需要注意的事情是 cossin 函数使用 弧度而不是度数。

angleInRadians = 320 * pi / 180 (about 5.59)
x = cos(angleInRadians) * 200 + 300 (about 453.21)
y = sin(angleInRadians) * 200 + 400 (about 271.44)

You can use cos and sin 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.

actualAngle = 90 - 130 = -40 
(which is the same as 320 degrees when used in sin/cos)

The other thing to note is that the cos and sin functions are using radians instead of degrees.

angleInRadians = 320 * pi / 180 (about 5.59)
x = cos(angleInRadians) * 200 + 300 (about 453.21)
y = sin(angleInRadians) * 200 + 400 (about 271.44)
永言不败 2024-07-20 15:16:16

我将详细介绍如何在不使用数学的情况下做到这一点:

请注意,我没有尝试我所说的,所以可能会有一些错误。

我将您正在移动的正方形称为 Visual B,

将正方形的容器称为 Visual A。

现在,您希望能够将 B 从 B 原点转换为 B',并从 A 原点检索 B' 坐标。

如果你的平移是(200,0),那么B'相对于B原点的坐标就是(200,0)。
您需要来自 A 原点的这些坐标。 所以你必须这样做。

var transform = B.TransformToVisual(A);
var pointFromAOrigin  = transform.Transform(new Point(200,0));

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.

var transform = B.TransformToVisual(A);
var pointFromAOrigin  = transform.Transform(new Point(200,0));

pointFromAOrigin reflect what you want.

北城半夏 2024-07-20 15:16:16

如果您有以下需求,请查看 TransformToVisual拥有来自 Visual A 原点的对象坐标,并且想要来自 Visual B 原点的对象坐标,那么您可以

   var transformation = A.TransformToVisual(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

   var transformation = A.TransformToVisual(B);

you can cast transformation from GeneralTransform to Transform if needed.
Then you can use Transform method of your GeneralTransform object.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文