Jump to content

  • Log In with Google      Sign In   
  • Create Account

Help with the math on this?


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
24 replies to this topic

#1 MARS_999   Members   -  Reputation: 1297

Like
0Likes
Like

Posted 12 June 2014 - 03:08 AM

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!



Sponsor:

#2 fir   Members   -  Reputation: -456

Like
13Likes
Like

Posted 12 June 2014 - 03:14 AM

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!

it would be  y= 1.0 - x 



#3 Paradigm Shifter   Crossbones+   -  Reputation: 5433

Like
1Likes
Like

Posted 12 June 2014 - 03:46 AM

It's called linear interpolation (Lerp).


"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley

#4 MARS_999   Members   -  Reputation: 1297

Like
0Likes
Like

Posted 12 June 2014 - 04:29 AM

Thanks



#5 DiegoSLTS   Members   -  Reputation: 1867

Like
8Likes
Like

Posted 12 June 2014 - 06:50 AM

It's called linear interpolation (Lerp).

Really? It sounds like just a complement like fir said. Linear interpolation requires 2 points (may be farther than 1 unit from the origin) and a delta between 0 and 1, and the original post doesn't name any of that.



#6 Paradigm Shifter   Crossbones+   -  Reputation: 5433

Like
2Likes
Like

Posted 12 June 2014 - 07:00 AM

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)


"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley

#7 Kaptein   Prime Members   -  Reputation: 2179

Like
0Likes
Like

Posted 12 June 2014 - 07:09 AM

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, 12 June 2014 - 07:15 AM.


#8 Paradigm Shifter   Crossbones+   -  Reputation: 5433

Like
0Likes
Like

Posted 12 June 2014 - 07:13 AM

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


"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley

#9 Bacterius   Crossbones+   -  Reputation: 9268

Like
4Likes
Like

Posted 12 June 2014 - 07:19 AM

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 wink.png


The slowsort algorithm is a perfect illustration of the multiply and surrender paradigm, which is perhaps the single most important paradigm in the development of reluctant algorithms. The basic multiply and surrender strategy consists in replacing the problem at hand by two or more subproblems, each slightly simpler than the original, and continue multiplying subproblems and subsubproblems recursively in this fashion as long as possible. At some point the subproblems will all become so simple that their solution can no longer be postponed, and we will have to surrender. Experience shows that, in most cases, by the time this point is reached the total work will be substantially higher than what could have been wasted by a more direct approach.

 

- Pessimal Algorithms and Simplexity Analysis


#10 Álvaro   Crossbones+   -  Reputation: 13905

Like
4Likes
Like

Posted 12 June 2014 - 09:09 AM

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.



#11 Paradigm Shifter   Crossbones+   -  Reputation: 5433

Like
0Likes
Like

Posted 12 June 2014 - 09:30 AM

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

 

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


Edited by Paradigm Shifter, 12 June 2014 - 09:33 AM.

"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley

#12 MARS_999   Members   -  Reputation: 1297

Like
2Likes
Like

Posted 12 June 2014 - 10:04 AM

thanks for all the help guys!!!



#13 fir   Members   -  Reputation: -456

Like
0Likes
Like

Posted 12 June 2014 - 11:13 AM

(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



#14 Waterlimon   Crossbones+   -  Reputation: 2635

Like
0Likes
Like

Posted 12 June 2014 - 11:55 AM

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

}


o3o


#15 Paradigm Shifter   Crossbones+   -  Reputation: 5433

Like
0Likes
Like

Posted 12 June 2014 - 01:17 PM

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.
"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley

#16 aregee   Members   -  Reputation: 1026

Like
0Likes
Like

Posted 12 June 2014 - 01:52 PM

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.



#17 Ectara   Crossbones+   -  Reputation: 3057

Like
0Likes
Like

Posted 15 June 2014 - 09:51 PM

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.



#18 Funkymunky   Members   -  Reputation: 685

Like
5Likes
Like

Posted 15 June 2014 - 10:09 PM

I am astounded at how many replies there are to this question



#19 Paradigm Shifter   Crossbones+   -  Reputation: 5433

Like
0Likes
Like

Posted 16 June 2014 - 01:26 AM

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 ;)


"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley

#20 MARS_999   Members   -  Reputation: 1297

Like
0Likes
Like

Posted 16 June 2014 - 12:14 PM

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!






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS