Skip to content

Instantly share code, notes, and snippets.

@codergautam
Forked from shaunlebron/angleLerp.js
Last active October 19, 2022 02:37
Show Gist options
  • Save codergautam/a574787d79c9809cc2e22d5187e9d02f to your computer and use it in GitHub Desktop.
Save codergautam/a574787d79c9809cc2e22d5187e9d02f to your computer and use it in GitHub Desktop.
The best way to interpolate 2D angles
//note: degrees only
const lerp = (start,end,amt) => start+(end-start)*amt
const clamp = (num, min, max) => Math.min(Math.max(num, min), max);
const repeat = (t, m) => clamp(t - Math.floor(t / m) * m, 0, m);
function lerpTheta(a, b, t) {
const dt = repeat(b - a, 360);
return lerp(a, a + (dt > 180 ? dt - 360 : dt), t);
}
//
@LupusInferni315
Copy link

LupusInferni315 commented Jun 9, 2022

why use "(1-t)a+tb" this "a+(b-a)*t" is much easier to read and write.

@codergautam
Copy link
Author

gotcha, I have updated it

@1j01
Copy link

1j01 commented Oct 19, 2022

The updated code is incorrect: amt+(end-start)*amt should be start+(end-start)*amt

@codergautam
Copy link
Author

How did I miss this?! Thanks so much for catching this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment