| 231 | } |
| 232 | |
| 233 | std::optional<QPointF> curvePointAt(const QwtPlotCurve* curve, double x) { |
| 234 | if (curve == nullptr || curve->dataSize() == 0) { |
| 235 | return std::nullopt; |
| 236 | } |
| 237 | if (curve->dataSize() == 1) { |
| 238 | return curve->sample(0); |
| 239 | } |
| 240 | |
| 241 | std::size_t low = 0; |
| 242 | std::size_t high = curve->dataSize(); |
| 243 | while (low < high) { |
| 244 | const std::size_t mid = low + (high - low) / 2; |
| 245 | if (curve->sample(mid).x() <= x) { |
| 246 | low = mid + 1; |
| 247 | } else { |
| 248 | high = mid; |
| 249 | } |
| 250 | } |
| 251 | |
| 252 | if (low == 0) { |
| 253 | return curve->sample(0); |
| 254 | } |
| 255 | if (low >= curve->dataSize()) { |
| 256 | return curve->sample(curve->dataSize() - 1); |
| 257 | } |
| 258 | |
| 259 | const QPointF left = curve->sample(low - 1); |
| 260 | const QPointF right = curve->sample(low); |
| 261 | const double middle_x = (left.x() + right.x()) * 0.5; |
| 262 | return x < middle_x ? left : right; |
| 263 | } |
| 264 | |
| 265 | } // namespace PJ |
no test coverage detected