|
Author |
Topic: Mixing Two Numbers (Read 764 times) |
|
skloopy
|
Mixing Two Numbers
« on: Apr 30th, 2004, 3:37am » |
|
Hey everybody. I've been trying to figure this one out for a while and i'm pretty stumped, so i thought i'd see if anybody knows the answer.. I'm trying to mix two numbers together based on a float value beteen 0 and 1. I found this method by Toxi, where a and b are the numbers to mix and f is the weight value. Code:float mix(float a,float b, float f) { return a+(b-a)*f; } |
| It mixes two positive floats, but it doesn't work if either number is negative. Anyone know how to do that?
|
« Last Edit: Apr 30th, 2004, 3:38am by skloopy » |
|
|
|
|
justo
|
Re: Mixing Two Numbers
« Reply #1 on: Apr 30th, 2004, 6:11am » |
|
thats whats known as a linear interpolation, commonly shortened to a "lerp" (impress your friends!), and that formula is correct. for instance, to get a fourth of the way in between -4 and 5: Code: a = -4 b = 5 f = .25 (-4) + (5 - (-4)) * .25) = (-4) + (9 * .25) = (-4) + 2.25 = -1.75 |
| you can think of it as taking the original number as a starting point and adding a fraction of the distance from the first number to the second one. the forumla is also equivalent to ((1 - f)*a) + (f*b) so you are basically taking a fraction of each number and adding them together...like a weighted average. its just rearranged to avoid an extra multiply. hopefully that helps.
|
|
|
|
skloopy
|
Re: Mixing Two Numbers
« Reply #2 on: Apr 30th, 2004, 9:37am » |
|
Thanks justo! okay i just figured out i was doing the maths wrong.. okay it must be something i'm doin wrong somewhere else Thanks for the help jeez im a dummy
|
« Last Edit: Apr 30th, 2004, 9:43am by skloopy » |
|
|
|
|
fry
|
Re: Mixing Two Numbers
« Reply #3 on: Apr 30th, 2004, 7:27pm » |
|
sounds like a lerp(a, b, f) function might be a good idea..
|
|
|
|
mflux
|
Re: Mixing Two Numbers
« Reply #4 on: May 3rd, 2004, 10:27am » |
|
ben is so smart
|
|
|
|
arielm
|
Re: Mixing Two Numbers
« Reply #5 on: May 3rd, 2004, 12:08pm » |
|
on Apr 30th, 2004, 7:27pm, fry wrote:sounds like a lerp(a, b, f) function might be a good idea.. |
| good idea, but if you call it lerp(), you'll be a good candidate for the geek-god annual prize (stealing toxi's place)! interpolate() could save your soul for a while
|
Ariel Malka | www.chronotext.org
|
|
|
fry
|
Re: Mixing Two Numbers
« Reply #6 on: May 4th, 2004, 8:28pm » |
|
hm, i'll do anything i can to go after toxi for the geek god prize. though to be honest i can't claim too much geekiness for it.. "lerp" is actually a heavily used term in computer graphics. besides, would you rather type interpolate() or lerp()?
|
|
|
|
mKoser
|
Re: Mixing Two Numbers
« Reply #7 on: May 5th, 2004, 12:53am » |
|
(what would make most sense to someone who's a computer graphics virgin?... ie. in the target group of processing!) i'd ask ... what would you rather teach?
|
mikkel crone koser | www.beyondthree.com | http://processing.beyondthree.com
|
|
|
justo
|
Re: Mixing Two Numbers
« Reply #8 on: May 5th, 2004, 1:30am » |
|
well, maybe the question is better if you posed it like this: which would you rather type linearInterpolate() or lerp()? theres many more ways of interpolating, and only one is linear
|
|
|
|
kevinP
|
Re: Mixing Two Numbers
« Reply #9 on: May 5th, 2004, 12:49pm » |
|
on May 5th, 2004, 12:53am, mKoser wrote:(what would make most sense to someone who's a computer graphics virgin... ie. in the target group of processing!) i'd ask ... what would you rather teach |
| But it's such a lovely word (which is new to me) that I think that once you've heard it: "linear interpolation == lerp" that I think it would stick. (I don't think I'll forget it). Then there's always "lerpolate" or "linerpolate", but "lerp" is sweet and short and seems to follow some rule that says "if you are going to make a bad acronym or abbreviation then make it really bad (i.e. memorable)". -K
|
Kevin Pfeiffer
|
|
|
kevinP
|
Re: Mixing Two Numbers
« Reply #10 on: May 5th, 2004, 12:57pm » |
|
Follow-Up... But in the Reference docs for the possible "lerp()", be sure to mention the original formula. I think that "linking" layers of abstraction is good for the soul (i.e. helps one to learn more deeply). For example, with many packaged Linuxes today one can use an entirely graphical interface to do all system management functions. Problem is that you then quickly forget (or are never aware of) the command line functions behind the interface. IMO the best implementation is to provide the shortcut or higher-level solution, but include (at least a reference to) the lower-level "meat and potatoes". <end of soapbox and bad metaphors>
|
Kevin Pfeiffer
|
|
|
fry
|
Re: Mixing Two Numbers
« Reply #12 on: Sep 27th, 2004, 6:41pm » |
|
megabucket now has lerp(). fear it.
|
|
|
|
|