C# 在 2D 中将一个点旋转另一个点

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/786472/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-05 01:06:12  来源:igfitidea点击:

Rotate a point by another point in 2D

c#mathrotationangle

提问by James

I want to know how to work out the new co-ordinates for a point when rotated by an angle relative to another point.

我想知道如何计算一个点相对于另一个点旋转一个角度时的新坐标。

I have a block arrow and want to rotate it by an angle theta relative to a point in the middle of the base of the arrow.

我有一个块箭头,想将它旋转一个角度 theta 相对于箭头底部中间的一个点。

This is required to allow me to draw a polygon between 2 onscreen controls. I can't use and rotate an image.

这是允许我在 2 个屏幕控件之间绘制多边形所必需的。我无使用和旋转图像。

From what I have considered so far what complicates the matter further is that the origin of a screen is in the top left hand corner.

从我目前所考虑的情况来看,使问题进一步复杂化的是屏幕的原点位于左上角。

采纳答案by Sophie Alpert

If you rotate point (px, py)around point (ox, oy)by angle thetayou'll get:

如果您旋转点(px, py)周围点(ox, oy)的角度theta,你会得到:

p'x = cos(theta) * (px-ox) - sin(theta) * (py-oy) + ox
p'y = sin(theta) * (px-ox) + cos(theta) * (py-oy) + oy

回答by Mehrdad Afshari

If you are using GDI+ to do that, you can use Transformmethods of the Graphicsobject:

如果您使用 GDI+ 来做到这一点,您可以使用对象的TransformGraphics

graphics.TranslateTransform(point of origin);
graphics.RotateTransform(rotation angle);

Then draw the actual stuff.

然后画出实物。

回答by thumbmunkeys

If you have the System.Windows.Medianamespace available, then you can use the built in transformations:

如果您有System.Windows.Media可用的命名空间,那么您可以使用内置转换:

    using System.Windows.Media;

    var transform = new RotateTransform() {Angle = angleInDegrees, CenterX = center.X, CenterY = center.Y};
    var transformedPoint = transform.Transform(point);