# unlerp

Discussion and feedback on Construct 2

### » Sun Aug 12, 2012 10:51 pm

Could someone please explain me how it works?

Manual says

[Quote]unlerp(a, b, y) Reverse linear interpolation: if lerp(a, b, x) = y, then unlerp(a, b, y) = x. Calculates (y - a) / (b - a).[/QUOTE]

but to be fair i don't get it.
B
140
S
58
G
37
Posts: 2,549
Reputation: 31,711

### » Sun Aug 12, 2012 11:58 pm

The manual gave the full definition: it calculates (y - a) / (b - a), which is the opposite of lerp. So unlerp(a, b, lerp(a, b, x)) = x. E.g. lerp(10, 20, 0.5) gives 15, so unlerp(10, 20, 15) gives 0.5.Ashley2012-08-12 23:59:42
Scirra Founder
B
362
S
216
G
75
Posts: 23,063
Reputation: 180,303

### » Mon Aug 13, 2012 12:10 am

Yes, that part of manual I can understand ;P

Let me rephrase my question. What about practical use of unlerp

I can easily comprehend what e.g. lerp(self.x,target.x,dt) do, but unlerp?

I know i'm missing something simple.
B
140
S
58
G
37
Posts: 2,549
Reputation: 31,711

### » Mon Aug 13, 2012 3:59 am

One possible use of unlerp is to do scrollbar math.
http://dl.dropbox.com/u/5426011/examples13/unlerp_scroll.capx
B
82
S
26
G
61
Posts: 4,821
Reputation: 44,682

### » Mon Aug 13, 2012 8:45 am

In layman's terms, it gives you the percentage of point between two values.

You have points A and B and you want to know where your "x" between points is in relation to those points. So you do unlerp(A,B,x) to see where the x is standing relative to the range between A and B.
B
62
S
21
G
12
Posts: 1,910
Reputation: 13,155

### » Tue Jun 11, 2013 9:55 pm

Thanks for the examples of using unlerp guys! It's SUPER useful stuff.
B
14
S
4
G
4
Posts: 19
Reputation: 3,206