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.
Moderator
Posts: 869
Joined: 2003.01
Post: #17
This is all just very, very weird...
Sage
Posts: 1,403
Joined: 2005.07
Post: #18
That works much better Zekaric, much neater and well laid out too
More people more ideas?

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