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.

Revisions

  1. @magicznyleszek magicznyleszek revised this gist Jul 29, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion random-non-overlapping-position.js
    Original file line number Diff line number Diff line change
    @@ -51,4 +51,4 @@ function getRandomPosition(array, removeTaken) {
    return coordinates;
    }

    // getRandomPosition(positions, true);
    getRandomPosition(positions, true);
  2. @magicznyleszek magicznyleszek created this gist Jul 29, 2014.
    54 changes: 54 additions & 0 deletions random-non-overlapping-position.js
    Original 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);