Skip to content

Instantly share code, notes, and snippets.

@magicznyleszek
Last active April 11, 2022 21:37
Show Gist options
  • Star 17 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save magicznyleszek/1ca053ca9e52a37d1ece to your computer and use it in GitHub Desktop.
Save magicznyleszek/1ca053ca9e52a37d1ece to your computer and use it in GitHub Desktop.
JavaScript -- get random non-overlapping position
// declarations
var positions = [];
// Returns a random integer between min (included) and max (excluded)
// Using Math.round() will give you a non-uniform distribution!
function getRandomInt(min, max) {
return Math.floor(Math.random() * (max - min)) + min;
}
// generate random positions
function generatePositionsArray(maxX, maxY, safeRadius, irregularity) {
// declarations
var positionsArray = [];
var r, c;
var rows;
var columns;
// count the amount of rows and columns
rows = Math.floor(maxY / safeRadius);
columns = Math.floor(maxX / safeRadius);
// loop through rows
for (r = 1; r <= rows; r += 1) {
// loop through columns
for (c = 1; c <= columns; c += 1) {
// populate array with point object
positionsArray.push({
x: Math.round(maxX * c / columns) + getRandomInt(irregularity * -1, irregularity),
y: Math.round(maxY * r / rows) + getRandomInt(irregularity * -1, irregularity)
});
}
}
// return array
return positionsArray;
}
positions = generatePositionsArray(2400, 1600, 40, 10);
// get random position from positions array
function getRandomPosition(array, removeTaken) {
// declarations
var randomIndex;
var coordinates;
// get random index
randomIndex = getRandomInt(0, array.length - 1);
// get random item from array
coordinates = array[randomIndex];
// check if remove taken
if (removeTaken) {
// remove element from array
array.splice(randomIndex, 1);
}
// return position
return coordinates;
}
getRandomPosition(positions, true);
@Tusko
Copy link

Tusko commented Sep 26, 2017

Can you provide an example how it works?

@RadGH
Copy link

RadGH commented Feb 16, 2018

I got this algorithm to work but it had plenty of collisions :(

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment