# Transforming 2D points on to a regular grid or lattice

I have a set of points in 2D generated by doing a DimensionReduce on a list of colors:

colors = RandomColor;
coords = DimensionReduce[colors, 2, Method -> "TSNE"]; However, I would like to arrange these points into a regular 2D grid (or other lattice) while maintaining neighbourhoods as much as possible, to make something like this: (this is just a mockup)

Is there a function to provide this transformation? Or am I better off writing something to shuffle the colors in a grid to minimise neighbor distances? Here's my crude attempt at such a process:

dm = DistanceMatrix[colors, DistanceFunction -> ColorDistance];
g = SystemGridGraph[{10, 10}, VertexSize -> .8];
vneighbors =
GatherBy[Position[adj // Normal, 1], First][[;; , ;; , 2]];
vlabels = Range;
Fold[SetProperty[{#1, #2},
VertexStyle -> colors[[vlabels[[#2]]]]] &, g, Range] Do[
While[{swapi, swapj} = RandomInteger[{1, 100}, 2]; swapi == swapj];
cost = Total@
dm[[vlabels[[swapi]], vlabels[[vneighbors[[swapi]]]]]] +
Total@dm[[vlabels[[swapj]], vlabels[[vneighbors[[swapj]]]]]];
swapcost =
Total@dm[[vlabels[[swapj]], vlabels[[vneighbors[[swapi]]]]]] +
Total@dm[[vlabels[[swapi]], vlabels[[vneighbors[[swapj]]]]]];
If[cost > swapcost, temp = vlabels[[swapi]];
vlabels[[swapi]] = vlabels[[swapj]]; vlabels[[swapj]] = temp],
10000];
Fold[SetProperty[{#1, #2},
VertexStyle -> colors[[vlabels[[#2]]]]] &, g, Range] Is something like this the best solution to the problem? There must be something cleverer Mathematica can give me. It is hard to understand what you mean, but here is a simple take. Round can take an arbitrary step. That makes a grid of an arbitrary step.

colors=RandomColor;
coords=DimensionReduce[colors,2,Method->"TSNE"];

gridStep=.5;

ListPlot[
`