Question

I have a image which contains 40 rotated images.

With image index starting at 0. 0-39 actually.

Here is the code which transforms 0-39 to degrees

int image_direction = 0; //Can be 0-39
int facing_degrees = (int)(360.0 * (-(image_direction- 10.0))/40.0);
while(facing_degrees < 0)
    facing_degrees += 360;
while (facing_degrees > 360)
    facing_degrees -= 360;

So yeah it can also give out negative degrees as well as degrees over 360. So thats why there is the 2 while loops.

Now I wish to reverse this process say I specify 90 degrees I would like to get back 0..

I was thinking of doing something like

 if(degrees == 90 || degrees >= 80 && degrees <= 99)
    image_direction = 0;
 elseif(degrees == 100 || degrees >= 91 && degrees <= 109)
    image_direction = 39;
//etc.......

Well I'm not good at mathematics, well I forgot about this kind of stuff.

I'm wondering how can you reverse the function that gives degrees from image_direction to run backwards in a plain equation to avoid the huge case of if statements.

Here is some results rotation results

Image Index = 0 Image Degree = 90
Image Index = 1 Image Degree = 81
Image Index = 2 Image Degree = 72
Image Index = 3 Image Degree = 63
Image Index = 4 Image Degree = 54
Image Index = 5 Image Degree = 45
Image Index = 6 Image Degree = 36
Image Index = 7 Image Degree = 27
Image Index = 8 Image Degree = 18
Image Index = 9 Image Degree = 9
Image Index = 10 Image Degree = 0
Image Index = 11 Image Degree = 351
Image Index = 12 Image Degree = 342
Image Index = 13 Image Degree = 333
Image Index = 14 Image Degree = 324
Image Index = 15 Image Degree = 315
Image Index = 16 Image Degree = 306
Image Index = 17 Image Degree = 297
Image Index = 18 Image Degree = 288
Image Index = 19 Image Degree = 279
Image Index = 20 Image Degree = 270
Image Index = 21 Image Degree = 261
Image Index = 22 Image Degree = 252
Image Index = 23 Image Degree = 243
Image Index = 24 Image Degree = 234
Image Index = 25 Image Degree = 225
Image Index = 26 Image Degree = 216
Image Index = 27 Image Degree = 207
Image Index = 28 Image Degree = 198
Image Index = 29 Image Degree = 189
Image Index = 30 Image Degree = 180
Image Index = 31 Image Degree = 171
Image Index = 32 Image Degree = 162
Image Index = 33 Image Degree = 153
Image Index = 34 Image Degree = 144
Image Index = 35 Image Degree = 135
Image Index = 36 Image Degree = 126
Image Index = 37 Image Degree = 117
Image Index = 38 Image Degree = 108
Image Index = 39 Image Degree = 99
Image Index = 0 Image Degree = 90
Image Index = 1 Image Degree = 81
Image Index = 2 Image Degree = 72
Image Index = 3 Image Degree = 63
Image Index = 4 Image Degree = 54
Image Index = 5 Image Degree = 45
Image Index = 6 Image Degree = 36
Image Index = 7 Image Degree = 27
Image Index = 8 Image Degree = 18
Was it helpful?

Solution

From your mapping of input to outputs, I pasted these into Excel and worked out that the formula to calculate degrees is as follows:

RawDegrees = 90 - (index * 9)

Then clipping this to 0...360 (wrap around using the while loop) you get your final degrees output. The formula to invert this is found by rearranging the above to make Index the subject

    RawDegrees - 90 = - (index * 9) --- 1
    RawDegrees/9.0 - 10 = -index    --- 2

    Therefore
    index = 10 - Degrees/9.0

This will give you the following mapping

    Degree  Discovered Index
    90      0
    81      1
    72      2
    63      3
    54      4
    45      5
    36      6
    27      7
    18      8
    9       9
    0       10
    351     -29
    342     -28
    333     -27
    324     -26
    315     -25
    306     -24
    297     -23
    288     -22
    279     -21
    270     -20
    261     -19
    252     -18
    243     -17
    234     -16
    225     -15
    216     -14
    207     -13
    198     -12
    189     -11
    180     -10
    171     -9
    162     -8
    153     -7
    144     -6
    135     -5
    126     -4
    117     -3
    108     -2
    99      -1
    90      0
    81      1
    72      2
    63      3
    54      4
    45      5
    36      6
    27      7
    18      8

All you need to do is perform a wrap around to clip the answer to 0..39, e.g.

index = index < 0 ? index + 40 : index;

to get the correct output.

Incidentally, your index to degrees code may be factorized as follows to get the same output (valid for inputs 0..39)

int facing_degrees = (int)(90.0 - (image_direction * 9.0));    
facing_degrees = facing_degrees < 0 ? facing_degrees + 360 : facing_degrees;

OTHER TIPS

hmm why not use something similar to the "percentage formula" with a little tweaking:

image_direction = (int)(((float)(degrees-90) / 360) * 40) % 40;
if (image_direction < 0)
    image_direction += 40; // for negative integers

Breaking down the formula; we divide degrees by 360 and multiply by 40 to get the correct image given a specified degree, we then use the modulo operator to keep the image_direction in the range 0-39 for degrees > +360 or < -360 and finally, if the degree was a negative one image_direction will turn out to be negative therefore adding 40 to it will correct the image_direction.

Hope this helps.

Edit: Oh sorry, I seemed to have missed the part about a degree of 90 == image index of 0, easy enough to add in here, just subtract 90 from the actual degree (code edited above)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top