Skip to content

Instantly share code, notes, and snippets.

@vlevit
Last active August 29, 2015 14:06
Show Gist options
  • Save vlevit/c5f8dfc413adccbde740 to your computer and use it in GitHub Desktop.
Save vlevit/c5f8dfc413adccbde740 to your computer and use it in GitHub Desktop.
mean angle
#!/usr/bin/python2
import sys
def mean_angle(angles):
edges = [a - angles[i] for i, a in enumerate(angles[1:])] + [360 - angles[-1] + angles[0]]
maxedge = max(edges)
start = (edges.index(maxedge) + 1) % len(edges)
angles2 = angles[:start] + [a - 360 for a in angles[start:]]
mean = float(sum(angles2)) / len(angles)
if mean < 0:
mean += 360
return mean
if __name__ == '__main__':
if len(sys.argv) < 2:
print("usage: meanangle ANGLES")
raise SystemExit(2)
angles = sorted(map(int, sys.argv[1:]))
mean = mean_angle(angles)
print("mean angle: {}".format(mean))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment