I know there has been discussion about this a few times, but I still can’t seem to figure out how to combine a horrid string of if-then statements into one that works for what I’m doing.
I am essentially trying to convert a list of numbers (ranging from 0-360) into 16 different cardinal directions. So im trying to make a string of If/then statements that convert the range of numbers into N, NNE, NE, ENE, etc.
There must be a better way to do this lol but right now all I can think of is a string of If-then statements, such as: If (((x > 0) and (x < 22.5)), “NNE”, x)
compass_rose_2019Jun19a.gh (15.5 KB) (deprecated, see below)
On second thought… I see a rounding (or “Floor”?) error in this… and a flaw in the spec.
OK, got a little ugly to fix that (purple group) but now it works correctly. Headings 11 degrees plus or minus of North (for example) will be assigned the label “North”, as with all other directions. South is 169 (180-11) through 191 (180+11) degrees. [[360 / 16 labels / 2 (+/-) = +-11.25 degrees of each labeled direction]]
Joseph, this is amazing. Thank you. You were right to make the direction ±11.25 degrees! I wasn’t sure how to formulate that into a domain because i didn’t know how to translate - degrees from the given 0-360 inputs, but you managed to figure it out. Thanks so much
I’m a sailor so the defect was obvious. Notice that I use the ‘F’ output (Floor) of Round, which is an important detail. Using ‘N’ (Nearest) is wrong there.
P.S. Here is a much simpler version where ‘Nearest’ is used indirectly. The ‘i’ input to List Item rounds to nearest integer (and handles Modulus and negative value wrapping!) to automagically implement the ±11.25 degree issue:
You can do this with a Python component, something like this (not tested!)
ÉDIT : forgot the modulo!
from __future__ import division
directions= 'N NNE NE ENE E ESE SE SSE S SSW SW WSW W WNW NW NNW'.split()
bin_size = 360/ len(directions)
a = list()
for value in x:
index = int(round( value/bin_size )) % len(directions)
a.append(directions[index])
I do believe the better choice is using Python . Like @Dancergraham posted.
There are countless possibilities to overcome the rounding issue, like what if you convert the angle into cartesian coordinates (polar to cartesian) this way you can better make the switch because you have two variables that get positive and negative values.