Hi Thomas
> (unsigned char)(atan2((double) (x - me->p_x),
> (double) (me->p_y - y)) / 3.141592 * 128.);
>
> I am fairly certain per-C99 that the result of casting a negative floating
> point to any unsigned type is undefined, so exactly what happens may vary
> by architecture and compiler.
"If the value of the integral part cannot be represented by the
integer type, the behavior is undefined. … The remaindering operation
performed when a value of integer type is converted to unsigned type
need not be performed when a value of real floating type is converted
to unsigned type."
Solution:
(unsigned char)(int)(atan2((double) (x - me->p_x), (double)
(me->p_y - y)) / 3.141592 * 128.)
But the value of pi is incorrectly rounded and those double casts are
unnecessary; in any case we clearly do not need double precision:
(unsigned char)(int)(atan2f((x - me->p_x), (me->p_y - y)) / 3.141593 * 128.)
> I am not certain how the course system works, but guessing from the fact
> that it needs to fit in an unsigned char, I am thinking that 127 + the
> absolute value would be a good guess
> (1) Does all that sound correct?
Not quite. Due north 0.0 maps to 0, not 127.
> (2) How is the unsigned char-based course system expected to work?
> 360 degrees carved into 255 slices? Or Something else?
It is 360 degrees unequally divided into 256 slices: "0" has twice the
normal width and "180" has zero width.
Fixing this requires rounding rather than truncation. With C99 and
IMO clearer layout:
(unsigned char) (int) ( roundf( 128.f*atan2f( dx, dy )/3.141593f ) )
Cheers
Michael