如何从 x 轴和 y 轴找到圆角
How can I find a circle angle from x and y axis
随时将我的问题标记为重复。因为我对 COS、SIN 和 TAN 完全一无所知,其他人可能已经问过这个问题了。
所以,我尝试根据可以从游戏手柄输入获得的 x 和 y 轴设置圆形进度条。进度条说简单点就是最小值0,最大值360。
我确实试着搜索了一下,但我最好的理解是它只适用于 180 度和正 x 和 y。但是我从控制器得到的输入是 y 从 -1 到 1(其中 x -1 是左边,1 是右边,y -1 是底部,1 是顶部)
到目前为止,这是我的代码。
var controller = Windows.Gaming.Input.Gamepad.Gamepads[0].GetCurrentReading();
x = controller.LeftThumbstickX
y = controller.LeftThumbstickY
//what do I have to do from here?
progress.Value = angle; //?
三角函数atan2
is the tool for this job. In C#, this is implemented by Math.Atan2
:
double angleInRadians = Math.Atan2(y, x);
double angleInDegrees = (180 / Math.PI) * angleInRadians;
将此公式与(例如)参数 (1,1)
一起使用,您将得到 45 的结果。
然而,就极轴排列而言,这个角度是从 "east" 开始逆时针测量的。要将其转换为从 "north" 顺时针测量的角度:
double compassRadians = Math.PI / 2 - angleInRadians;
double compassDegrees = (180 / Math.PI) * compassRadians;
但现在我们可能会遇到负值,所以我们可以通过以下方法对其进行归一化:
double normalizeDegrees(double a) => ((a % 360) + 360) % 360; //convert to 0-360
然后
var compassAngle = normalizeDegrees(compassDegrees);
您想要的方法是Math.Atan2
。这有两个参数 - 首先是 y 值,然后是 x 值 - 它给你一个以弧度为单位的角度。
由于您需要以度为单位的角度,因此需要进行转换 - 转换系数为 180 / Math.PI
。所以你将使用类似的东西:
var radiansToDegrees = 180 / Math.PI;
progress.Value = Math.Atan2(y,x) * radiansToDegrees;
根据 x 和 y 的确切组合需要对应于 0,您可能需要在之后添加度数。按原样,x = 1、y = 0 时为 0 度,x = 0、y = 1 时为 90 度,等等。
随时将我的问题标记为重复。因为我对 COS、SIN 和 TAN 完全一无所知,其他人可能已经问过这个问题了。
所以,我尝试根据可以从游戏手柄输入获得的 x 和 y 轴设置圆形进度条。进度条说简单点就是最小值0,最大值360。
我确实试着搜索了一下,但我最好的理解是它只适用于 180 度和正 x 和 y。但是我从控制器得到的输入是 y 从 -1 到 1(其中 x -1 是左边,1 是右边,y -1 是底部,1 是顶部)
到目前为止,这是我的代码。
var controller = Windows.Gaming.Input.Gamepad.Gamepads[0].GetCurrentReading();
x = controller.LeftThumbstickX
y = controller.LeftThumbstickY
//what do I have to do from here?
progress.Value = angle; //?
三角函数atan2
is the tool for this job. In C#, this is implemented by Math.Atan2
:
double angleInRadians = Math.Atan2(y, x);
double angleInDegrees = (180 / Math.PI) * angleInRadians;
将此公式与(例如)参数 (1,1)
一起使用,您将得到 45 的结果。
然而,就极轴排列而言,这个角度是从 "east" 开始逆时针测量的。要将其转换为从 "north" 顺时针测量的角度:
double compassRadians = Math.PI / 2 - angleInRadians;
double compassDegrees = (180 / Math.PI) * compassRadians;
但现在我们可能会遇到负值,所以我们可以通过以下方法对其进行归一化:
double normalizeDegrees(double a) => ((a % 360) + 360) % 360; //convert to 0-360
然后
var compassAngle = normalizeDegrees(compassDegrees);
您想要的方法是Math.Atan2
。这有两个参数 - 首先是 y 值,然后是 x 值 - 它给你一个以弧度为单位的角度。
由于您需要以度为单位的角度,因此需要进行转换 - 转换系数为 180 / Math.PI
。所以你将使用类似的东西:
var radiansToDegrees = 180 / Math.PI;
progress.Value = Math.Atan2(y,x) * radiansToDegrees;
根据 x 和 y 的确切组合需要对应于 0,您可能需要在之后添加度数。按原样,x = 1、y = 0 时为 0 度,x = 0、y = 1 时为 90 度,等等。