Midpoint and Linear Interpolation

Understand the use of 'std::midpoint' and 'std::lerp' with an example.

  • std::midpoint(a, b): calculates the midpoint (a+(b−a)/2)(a + (b - a) / 2) of integers, floating points, or pointers. If a and b are pointers, they have to point to the same array object. The function needs the header <numeric>.
  • std::lerp(a, b, t): calculates the linear interpolation (a+t(b−a))(a + t(b - a)). When t is outside the range [0,1][0, 1], it calculates the linear extrapolation. The function needs the header <cmath>.

The following program applies both functions.

Get hands-on with 1400+ tech skills courses.