| 15 | } |
| 16 | |
| 17 | CRGB sampleBilinear(const CRGB *grid, const XYMap &xyMap, float x, float y) { |
| 18 | int x0 = static_cast<int>(x); |
| 19 | int y0 = static_cast<int>(y); |
| 20 | int x1 = min(x0 + 1, static_cast<int>(xyMap.getWidth()) - 1); |
| 21 | int y1 = min(y0 + 1, static_cast<int>(xyMap.getHeight()) - 1); |
| 22 | |
| 23 | // Clamp x0 and y0 to valid range |
| 24 | x0 = max(0, min(x0, static_cast<int>(xyMap.getWidth()) - 1)); |
| 25 | y0 = max(0, min(y0, static_cast<int>(xyMap.getHeight()) - 1)); |
| 26 | |
| 27 | float fx = x - x0; |
| 28 | float fy = y - y0; |
| 29 | |
| 30 | // Get four neighboring pixels |
| 31 | CRGB c00 = grid[xyMap.mapToIndex(x0, y0)]; |
| 32 | CRGB c10 = grid[xyMap.mapToIndex(x1, y0)]; |
| 33 | CRGB c01 = grid[xyMap.mapToIndex(x0, y1)]; |
| 34 | CRGB c11 = grid[xyMap.mapToIndex(x1, y1)]; |
| 35 | |
| 36 | // Interpolate |
| 37 | CRGB c0 = CRGB::blend(c00, c10, static_cast<u8>(fx * 255)); |
| 38 | CRGB c1 = CRGB::blend(c01, c11, static_cast<u8>(fx * 255)); |
| 39 | return CRGB::blend(c0, c1, static_cast<u8>(fy * 255)); |
| 40 | } |
| 41 | |
| 42 | CRGB sampleNearest(const CRGB *grid, const XYMap &xyMap, float x, float y) { |
| 43 | int xi = static_cast<int>(x + 0.5f); // Round to nearest |