Function to change inputs from 0.5 to 1, to 0 to 1

456 Views Asked by At

Let's say we have x, which is any decimal number between 0.5 and 1.

What function can be applied to it such that an input of 0.5 equals 0 and input of 1 equals 1? It should be linear.

I tried something like this:

f(x) => x / 2 + 0.5

Which works for 1, but not 0.5. Apparently my math skills are lacking... What is the approach to figure out problems like this in general?

PS - for the curious, this is the value of the opacity of a label while paging items in an iPad app.

2

There are 2 best solutions below

0
On BEST ANSWER

Suppose the function we want to find is $f(x)$. Since it should be linear, we have $f(x)=ax+b$. Suppose we want $f(x_0)=y_0$ and $f(x_1)=y_1$. That can be rewritten to $$ ax_0+b=y_0\\ ax_1+b=y_1 $$ Solving that for $a$ and $b$ yields $$ a=\frac{y_0-y_1}{x_0-x_1}\\ b=y_0-ax_0 $$ In your case, we have $f(\frac 12)=0$ and $f(1)=1$. That gives $$ a=\frac{1-0}{\frac 12-0}=\frac 1{\frac 12}=2\\ b=0-2\frac 12=-1\\ f(x)=ax+b=2x-1 $$

0
On

First look at $h(x)=x-\frac{1}{2}$. That will make the input go from $0$ to $\frac{1}{2}$. So what you want is actually

$$g(x)=2\cdot h(x)=2x-1.$$