【发布时间】:2019-12-21 02:13:07
【问题描述】:
我想知道什么是检查角度是否在特定角度的特定阈值内的简单方法。
说 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 检查?
【问题讨论】: