How to find the mid-point of two angles(Python)?

Just as the title says. I tried adding the angles and dividing by two, but that didn’t give proper results. I tried converting the angles to two-component vectors then finding the mid-point between them(by normalizing their sum), but that approach fails when the two angles are exactly opposite. Any suggestions? Thanks.

In case it’s not clear what I mean by mid-point: I mean the angle that is exactly halfway between one angle and another(and I’m talking 2D angles, not Euler angles).

like an angle in degrees?

adding them together and dividing by two should work fine… what’s the problem?

Here’s an example to show the problem: 355 degrees and 5 degrees are only 10 degrees apart from each other. The real mid-point between them would be 360 or 0 degrees. However, (355 + 5) /2 = 180, which is clearly an incorrect answer.

Blender actually uses radians instead of degrees, but the problem is the same either way.

I think I may have figured it out now. It involves conversion to vectors and atan2. Here’s a sample, if anyone’s interested:

import math
pi = math.pi

Angle1 = (Insert any number)
Angle2 = (Insert any number)

Vec1 = (math.sin(Angle1), math.cos(Angle1))
    
Vec2 = (math.sin(Angle2), math.cos(Angle2))
  
VecSum = tuple(sum(x) for x in zip(Vec1, Vec2))

FinalVec = VecSum
FinalVec = tuple(x/(math.sqrt(VecSum[0]**2 + VecSum[1]**2)) for x in FinalVec)

FinalAngle = math.atan2(FinalVec[0], FinalVec[1])

FinalAngle is the midpoint of Angle1 and Angle2. I’m not 100% sure it works, but it seems to so far.

If you work with a domain of -180 to 180 degrees it will work better. So convert 355 to -5 and then continue along you’re original path.

If you limit the numerator to the range 0-359 in your original equation I think it will work. To do this use the modulus operator:


mid_angle = ((angle1 + angle2)%360)/2

Do you need an angle or do you need a vector?

The mathutils.Vector type carries the angle method, which calculates the angle between two vectors.

Thanks for the replies, guys.

andrew_101: Hmm, that doesn’t seem like it would really solve the problem. For instance, what if the two angles were -175 and 175?

Kupoman: I just tried that out, and it still seems to have the same problem as before.

Monster: I need an angle, but I can always convert from a vector if need be.

Goran: That’s not quite what I’m looking for.

Anyway, although my previous method was working fine, I found a much simpler and more elegant method here(the wording is kind of confusing, though):

I’ll mark this thread as solved, because that’s probably the best way to do it.