# Help with the math on this?

This topic is 1265 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I am not sure what this is even called in math terms, but here I go...

i have a range from 0 to 1

and I want to take the value of 1 and make that 0, and 0 and make that 1. and if I have .1 make it .9 and .9 make it .1

So I am not sure how to do this. And/or what it's called.

Thanks!

##### Share on other sites

It's called linear interpolation (Lerp).

Thanks

##### Share on other sites

The 2 points are p0 = 1 and p1 = 0 (values at time t = 0 and t = 1 respectively).

He mentioned specific values for t in his post (t = 0.1 and 0.9).

Lerp is the general case for this

p = (1 - t) * p0 + t * p1 = p0 - t * (p0 - p1)

Plugging in p0 = 1 and p1 = 0 we get

p = 1 - t(1 - 0) = 1 - t

which is what fir said too (with variables named differently, y == p and x == t)

##### Share on other sites

From reading his post, he wants 0 --> 1, 1 --> 0   and  0.1 --> 0.9 and 0.9 --> 0.1.

Indeed, y = 1.0 - x

You can use it to determine diagonals on slopes if you have square tiles, for example.

Edited by Kaptein

##### Share on other sites

I'd read his post again then ;)

He wants

0.0 -> 1.0

0.1 -> 0.9

0.9 -> 0.1

1.0 -> 0.0

##### Share on other sites

I agree with Diego that it's.. kind of a stretch to call this linear interpolation. It's just a simple complement, and just happens to be a special case of linear interpolation with a = 1, b = 0 (and probably a special case of plenty of other transforms). But anyway it's good to have a reference to it since most likely MARS_999 will need it soon

##### Share on other sites

Why stop at linear interpolation? We can complicate things further and think of it as polynomial interpolation: There is a unique polynomial of degree up to 3 such that f(0)=1, f(0.1)=0.9, f(0.9)=0.1 and f(1)=0.

##### Share on other sites

Well yeah, I assumed he wanted linear interpolation not polynomial.

Lagrange's Polynomial Interpolation Formulae will do that though. EDIT: Maybe Legendre not Lagrange ;)

##### Share on other sites

thanks for all the help guys!!!

##### Share on other sites

(imo) this is not interpolation, this is some specyfic case of linear transformation ( [0,1] -> [0,1] ) whose probably has some naming, though i dont know it

##### Share on other sites

Stop overly complicating it guys.

float getVal(float in)

{

return in==1.0f ? 0.0f : in==0.0f ? 1.0f : in==0.1f ? 0.9f : in==0.9f ? : 0.1f; --assumes infinite precision floating point, otherwise not possible to satisfy requirements

}

##### Share on other sites
I'm pretty sure that's not what the op wants, I expect they want 0.5 to map to 0.5 and 0.25 to map to 0.75, so f(t) = 1 - t is correct.

##### Share on other sites

I don't understand why some of you developers are so against 'generalisations', that is what you do all the time anyway.  Instead of bashing the people who mentioned LERP and made it more 'complicated', you should be grateful to learn something new, although more 'complicated'.  It is a good exercise to get better at what you are doing.   If you are ever going to translate between any other values, that is the formula you will need to to get the job done anyway.  It is seriously good to know, although it shouldn't be too hard to come up with that yourself with a bit of thinking.

It is not like the easy solution was not posted already.

Personally, I have used both LERP and 1-t a lot of times without even seeing the connection, so for me it was kind of useful.  I have actually 'invented' LERP a lot of times in my head already without even knowing the 'label' LERP.  I am sure anyone who has ever needed a fixed number of steps between two arbitrary points have done that.

By the way...  Using values between 0 and 1 is used in a lot of fields, one which is probability.  Me, for one, last time I used this kind of value was to multiply with sample data to make a volume control, (0 = 0, 1000000 = 1) so these kind of values crops up everywhere.

##### Share on other sites

I don't understand why some of you developers are so against 'generalisations', that is what you do all the time anyway.

I don't think anyone's against generalizing, per se. Personally, I feel that over-complicating the situation is adding nothing significant; otherwise, we could provide all sorts of methods to solve simple problems, like trying to tell our children to find the area of a square for their geometry homework by integrating.

However, there's something more to it:

I want to take the value

It sure sounds like he's not doing it out of curiosity, by his wording. It also sounds like he wants to program something to do it; otherwise, why ask, if he could do it by hand, and isn't asking simply to be scholarly?

The thing is, if he doesn't understand the algorithm for what he wants, and he's trying to implement it, then giving him a complex and inefficient way of solving the problem isn't the best answer, if he may be unable to derive the simpler answer for his use case. Linear interpolation is much slower than subtracting two numbers, so it is no better of an answer than the one already posted.

Of course, if Paradigm Shifter was attempting to build on fir's answer with more information for the sake of knowledge, then that's well and fine. However, providing a one line answer that may sound contradictory to someone who doesn't immediately see the link between the two creates confusion. If Paradigm Shifter's first two posts were combined into one, from the beginning, and underscored that the simplification was what the OP was after, and how we got there, there probably would have been less conflict.

All in all, with clarification in place, I agree with the above points.

##### Share on other sites

I gave a one line answer because the OP asked what it was called, fir had already given the specific answer and I just said what the technique was called in the general case ;)

##### Share on other sites

I am not sure how to go about setting up a method to determine a color value for each object that is unique. Obviously they need to be unique to determine which one you clicked on, but with only RGB 256 each channel just doing red++ for 256 values and moving to green++ is one way but how can I code it so it a rolling increment.

e.g.

start with red = 0, red++ when red hits 255 move to green, when green hits 255, blue is incremented to 255 then after that start with red = 1, green = 1, blue = 1 or other combos green = 1, blue = 1 red = 0?

Not sure I am making myself clear, but starting to think ray casting and AABB would just be easier in the long run?

THanks!

##### Share on other sites

I am not sure how to go about setting up a method to determine a color value for each object that is unique. Obviously they need to be unique to determine which one you clicked on, but with only RGB 256 each channel just doing red++ for 256 values and moving to green++ is one way but how can I code it so it a rolling increment.

e.g.

start with red = 0, red++ when red hits 255 move to green, when green hits 255, blue is incremented to 255 then after that start with red = 1, green = 1, blue = 1 or other combos green = 1, blue = 1 red = 0?

Not sure I am making myself clear, but starting to think ray casting and AABB would just be easier in the long run?

THanks!

For unique colors, you can simply have the color be a 32bit number, and add one when you need a new one. To extract the color, take the bottom eight bits as red, the next lowest eight bits as green, then the eight bits above that as blue. If you want the exact scheme as you've posted, that will require some more logic, but this is the fastest way.

##### Share on other sites

I don't know enough to make it work, but in pseudocode

static int _x = 0;

int[red, green, blue, alpha] GetNextColor()

{

//note, after 2^32 colors, this will fail. if you remove alpha, you only get 2^24 colors

_x++;

int alpha (_x >> 24) % 256 //Only take the last 8 bits

int red = (_x >> 16) % 256 //I think this means that you will ignore the first 16 bits of _x, and only look at the next 8 bits

int green = (_x >> 8) % 256 //Ignore the first 8 bits, and only look at the next 8 bits

int blue = _x % 256 //Only look at the first 8 bits

return [red, green, blue, alpha]

}

##### Share on other sites

I don't understand why some of you developers are so against 'generalisations', that is what you do all the time anyway.

I don't think anyone's against generalizing, per se. Personally, I feel that over-complicating the situation is adding nothing significant; otherwise, we could provide all sorts of methods to solve simple problems, like trying to tell our children to find the area of a square for their geometry homework by integrating.

However, there's something more to it:

I want to take the value

It sure sounds like he's not doing it out of curiosity, by his wording. It also sounds like he wants to program something to do it; otherwise, why ask, if he could do it by hand, and isn't asking simply to be scholarly?

The thing is, if he doesn't understand the algorithm for what he wants, and he's trying to implement it, then giving him a complex and inefficient way of solving the problem isn't the best answer, if he may be unable to derive the simpler answer for his use case. Linear interpolation is much slower than subtracting two numbers, so it is no better of an answer than the one already posted.

Of course, if Paradigm Shifter was attempting to build on fir's answer with more information for the sake of knowledge, then that's well and fine. However, providing a one line answer that may sound contradictory to someone who doesn't immediately see the link between the two creates confusion. If Paradigm Shifter's first two posts were combined into one, from the beginning, and underscored that the simplification was what the OP was after, and how we got there, there probably would have been less conflict.

All in all, with clarification in place, I agree with the above points.

##### Share on other sites

I don't know enough to make it work, but in pseudocode

static int _x = 0;

int[red, green, blue, alpha] GetNextColor()

{

//note, after 2^32 colors, this will fail. if you remove alpha, you only get 2^24 colors

_x++;

int alpha (_x >> 24) % 256 //Only take the last 8 bits

int red = (_x >> 16) % 256 //I think this means that you will ignore the first 16 bits of _x, and only look at the next 8 bits

int green = (_x >> 8) % 256 //Ignore the first 8 bits, and only look at the next 8 bits

int blue = _x % 256 //Only look at the first 8 bits

return [red, green, blue, alpha]

}

I am not sure what you are looking to do here? Is this allowing me to add one to the color palette and cycle all the way through rgb? so get all 16.7 million color variations?

If so thanks!

Update!

BedderDanu  thanks that worked great!!! Edited by MARS_999

##### Share on other sites

If you are not interested?

No one said I wasn't interested. To be honest, I never thought of it that way.

Never said that.

Of course, if Paradigm Shifter was attempting to build on fir's answer with more information for the sake of knowledge, then that's well and fine.

I have no idea where you are getting the idea that I am against his insight.

##### Share on other sites

This topic is 1265 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Create an account

Register a new account

• ### Forum Statistics

• Total Topics
628653
• Total Posts
2984051

• 10
• 9
• 9
• 10
• 21