Converting (x, y) into NESW direction
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
...
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.
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];
}
}
}
}
}
That works much better Zekaric, much neater and well laid out too 
More people more ideas?

More people more ideas?
Sir, e^iπ + 1 = 0, hence God exists; reply!
This is rapidly turning into an obfuscated code contest.
We have a ways to go to reach ioccc.org status.
Possibly Related Threads...
| Thread: | Author | Replies: | Views: | Last Post | |
| Smooth acceleration in every direction. | Honey Sharma | 1 | 3,532 |
Aug 16, 2011 01:37 AM Last Post: PoseMotion |
|
| newbie request direction | Teehee | 3 | 5,437 |
Jul 27, 2011 09:57 AM Last Post: AndyKorth |
|
| give direction to sprite | termitis | 9 | 8,610 |
Mar 11, 2011 02:38 PM Last Post: compiler |
|
| Direction of normal | Miglu | 3 | 3,074 |
Oct 4, 2010 05:08 PM Last Post: Skorche |
|
| Direction formula? | TimMcD | 2 | 4,186 |
Nov 11, 2009 11:42 PM Last Post: TimMcD |
|

