From 50a77ef7f05dd4ac8b5ed3e9f3e380c5da7ef50c Mon Sep 17 00:00:00 2001
From: Jason Smith <jason.inet@gmail.com>
Date: Tue, 17 Nov 2020 19:11:13 -0800
Subject: [PATCH] Fix NAN mesh entries with ABL_BILINEAR_SUBDIVISION (#20143)

---
 Marlin/src/feature/bedlevel/abl/abl.cpp | 8 ++++++++
 1 file changed, 8 insertions(+)

diff --git a/Marlin/src/feature/bedlevel/abl/abl.cpp b/Marlin/src/feature/bedlevel/abl/abl.cpp
index 39d8815fad..44e4fc38a1 100644
--- a/Marlin/src/feature/bedlevel/abl/abl.cpp
+++ b/Marlin/src/feature/bedlevel/abl/abl.cpp
@@ -161,6 +161,14 @@ void print_bilinear_leveling_grid() {
   #define LINEAR_EXTRAPOLATION(E, I) ((E) * 2 - (I))
   float bed_level_virt_coord(const uint8_t x, const uint8_t y) {
     uint8_t ep = 0, ip = 1;
+    if (x > GRID_MAX_POINTS_X + 1 || y > GRID_MAX_POINTS_Y + 1) {
+      // The requested point requires extrapolating two points beyond the mesh.
+      // These values are only requested for the edges of the mesh, which are always an actual mesh point,
+      // and do not require interpolation. When interpolation is not needed, this "Mesh + 2" point is
+      // cancelled out in bed_level_virt_cmr and does not impact the result. Return 0.0 rather than
+      // making this function more complex by extrapolating two points.
+      return 0.0;
+    }    
     if (!x || x == ABL_TEMP_POINTS_X - 1) {
       if (x) {
         ep = GRID_MAX_POINTS_X - 1;