This one is, or rather, should be, fairly simple. I have a list of tuples of XY positions, and am trying to pull out angles relative to the origin from it. However, unlike
atan2 / friends, I want it to be continuous, in other words not wrapping. I know (and am assuming) that the differences between successive XY coordinates are small, and that there is a minimum radius. (In other words, it should pick the multiple of \$2\pi\$ that makes the difference between the previous angle and the current angle the smallest.)
Currently, this is the relevant snippet:
thetas = [math.atan2(coords, coords)] prev = thetas for index in range(1, len(coords)): coord = coords[index] theta = math.atan2(coord, coord) + (prev // (2*math.pi)) * (2 * math.pi) while theta - prev > math.pi: theta -= 2*math.pi while prev - theta > math.pi: theta += 2*math.pi assert abs(theta - prev) < math.pi/2 thetas.append(theta) prev = theta
However, it's slow, and I doubt it's Pythonic. It's also doing a lot of "magic", for lack of a better word. Any suggestions for improvement?
Line profiler results here.