Relating to this question; What's the best way of transforming a 2D vector into the closest 8-way compass direction?

My query is if this would work in the following order(below) and if so, how would i achieve that?

 enum compassDir {
    S = 0, SE = 1,
    E = 2, NE = 3,
    N = 4, NW = 5,
    W = 6, SW = 7
  • \$\begingroup\$ -1 Are you asking for clarification to the answer of another question? Write a comment instead. \$\endgroup\$ – Anko Jul 19 '13 at 11:00
  • \$\begingroup\$ Your starting angle should have an offset. In 2D that would be +90 degrees. or +PI/4 radians. Since south is pointing downwards. \$\endgroup\$ – Sidar Jul 19 '13 at 11:31
  • 1
    \$\begingroup\$ I feel like this question could easily be answered by trying it. This is almost entirely a coding challenge. The answer I gave goes clockwise around the compass, so the enums are ordered in that way. You have your enums with a different offset and order. Think about how you might reverse the order the while loop traverses the compass. Yes it's possible, but I'm not going to write the code for you :) I agree with Anko, this should be a comment on the original post. \$\endgroup\$ – MichaelHouse Jul 19 '13 at 12:57
  • \$\begingroup\$ Yeah sorry guys, dumb question. I had it the right code all along but after the clarification by Sidar, i realised i had made a typo when the direction gets set. Shit happens :p \$\endgroup\$ – dan369 Jul 20 '13 at 11:20

The unit vectors for the eight compass directions can easily be hardcoded into an array in the same order as your enum. Then one simply uses the dot-(or inner-)product of the given vector against the vector array. The closest compass direction will yield the maximal dot-product value.

| improve this answer | |

Not the answer you're looking for? Browse other questions tagged or ask your own question.