[Edu-sig] problem with IF

Kent Johnson kent37 at tds.net
Sat Jul 24 18:07:09 CEST 2004


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.
> 



More information about the Edu-sig mailing list