Search code examples
offsetfractions

Descending offset fraction method


for(int i = 0; i <= 3; i++)
{
   float offsetFraction = ((float)(i+1))/(4);
}

gives 0.25, 0.5, 0.75, 1 respectively.

What I would like to get is in the order; 1, 0.75, 0.5, 0.25

I could not figure out the required code to get it. Can somebody have an idea how to get these values respectively? Thanks.


Solution

  • Use:

    for(int i = 3; i >= 0; i--)
    {
       float offsetFraction = ((float)(i+1))/(4);
    }
    

    This starts with the value 3 for i and decrements it at each loop iteration.

    Unless you are using i for something else, this is simpler to understand:

    for(int i = 4; i >= 1; i--)
    {
       float offsetFraction = ((float)i)/(4);
    }
    

    As you commented that you are using i for something else, perhaps you want the value 0 for i correspond to 0.75 for offsetFraction, 1 correspond to 0.5, and so on:

    for(int i = 0; i <= 3; i++)
    {
       float offsetFraction = ((float)(4-i))/(4);
    }