Skip to content
Darrin W. Cullop edited this page Feb 24, 2016 · 1 revision

Helper Code

Here are some code snippets that proved useful.

Miscellaneous Stuff

// Utility #define to ensure proper rounding
#define Round(x) (((x)>0.0f)?(int)(0.5f+(x)):-(int)(0.5f-(x)))

Conversion Functions

Conversion between Coordinates and Longs

To convert from a single coordinate (a float) to a four byte long, use this function:

long CoordToLong(const float nCoord)
{
    return (long)Round(nCoord * 8.0)
}

To convert from a four byte long to a single coordinate (a float), use this function:

float LongToCoord(const long nConvert)
{
     return ((float)nConvert / 8.0)
}

Conversion between Angles and Shorts

To convert from an angle (a float) to a two byte short, use this function:

short AngleToShort(const float nAngle)
{
     return (short)Round(nAngle * (65535.0 / 360.0))
}

To convert from a two byte short to an angle (a float), use this function:

float ShortToAngle(const short nConvert)
{
     return ((float)nConvert * (360.0 / 65535.0))
}

Conversion between Angles and Bytes

To convert from an angle (a float) to a byte, use this function:

char AngleToByte(const float nAngle)
{
     return (char)Round(nAngle * (255.0 / 360.0))
}

To convert from byte to an angle (a float), use this function:

float ByteToAngle(const char nConvert)
{
    return ((float)nConvert * (360.0 / 255.0))
}