From 3d2342412157e35feb201f55c3d896ccaff3c582 Mon Sep 17 00:00:00 2001
From: AnHardt <github@kitelab.de>
Date: Thu, 6 Aug 2015 13:46:05 +0200
Subject: [PATCH 1/4] Add array indexIntoAB[xx][yy]

and use it.
---
 Marlin/Marlin_main.cpp | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)

diff --git a/Marlin/Marlin_main.cpp b/Marlin/Marlin_main.cpp
index e6a0760d5d..3481244607 100644
--- a/Marlin/Marlin_main.cpp
+++ b/Marlin/Marlin_main.cpp
@@ -2561,6 +2561,7 @@ inline void gcode_G28() {
         double eqnAMatrix[abl2 * 3], // "A" matrix of the linear system of equations
                eqnBVector[abl2],     // "B" vector of Z points
                mean = 0.0;
+        int8_t indexIntoAB[auto_bed_leveling_grid_points][auto_bed_leveling_grid_points];
       #endif // !DELTA
 
       int probePointCounter = 0;
@@ -2625,7 +2626,7 @@ inline void gcode_G28() {
             bed_level[xCount][yCount] = measured_z + z_offset;
           #endif
 
-          probePointCounter++;
+          indexIntoAB[xCount][yCount] = probePointCounter++;
 
           idle();
 
@@ -2679,7 +2680,7 @@ inline void gcode_G28() {
 
           for (int yy = auto_bed_leveling_grid_points - 1; yy >= 0; yy--) {
             for (int xx = 0; xx < auto_bed_leveling_grid_points; xx++) {
-              int ind = yy * auto_bed_leveling_grid_points + xx;
+              int ind = indexIntoAB[xx][yy];
               float diff = eqnBVector[ind] - mean;
 
               float x_tmp = eqnAMatrix[ind + 0 * abl2],
@@ -2705,7 +2706,7 @@ inline void gcode_G28() {
 
             for (int yy = auto_bed_leveling_grid_points - 1; yy >= 0; yy--) {
               for (int xx = 0; xx < auto_bed_leveling_grid_points; xx++) {
-                int ind = yy * auto_bed_leveling_grid_points + xx;
+                int ind = indexIntoAB[xx][yy];
                 float x_tmp = eqnAMatrix[ind + 0 * abl2],
                   y_tmp = eqnAMatrix[ind + 1 * abl2],
                   z_tmp = 0;

From 4ca8f4a9a7fe3a11dc20e149c5c74bec4150d891 Mon Sep 17 00:00:00 2001
From: AnHardt <github@kitelab.de>
Date: Thu, 6 Aug 2015 13:50:41 +0200
Subject: [PATCH 2/4] Always zigzag

Use the shortest way between probing-points.
---
 Marlin/Marlin_main.cpp | 8 +-------
 1 file changed, 1 insertion(+), 7 deletions(-)

diff --git a/Marlin/Marlin_main.cpp b/Marlin/Marlin_main.cpp
index 3481244607..2d6a2b0263 100644
--- a/Marlin/Marlin_main.cpp
+++ b/Marlin/Marlin_main.cpp
@@ -2582,13 +2582,7 @@ inline void gcode_G28() {
           xInc = -1;
         }
 
-        #if DISABLED(DELTA)
-          // If do_topography_map is set then don't zig-zag. Just scan in one direction.
-          // This gets the probe points in more readable order.
-          if (!do_topography_map) zig = !zig;
-        #else
-          zig = !zig;
-        #endif
+        zig = !zig;
 
         for (int xCount = xStart; xCount != xStop; xCount += xInc) {
           double xProbe = left_probe_bed_position + xGridSpacing * xCount;

From d8860f9ad9b590a3de9905bdcfcda252873ed706 Mon Sep 17 00:00:00 2001
From: AnHardt <github@kitelab.de>
Date: Thu, 6 Aug 2015 13:54:23 +0200
Subject: [PATCH 3/4] Alway end at the same point

Always end at [RIGHT_PROBE_BED_POSITION, BACK_PROBE_BED_POSITION]
Regardles the evennes of auto_bed_leveling_grid_points.
---
 Marlin/Marlin_main.cpp | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/Marlin/Marlin_main.cpp b/Marlin/Marlin_main.cpp
index 2d6a2b0263..0f38cd8520 100644
--- a/Marlin/Marlin_main.cpp
+++ b/Marlin/Marlin_main.cpp
@@ -2565,7 +2565,7 @@ inline void gcode_G28() {
       #endif // !DELTA
 
       int probePointCounter = 0;
-      bool zig = true;
+      bool zig = (auto_bed_leveling_grid_points & 1) ? true : false; //always end at [RIGHT_PROBE_BED_POSITION, BACK_PROBE_BED_POSITION]
 
       for (int yCount = 0; yCount < auto_bed_leveling_grid_points; yCount++) {
         double yProbe = front_probe_bed_position + yGridSpacing * yCount;

From 5707a2c3d5ac8f6b5fb35268bc9682b882b439a3 Mon Sep 17 00:00:00 2001
From: AnHardt <github@kitelab.de>
Date: Sat, 8 Aug 2015 21:15:26 +0200
Subject: [PATCH 4/4] indexindexIntoAB is only vald for !DELTA

---
 Marlin/Marlin_main.cpp | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)

diff --git a/Marlin/Marlin_main.cpp b/Marlin/Marlin_main.cpp
index 0f38cd8520..7c58ff7afd 100644
--- a/Marlin/Marlin_main.cpp
+++ b/Marlin/Marlin_main.cpp
@@ -2616,12 +2616,13 @@ inline void gcode_G28() {
             eqnAMatrix[probePointCounter + 0 * abl2] = xProbe;
             eqnAMatrix[probePointCounter + 1 * abl2] = yProbe;
             eqnAMatrix[probePointCounter + 2 * abl2] = 1;
+            indexIntoAB[xCount][yCount] = probePointCounter;
           #else
             bed_level[xCount][yCount] = measured_z + z_offset;
           #endif
 
-          indexIntoAB[xCount][yCount] = probePointCounter++;
-
+          probePointCounter++;
+  
           idle();
 
         } //xProbe