Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

interpolation of number in proportion function

Discussion in 'Scripting' started by like_a_bosss, Sep 20, 2015.

  1. like_a_bosss

    like_a_bosss

    Joined:
    Mar 18, 2015
    Posts:
    56
    hi i have a number that interpolates from 0 to 0.72
    I would like to have interpolation funcion where 0.72 would return for some other varible =100,
    when 0.71 return around 98 and that number would go to zero when that 0.72 would be going to zero

    0<------->0.72
    ==
    0<----------->100
    is there any allready build in function? thx in advance
     
    Last edited: Sep 20, 2015
  2. Alanisaac

    Alanisaac

    Joined:
    Jan 25, 2015
    Posts:
    15
    double Interpolate(double min, double max, double value)
    {
    return (value - min) / (max - min)
    }

    Where in your example, min = 0, max = 0.72, value = 0.71 and it should return 98.61111111.

    If you want this to be a round integer, you can have the return type be an int, and use a floor, ceiling, or round function to get an integer.
     
  3. like_a_bosss

    like_a_bosss

    Joined:
    Mar 18, 2015
    Posts:
    56
    in update
    chwilowaLiczba=Interpolate(0,0.72,1);

    function Interpolate(min:double, max:double,valuee:double)
    {
    valuee =(valuee - min) / (max - min);
    return valuee;


    It returns.1.3
     
  4. like_a_bosss

    like_a_bosss

    Joined:
    Mar 18, 2015
    Posts:
    56
    function Interpolate(x:double)
    {
    x=100*(interpolatedNumber*10)/72;
    return x;
    }

    but this works, 72 is range 0.72
     
  5. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,380
    Yeah, 1 is larger that .72... it's 30% larger.
     
  6. like_a_bosss

    like_a_bosss

    Joined:
    Mar 18, 2015
    Posts:
    56
    Code (JavaScript):
    1. function Interpolate(multiculti:double,x:float)
    2. {
    3. x=100*(mojeG)/73;
    4. multiculti=100-(x*100)+0.6;
    5. multiculti.ToString("F2");
    6. return multiculti;
    7. }
    in update
    CountryMultiCultiLevel=Interpolate(0,0);
    multiculty=CountryMultiCultiLevel.ToString("F2");

    this will take interpolated value from 0 to 0.72 as mojeG, then its converted to 0 to 72 then it will Applay that change in interpolation in range 0,100, then its inverted for my own purpose the higher number in range 0,100 the lowest
    CountryMultiCultiLevel. tested and it works

    BTW 1 is larger than 72 by more then 30% of 72
    0.3 is 30% from 1, 30% of 72=21--->72+21=93
     
  7. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,380
    Yeah, you must not have printed out the correct result. Because I just ran it and the formula spits back 1.388888...

    Which it is.

    I also have no idea what this means:

    But hey, if it gets you what you need... have at it.
     
  8. like_a_bosss

    like_a_bosss

    Joined:
    Mar 18, 2015
    Posts:
    56
    I dynamicly change one of values of Color(X,MyColor,X,X); variable MyColor is being interpolated from 0 to 0.72 dynamicly
    I wanted to to use interpolation of value MyColor to interpolate the variable double "Multi_culti_level"
    in range from 0 to 100 that would correspond to the change in MyColor variable

    After it was achieved I inverted interpolation, so the bigger value of MyColor(0 to 0.72) the lower Multi_culti_level value from 0,100 is:) maybe what was written before was a bit unclear XD
     
    Last edited: Sep 20, 2015