Scale a value in a given range

-2

My programming teacher asked this question and I could not understand why I have to use one of these formulas to scale the temperature. And I also could not identify what the correct answer would be. You have the variable temperature, which ranges from -40 to +40. You want to change the RedValue variable ranging from 0 to 255 as the temperature goes from -40 to +40. What is the correct answer?

a.255*(temp+40)/80
b. 255*(temp-40)/40
c. 255-(temp+40)/80
d. 255*(temp-40)/80
e.255-(temp+40)/40
    
asked by anonymous 26.10.2017 / 02:55

2 answers

0

The answer in this case would be the a. alternative.

Well, if we make a basic division of the possible variations between corVermelha and temperatura we have that every variation of temperatura varies 255 / 80 = 3.1875 of corVermelha .

So, to get the value of corVermelha we need to know how much temperatura has been multiplied by this value. In case:

(temp + 40) //Aqui obtemos a variação onde temp é a temperatura atual 255/80 //O valor da variação de corVermelha 255/80*(temp + 40) // A resposta a.

    
26.10.2017 / 03:19
0

The idea is to make a ratio between the two scales, these being:

  • % with% to% with%
  • % with% to% with%

Notice that the scales do not have the same base, so we can adjust by adding 0 to the scale 255 to -40 that scales 40 to 40 .

With the two scales on the same base, divide the highest value of the two to give the ratio:

255 / 80 = 3,1875

We can say that the scale -40 to 40 is 0 times greater than the scale 80 to 0 . Then transforming a value from 255 to 3,1875 into this scale is only multiplying by 0 .

But since the values range from 80 to 0 you must first adjust the value in this scale from 80 to 3,1875 by adding -40 .

See the following examples:

  • Temperature 40:% with%
  • Temperature 10:% with%
  • Temperature -23:% with%
  • Temperature -40:% with%

If you notice the steps that have been done, you will see that it corresponds to answer A, which is correct.

    
26.10.2017 / 03:14