I'm trying to generate an evenly spaced grid of coordinates inside a polygon. I know that I can generate random points like follows:

const polygon = L.polygon([somePoints]).addTo(map);
const bounds = polygon.getBounds();

const y_max = bounds.getSouth();
const y_min = bounds.getNorth();
const x_max = bounds.getEast();
const x_min = bounds.getWest();

const lat = y_min + (Math.random() * (y_max - y_min));
const lng = x_min + (Math.random() * (x_max - x_min));

const coordinate = turf.point([lat, lng]);

What I'm struggling with is preventing random clusters of points. Are there any guards I can place to make the generated coordinates evenly spaced (or as close to that as possible)?

  • If you want an evenly spaced grid there's no reason to introduce random numbers. It's just a matter of adding constant values dx and dy to x_min and y_min respectively. Create points at (x_min, y_min+dy), (x_min+dx, y_min), (x_min+dx, y_min+dy), etc. – csk Apr 16 at 19:11
  • And if you hate totally evenly spaced grid, you could add random deviations to points created as proposed by csk, with max offset not more then (+-dx/2, +-dy/2). – TomazicM Apr 16 at 20:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.