Interpolation.cpp 760 B

1234567891011121314151617181920212223242526272829303132333435
  1. /*
  2. * Copyright (c) 2022, Rodrigo Tobar <rtobarc@gmail.com>.
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibPDF/Interpolation.h>
  7. namespace PDF {
  8. static float slope(float x_min, float x_max, float y_min, float y_max)
  9. {
  10. return (y_max - y_min) / (x_max - x_min);
  11. }
  12. LinearInterpolation1D::LinearInterpolation1D(float x_min, float x_max, float y_min, float y_max)
  13. : m_x_min(x_min)
  14. , m_y_min(y_min)
  15. , m_slope(slope(x_min, x_max, y_min, y_max))
  16. {
  17. }
  18. float LinearInterpolation1D::interpolate(float x) const
  19. {
  20. return m_y_min + ((x - m_x_min) * m_slope);
  21. }
  22. void LinearInterpolation1D::interpolate(Span<float> const& x, Span<float> y) const
  23. {
  24. for (size_t i = 0; i < x.size(); ++i) {
  25. y[i] = interpolate(x[i]);
  26. }
  27. }
  28. }