Is there a simple function mapping -1 to 1 and 1 to 0

88 Views Asked by At

This is a rather simple question.

I'm writing a computer program, and I have a variable for velocity, which takes values $\pm 1$. I also have corresponding sprites, facing both right and left, on rows 0 and 1 of a spritesheet. Funny enough, I got the rows on the sheet backwards, so the leftwards is in the first row, and rightwards in the second row. What I'm trying to do is map -1 to 1 and 1 to 0.

So my question is: is there a simple function $f$ constructed from standard computer math like addition, subtraction, multiplication, division, exponentiation, absolute value, floors and ceilings that satisfies $f(-1) = 1$ and $f(1) = 0$.

3

There are 3 best solutions below

0
On BEST ANSWER

We have the points $(-1,1)$ and $(1,0)$. We can simply construct the line that goes through these points:

$y=\frac{1-x}{2}$

Does that work for your purposes?

0
On

One solution possible might be $f(x) = |(\text{floor}\frac{x}{2})|$

0
On

Since you said computer program, you may also use $$f(x) = 0 ^ {1 + x}.$$