Forked from magicznyleszek/random-non-overlapping-position.js
Created
September 16, 2017 20:20
-
-
Save samuelcotterall/180f7b0f3d306e2e4f27c640bda72fb6 to your computer and use it in GitHub Desktop.
Revisions
-
magicznyleszek revised this gist
Jul 29, 2014 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -51,4 +51,4 @@ function getRandomPosition(array, removeTaken) { return coordinates; } getRandomPosition(positions, true); -
magicznyleszek created this gist
Jul 29, 2014 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,54 @@ // 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);