Skip to content
This repository was archived by the owner on Jun 6, 2025. It is now read-only.

Commit 15aefdf

Browse files
committed
Add position.lerp
1 parent b94bc64 commit 15aefdf

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

changelog.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
Version: 0.13.0
33
Date: ????
44
Features:
5-
- [position] Added `position.ge` and `position.gt` comparisons.
5+
- [position] Added `position.ge`, `position.gt`, and `position.lerp` functions.
66
Bugfixes:
77
- [bounding-box] Fixed `bounding_box.recenter_on()` not properly preserving box dimensions.
88
---------------------------------------------------------------------------------------------------

position.lua

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
local flib_math = require("__flib__/math")
2+
13
--- Utilities for manipulating positions. All functions support both the shorthand and explicit syntaxes and will
24
--- preserve the syntax that was passed in.
35
--- ```lua
@@ -180,6 +182,20 @@ function flib_position.le(pos1, pos2)
180182
return x1 <= x2 and y1 <= y2
181183
end
182184

185+
--- Linearly interpolate between two positions. For example, an amount of 0.5 will return the midpoint.
186+
--- @generic P
187+
--- @param pos1 P
188+
--- @param pos2 P
189+
--- @param amount number
190+
--- @return P
191+
function flib_position.lerp(pos1, pos2, amount)
192+
if pos1.x then
193+
return { x = flib_math.lerp(pos1.x, pos2.x, amount), y = flib_math.lerp(pos1.y, pos2.y, amount) }
194+
else
195+
return { flib_math.lerp(pos1[1], pos2[1], amount), flib_math.lerp(pos1[2], pos2[2], amount) }
196+
end
197+
end
198+
183199
--- Test if `pos1` is less than `pos2`.
184200
--- @generic P
185201
--- @param pos1 P

0 commit comments

Comments
 (0)