Last active
August 29, 2015 14:06
-
-
Save vlevit/c5f8dfc413adccbde740 to your computer and use it in GitHub Desktop.
mean angle
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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