Note: I’m using Lua.
So, I’m trying to find out the degrees between two points on a circle. The problem is between something like 340 and 20, where the correct answer is 40 degrees, but doing something like
function FindLeastDegrees(s, f)
return ((f - s+ 360) % 360)
end
print(FindLeastDegrees(60, 260))
-- S = Start, F = Finish (In degrees)
Which works all all situations except for when trying to figure out the distance between the two. This below code is my next failed attempt.
function FindLeastDegrees(s, f)
local x = 0
if math.abs(s-f) <= 180 then
x = math.abs(s-f)
else
x = math.abs(f-s)
end
return x
end
print(FindLeastDegrees(60, 260))
I then tried:
function FindLeastDegrees(s, f)
s = ((s % 360) >= 0) and (s % 360) or 360 - (s % 360);
f = ((f % 360) >= 0) and (f % 360) or 360 - (f % 360);
return math.abs(s - f)
end
print(FindLeastDegrees(60, 350))
--> 290 (Should be 70)
So that failed. :/
So how would you find the shortest amount of degrees between two other degrees, and then if you should go clockwise or counterclockwise (Add or subtract) to get there. I’m entirely confused.
A few examples of what I’m trying to do…
FindLeastDegrees(60, 350)
--> 70
FindLeastDegrees(-360, 10)
--> 10
Which seems so hard! I know I will have to use…
- Modulus
- Absolute Values?
I would also like it to return if I should add or subtract to get to the value ‘Finish’.
Sorry for the lengthy description, I think you probably have got it…. :/
If the degrees are in the 0 to 360 range, the
% 360part can be skipped: