c# - 如果数量在阈值内,则捕捉到角度

标签 c# math

我想知道什么是检查角度是否在特定角度的特定阈值内的简单方法。

比如说 45 度,我目前可以获得 45 度的精确倍数,但我希望两侧各有 10 度的余量,这样对于整个 360 度圆也有效。但不确定是否有一种干净的方法来做到这一点。

这是我目前拥有的:

float angle = Vector3.SignedAngle(currentGrid.Vector3,Vector3.right, Vector3.up); //[-180,180]
angle += 360;
angle %= 360; //[0,360]
int angle2 = (int)(Mathf.RoundToInt(angle / 5f) * 5f); //snap to nearest 5 degrees

if (angle2 % 45 == 0 || /*angle is within threshold of a multiple of 45 degrees*/ ){
   print("Hello");
}

有没有一种方法可以做到这一点,而无需对每个 45 度角进行大量 if 检查?

最佳答案

您可以按照与 5° 相同的方式进行操作:

var closest45 = (int)(Mathf.RoundToInt(angle2 / 45f) * 45f);
if(Math.Abs(closest45 - angle2) < 10) //allow a 10° tolerance to either side
    angle2 = closest45;

关于c# - 如果数量在阈值内,则捕捉到角度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59433023/

相关文章:

C#快速像素渲染

c# WPF 过滤控件列表

c# - 如何在 C# Windows 应用程序中获取网格的列值?

java - 圆的 3D 旋转使边缘交叉两点

c# - c#中sql查询列中的无限小数值

C# 单击按钮时从字典中随机输入

c - 寻找素数。 6k+1 6k-1 以外的质数形式

algorithm - 在这种情况下,我对 big o 的解释是否正确?

math - "Law of the Eight"是什么?

python - 了解梯度策略推导