The best option, for best clarity, is probably to lerp. Lerping is an operation that should be in every programmer's toolbox. Lerp is short for linear interpolation; you pass it the origin value and the destination value, and then a value between 0 (for which it returns the origin) and 1 (for which it returns the destination).
Code: Select all
function lerp(a, b, t)
return a + (b - a) * t
end
Due to floating point rounding issues, however, it does not always return the destination when passing t = 1; in that case it's better to split it as follows:
Code: Select all
function lerp(a, b, t)
return t < 0.5 and a + (b - a) * t or b + (a - b) * (1 - t)
end
Never use a formulation like a * (1 - t) + b * t because that's not monotonic, which causes serious issues in some circumstances; see
https://math.stackexchange.com/question ... erpolation
Applied to the case at hand:
Code: Select all
halfway.x, halfway.y = lerp(a.x, b.x, 0.5), lerp(a.y, b.y, 0.5)