
July 24, 2004
12:07 p.m.
I cant' resist either. Here is the way I would do it. def degreesToDirection(deg): dirs = ['N', 'NE', 'E', 'SE', 'S', 'SW', 'W', 'NW', 'N'] ix = (int(deg) + 23) / 45 return dirs[ix] To test: for deg in [0, 21, 22, 45, 66, 67, 90, 135, 180, 225, 270, 315, 360]: print deg, degreesToDirection(deg) 0 N 21 N 22 NE 45 NE 66 NE 67 E 90 E 135 SE 180 S 225 SW 270 W 315 NW 360 N By the way this points out that the cutoff points in the original post are not evenly spaced and probably not what is wanted. It's easy to have Python tell you the correct cutoffs:
[ (45*i)+22 for i in range(8) ] [22, 67, 112, 157, 202, 247, 292, 337]
Kent
ps. I can't help thinking there may be a more elegant way of doing the bearing conversion.
7542
Age (days ago)
7542
Last active (days ago)
0 comments
1 participants
participants (1)
-
Kent Johnson