Point group sorting
How point group sorting works.
Random
shuffle(points)
X/Y Ascending
sortBy(points, ["body.x", "body.y"])
X/Y Descending
sortBy(points, ["body.x", "body.y"]).reverse()
Y/X Ascending
sortBy(points, ["body.y", "body.x"])
Y/X Descending
sortBy(points, ["body.y", "body.x"]).reverse()
X/Y Alternating
Not yet available in FarmBot OS.
const ordered = [];
uniq(points.map(p => p.body.x))
.sort()
.map((x, index) =>
index % 2 == 0
? sortBy(points.filter(p => p.body.x == x), "body.y")
: sortBy(points.filter(p => p.body.x == x), "body.y").reverse();
})
.map(row => row.map(p => ordered.push(p)))
Y/X Alternating
Not yet available in FarmBot OS.
const ordered = [];
uniq(points.map(p => p.body.y))
.sort()
.map((y, index) =>
index % 2 == 0
? sortBy(points.filter(p => p.body.y == y), "body.x")
: sortBy(points.filter(p => p.body.y == y), "body.x").reverse();
})
.map(row => row.map(p => ordered.push(p)))
Optimized
Not yet available in FarmBot OS.
Nearest neighbor algorithm:
const ordered = [];
let available = points.filter(p => p);
let from = { x: 0, y: 0 };
points.map(() => {
const nearest = sortBy(available.map(p => ({
point: p,
distance: Math.pow(Math.pow(p.body.x - p.body.x, 2)
+ Math.pow(from.y - from.y, 2), 0.5)
})), "distance")[0].point;
ordered.push(nearest);
from = { x: nearest.body.x, y: nearest.body.y };
available = available.filter(p => p.uuid !== nearest.uuid);
});