Scalate a value in a given range

Asked

Viewed 39 times

-2

My programming teacher asked this question and I couldn’t understand why I have to use one of these formulas to scale the temperature. And I couldn’t identify the right answer either. You have the temperature variable, which goes from -40 to +40. You want to change the variable colorVermine that goes from 0 to 255 as the temperature goes from -40 to +40. Which 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

2 answers

0

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

  • 0 to 255
  • -40 to 40

Note that the scales do not have the same basis, and so we can adjust by adding 40 to a scale -40 to 40 that gives a scale 0 to 80.

With the two scales on the same basis the highest value of the two is divided to give the ratio:

255 / 80 = 3,1875

We can say that the scale 0 to 255 is 3,1875 times larger than the scale 0 to 80. Soon transform a value of 0 to 80 for that scale is just multiply by 3,1875.

But how the values go from -40 to 40 it is first necessary to adjust the value to this scale of 0 to 80 adding up 40.

See the following examples:

  • Temperature 40: (40+40) * 3,1875 = 255
  • Temperature 10: (40+10) * 3,1875 = 158,75
  • Temperature -23: (40-23) * 3,1875 = 31,875
  • Temperature -40: (40-40) * 3,1875 = 0

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

  • Excellent explanation! Gave to understand the step by step! It was very clear now!

0


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

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

Therefore, to obtain the value of corVermelha we need to know about temperatura varied multiplied by this value. In the 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.

  • Now I understand and it makes a lot of sense. Excellent explanation!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.