Objective-C:在 Objective-C 中找到角度象限的最有效方法

Objective-C: most efficient way to find the quadrant of an angle in Objective-C

找到Objective-C中给出的角度的象限最有效的方法是什么(假设边界角0、90、270、360等都落在一个象限内)?

您不需要任何魔法功能。
整圆包含 2*Pi 弧度和 4 个象限。
所以只需将角度除以 Pi/2 并进行四舍五入以获得 0..3 象限编号(如果需要 1..4,则加 1)

Python 例子。请注意,整数模运算 % 4 提供 "angle normalisation" 因此函数适用于大角和负角
(Swift %table here 的工作方式不同,因此您可能需要制作类似 return ((floor(2.0 * angle / math.pi) % 4 + 4) %4 的内容)

import math
def quadrant(angle):
    return math.floor(2.0 * angle / math.pi) % 4

print(quadrant(math.pi/4))
print(quadrant(math.pi/4 + math.pi))
print(quadrant(math.pi/4 - math.pi))

0
2
2