Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save samuelcotterall/180f7b0f3d306e2e4f27c640bda72fb6 to your computer and use it in GitHub Desktop.
Save samuelcotterall/180f7b0f3d306e2e4f27c640bda72fb6 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);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment