Converting (x, y) into NESW direction

Member
Posts: 131
Joined: 2004.10
Post: #16
TWO looks wrong. It reduces to y>-x.

FOUR also looks wrong as it's the same as THREE.

Basically you are dealing with slopes as well but just slightly differently.
y = Mx;
M = .4142

Line 1: y = x/M
Line 2: y = -x/M
Line 3: y = Mx
Line 4: y = -Mx

North: y > x/M and y > -x/M
South: y < x/M and y < -x/M
East: Mx < y < -Mx
West: -Mx < y < Mx
...
Code:
typedef enum                     { WST, SWST, STH, SEST, EST, NEST, NTH, NWST } DIRECTION_INDEX;
const char *DIRECTION_NAMES[8] = { "W", "SW", "S", "SE", "E", "NE", "N", "NW" };

#define GT1 (y> x/M)
#define GT2 (y>-x/M)
#define GT3 (y> x*M)
#define GT4 (y>-x*M)

const char *direction2(double x, double y)
{
   if (GT1) {
      /* possibles N, NW, W, SW */
      if (GT2) {
         /* N */
         return DIRECTION_NAMES[NTH];
      }
      else {
         /* possibles NW, W, SW */
         if (!GT3) {
            /* SW */
            return DIRECTION_NAMES[SWST];
         }
         else {
            /* possibles NW, W */
            if (GT4) {
               return DIRECTION_NAMES[NWST];
            }
            else {
               return DIRECTION_NAMES[WST];
            }
         }
      }
   }
   else {
      /* Possibles S, SE, E, NE */
      if(!GT2){
         /* S */
         return DIRECTION_NAMES[STH];
      }
      else {
         /* Possibles SE, E, NE */
         if (GT3) {
            return DIRECTION_NAMES[NEST];
         }
         else {
            /* Possibles SE, E */
            if (GT4){
               return DIRECTION_NAMES[EST];
            }
            else{
               return DIRECTION_NAMES[SEST];        
            }
         }
      }
   }
}
like my if's, the some of the else's are not necessary as the returns will avoid them. Not sure if it'll make a lick of difference if speed is the issue. Then again the mutiplies and divides may hammer this for speed.
Quote this message in a reply
DoG
Moderator
Posts: 869
Joined: 2003.01
Post: #17
This is all just very, very weird...
Quote this message in a reply
Sage
Posts: 1,403
Joined: 2005.07
Post: #18
That works much better Zekaric, much neater and well laid out too Wink
More people more ideas?

Sir, e^iπ + 1 = 0, hence God exists; reply!
Quote this message in a reply
Sage
Posts: 1,199
Joined: 2004.10
Post: #19
This is rapidly turning into an obfuscated code contest.
Quote this message in a reply
Member
Posts: 131
Joined: 2004.10
Post: #20
We have a ways to go to reach ioccc.org status.
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  Direction of normal Miglu 3 3,683 Oct 4, 2010 05:08 PM
Last Post: Skorche
  Direction formula? TimMcD 2 5,381 Nov 11, 2009 11:42 PM
Last Post: TimMcD
  Converting Int To String Nick 4 14,292 Jun 2, 2005 02:13 AM
Last Post: hangt5
  converting xyz rotation to something else reubert 10 6,009 Mar 5, 2005 07:33 PM
Last Post: Puzzler183
  Converting strings to functions Joseph Duchesne 10 5,509 Feb 23, 2005 11:42 PM
Last Post: Skorche