Skip to content

Instantly share code, notes, and snippets.

@Dioarya
Last active July 24, 2022 04:36
Show Gist options
  • Save Dioarya/5892be0df996b6160ae4498072a1b98e to your computer and use it in GitHub Desktop.
Save Dioarya/5892be0df996b6160ae4498072a1b98e to your computer and use it in GitHub Desktop.
Linear Interpolation
def lerp(v0, v1, t):
return (1 - t) * v0 + t * v1
def inv_lerp(a, b, v):
return (v - a) / (b - a)
def remap(min0, max0, min1, max1, value):
return lerp(min1, max1, inv_lerp(min0, nax1, value))
def remap(v, a0, a1, b0, b1):
return (((v - a0) * (b1 - b0)) / (a1 - a0)) + b0
import math
def list_lerp(l:list | tuple, t:float | int) -> float:
if t == 1:
return l[-1]
t *= len(l) - 1
i = math.floor(t)
return (1 - t % 1) * l[i] + t * l[i + 1]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment