Interpolando que es gerundio
Hay pocas cosas tan útiles en programación como la interpolación, de hecho son tantos los casos en los que resulta útil (o incluso esencial) que ni me voy a molestar en mencionarlos.
Directamente dejo aqui un par de ejemplos, el primero es una función de interpolación lineal:
1 2 3 4 | float InterpLineal(float tiempo, float inicio, float fin) { return((1.0f - tiempo) * inicio + tiempo * fin); } |
Esta es una función de interpolación cuadrática (“Easy In”):
1 2 3 4 | float InterpCuadIn(float tiempo, float inicio, float fin) { return(InterpLineal(tiempo * tiempo, incio, fin)); } |
Y por último una interpolación cuadrática (“Easy In-Out”):
1 2 3 4 5 6 7 8 9 10 11 12 | float InterpCuadInOut(float tiempo, float inicio, float fin) { float medio = (inicio + fin) * 0.5f; tiempo *= 2.0f; if(tiempo <= 1.0f) { return(InterpLineal(tiempo * tiempo, inicio, medio)); } tiempo -= 1.0f; return(InterpLineal(tiempo * tiempo, medio, fin)); } |
En los tres casos se fija el rango a interpolar con los parámetros inicio y fin, y con el parámetro tiempo se indica el punto de interpolación (por supuesto se asume que tiempo tendrá un valor comprendido entre 0.0 y 1.0).