Get the reverse value of a Lerp

Hello, I am trying to figure out how I am able to get the reverse value of a Lerp. To clarify I want to get the alpha value of a Lerp by using the returned value of a regular Lerp.

Ways I have tried using:

  • Instead of using the regular Lerp function I instead tried substituting the value with the alpha which would normally work with numbers but wouldn’t work with other types of values such as Vectors.

  • I also tried changing the start and goals into percentages but it will not work with other types of values as well.

  • Lastly, I tried using the regular Lerp function to then get the value and return it with the Alpha value but it wouldn’t be useful because you already know the alpha. Which I am trying to get without any pre Lerping to already have the alpha value.

local function Lerp(a,b,i)
	return a + (b - a) * i 
end

local function ReverseLerp(a,b,v)
	return = a + (b - a) / v 
end

local Start = Vector2.new(0,0)
local End = Vector2.new(1,1)
local Alpha = .5
local Value = Vector2.new(.5,.5)

Lerp(Start,End,.5) -- prints(Vector2.new(.5,.5) )
ReverseLerp(Start,End,Value) -- prints(Vector2.new(2,2) ) with multiplying it will print Vector.new(.5,.5)
-- ReverseLerp should print .5 because it is in the middle

– How would I be able to get the alpha through the value?

I think you did your math wrong

When x = start + (goal - start) * alpha and we solve for alpha:

Move start to the other side:
x - start = alpha * (goal - start)
And then divide both sides by (goal - start):
alpha = (x - start) / (goal - start)

So it should be:

local function ReverseLerp(a, b, v)
	return (v - a) / (b - a)
end
1 Like

Thanks for helping but this wouldn’t work for other values that can be lerped such as vector; it will return a vector2 value which is what should be returned in a regular Lerp function but I am trying to reverse it in order to get an alpha value which is just a number.

I know this is an older thread, and you probably found the answer

But if you take the dot products of the vector and then divide it will give you the alpha value for interpolation

function inverseLerp(a: Vector3, b: Vector3, v: Vector3)
	local a1 = (v - a)
	local b1 = (b - a)

	return a1:Dot(b1) / b1:Dot(b1)
end