I suck at math and feel like this should be easy but I want it to be elegant instead of a hack.
I have a 360 degree angle that needs to be calculated on to a 255 degree freedom of rotation. Person enters angle, outputs accurately to a 255 degree circle.
int north = 90; float outputNorth = north/360f * 255f; //Results: 90 angle-> 63 angle But it needs to be:
int north = 90; float outputNorth = north/xf* yf; //Results: 90 angle -> 128 angle Problem: Coords are a little weird for the 255 degree of rotation. Not sure how to translate it.
Should be the following: //90 -> 128 //0 -> 192 //270 -> 0 //180 -> 64 
1 Answer
Your math has some rounding problems. I would use a different mapping where 360 deg would be 256 -> 0 instead of 255. Otherwise your 8bit values would have the same angle 0 stored twice and it would most likely cause other problems latter.
Here conversion to/from deg/8bit using linear interpolation:
int i; // angle [deg int] int o; // 0..255 [8bit uint] o=(192-((i<<8)/360))&255; i=((192-o)*360)>>8; Here the values:
i o 0 192 90 128 180 64 270 0 360 192 The i can be any even negative or bigger than 360 deg the conversion will still work...