Written by me (Anixias).

```
function wrap(val, minval, maxval)
{
while(val < minval) val += maxval - minval;
while(val >= maxval) val -= maxval - minval;
return val;
}
function lerp_wrap(src, dest, amt, minval, maxval)
{
if (dest > src)
{
if (abs(dest - src) >= abs((dest - (maxval - minval)) - src))
{
dest -= (maxval - minval);
}
}
else if (dest < src)
{
if (abs(dest - src) >= abs((dest + (maxval - minval)) - src))
{
dest += (maxval - minval);
}
}
else return wrap(dest, minval, maxval);
var val = lerp(src, dest, amt);
return wrap(val, minval, maxval);
}
```

## Usage

You can use this to lerp from one value to another, but it takes into account that there is a wrapping range. For example, say you want to change `image_angle`

to lerp towards some direction *D *every frame, but it should rotate in the direction closest to the goal. Let’s assume `image_angle`

is 350 degrees, and *D *is 15 degrees. Obviously, we should increase `image_angle`

, since 15 is the same as 375, and going up from 350 to 375 is faster than going down from 350 to 15. This script takes that into account. You can achieve this by calling `lerp_wrap(image_angle, D, 0.1, 0, 360)`

in the Step Event. 0.1 here can be any number you want, from 0 to 1.

**Note**: This script works in the range [minval, maxval). AKA, it can equal minval, but it will never equal maxval.