static float Integrate(const Curve &c, float x0, float x1) { bool negate = false; if (x0 > x1) { std::swap(x0, x1); negate = true; } int i0 = c.ToIndex(x0); int i1 = c.ToIndex(x1 + c.dx); i0 = std::min(std::max(i0, 0), c.size() - 1); i1 = std::min(std::max(i1, 0), c.size() - 1); float T = 0.5 * (c[i0] + c[i1]); float M = 0; for (int i = i0+2; i < i1; i += 2) { T += c[i]; M += c[i-1]; } float a = c.dx * (T + M); return negate ? -a : a; }