C# 将角度(以度为单位)转换为向量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18851761/
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
Convert an angle in degrees, to a vector
提问by Rory Becker
I'm doing some game programming. FWIW I'm using XNA, but I'm doubtful that this is relevant.
我正在做一些游戏编程。FWIW 我正在使用 XNA,但我怀疑这是否相关。
I'd like to convert degrees to a directional vector (ie X and Y) with magnitude 1.
我想将度数转换为幅度为 1 的方向向量(即 X 和 Y)。
My origin (0,0) is in the upper left.
我的原点 (0,0) 在左上角。
So I'd like 0 degrees to convert to [0, -1]
所以我想将 0 度转换为 [0, -1]
I thought the best way to do this was to take my definition of North/Up and rotate it using a matrix, but this does not seem to be working.
我认为最好的方法是采用我对北/上的定义并使用矩阵旋转它,但这似乎不起作用。
Here is the Code...
这是代码...
public class Conversion
{
public static Vector2 GetDirectionVectorFromDegrees(float Degrees)
{
Vector2 North= new Vector2(0, -1);
float Radians = MathHelper.ToRadians(Degrees);
var RotationMatrix = Matrix.CreateRotationZ(Radians);
return Vector2.Transform(North, RotationMatrix);
}
}
... and here are my unit tests...
...这是我的单元测试...
[TestFixture]
public class Turning_Tests
{
[Test]
public void Degrees0_Tests()
{
Vector2 result = Conversion.GetDirectionVectorFromDegrees(0);
Assert.AreEqual(0, result.X);
Assert.AreEqual(-1, result.Y);
}
[Test]
public void Degrees90_Tests()
{
Vector2 result = Conversion.GetDirectionVectorFromDegrees(90);
Assert.AreEqual(1, result.X);
Assert.AreEqual(0, result.Y);
}
[Test]
public void Degrees180_Tests()
{
Vector2 result = Conversion.GetDirectionVectorFromDegrees(180);
Assert.AreEqual(0, result.X);
Assert.AreEqual(1, result.Y);
}
[Test]
public void Degrees270_Tests()
{
Vector2 result = Conversion.GetDirectionVectorFromDegrees(270);
Assert.AreEqual(-1, result.X);
Assert.AreEqual(0, result.Y);
}
}
Am I approaching this all wrong? Should I be using a matrix? Have I screwed up and converted from degrees to radians in the wrong place?
我这样做是错的吗?我应该使用矩阵吗?我是否在错误的地方搞砸并从度数转换为弧度?
I've seen suggestions that this can be done using code like:
我已经看到可以使用以下代码完成此操作的建议:
new Vector2((float)Math.Cos(Angle), (float)Math.Sin(Angle));
...or sometimes...
……或者有时……
new Vector2((float)Math.Sin(Angle), (float)Math.Cos(Angle));
However these don't seem to work either
然而,这些似乎也不起作用
Can someone put me on the right path... or better yet give me some code which causes the 4 provided unit tests to path?
有人可以让我走上正确的道路......或者更好地给我一些代码,导致 4 个提供的单元测试通过?
Many thanks in advance.
提前谢谢了。
采纳答案by Jeppe Stig Nielsen
Just use:
只需使用:
new Vector2((float)Math.Cos(radians), (float)Math.Sin(radians))
Be sure to convert from degrees to radians with this approach too.
一定要使用这种方法将度数转换为弧度。
This uses the mathematician's convention of starting from [1, 0]
and going in the direction towards [0, 1]
(that is counter-clockwise with the orientation that mathematicians use for the two axes).
这使用了数学家的惯例,即从[1, 0]
朝向的方向开始和前进[0, 1]
(即逆时针方向,数学家用于两个轴的方向)。
To use instead your convention (starting from [0, -1]
and going in the direction of [1, 0]
) you need:
要使用您的约定(从 开始[0, -1]
并朝着 方向[1, 0]
),您需要:
new Vector2((float)Math.Sin(radians), -(float)Math.Cos(radians))
Note that your conversion from degrees to radians can never be exact (it involves something with π
). You should allow for some tolerance in your tests. Also, if you use double
instead of float
for the radians
, you will have some extra precision in the intermediate calculation.
请注意,从度数到弧度的转换永远不可能精确(它涉及到π
)。您应该在测试中允许一些容忍度。此外,如果您使用double
而不是float
for radians
,您将在中间计算中获得一些额外的精度。