如何比较两个正切值?

如何比较两个正切值?问题在于atan2有时会返回非常相似的角度,但结果却相差很大。

local t1 = math.atan2(y2-y1, x2-x1)
local t2 = math.atan2(y3-y2, x3-x2)
if t2-t1 < -math.pi/2 then
    print (t1, t2, t2-t1+2*math.pi)
elseif t2-t1 > math.pi/2 then
    print (t1, t2, t2-t1-2*math.pi)
else
    print (t1, t2, t2-t1)
end

或许可以这样更正:

local t3 = t2-t1 < -math.pi/2 and 2*math.pi or t2-t1 > math.pi/2 and -2*math.pi or 0
print (t1, t2, t2-t1+t3)
点赞