Skip to content

Instantly share code, notes, and snippets.

@rgov
Forked from mbostock/.block
Created December 12, 2017 01:36
Show Gist options
  • Select an option

  • Save rgov/bcffaf09f941e9f9148ea75fe9352e02 to your computer and use it in GitHub Desktop.

Select an option

Save rgov/bcffaf09f941e9f9148ea75fe9352e02 to your computer and use it in GitHub Desktop.

Revisions

  1. rgov revised this gist Dec 12, 2017. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion index.html
    Original file line number Diff line number Diff line change
    @@ -50,7 +50,7 @@
    // Make a new candidate between [radius, 2 * radius] from the existing sample.
    for (var j = 0; j < k; ++j) {
    var a = 2 * Math.PI * Math.random(),
    r = Math.sqrt(Math.random() * R + radius2),
    r = Math.random() * radius + radius,
    x = s[0] + r * Math.cos(a),
    y = s[1] + r * Math.sin(a);

  2. @mbostock mbostock revised this gist Feb 9, 2016. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions .block
    Original file line number Diff line number Diff line change
    @@ -0,0 +1 @@
    license: gpl-3.0
  3. @mbostock mbostock revised this gist Oct 30, 2015. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion index.html
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,7 @@
    <!DOCTYPE html>
    <meta charset="utf-8">
    <body>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.5/d3.min.js"></script>
    <script src="//d3js.org/d3.v3.min.js"></script>
    <script>

    var width = 960,
  4. @mbostock mbostock revised this gist Jun 11, 2015. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion index.html
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,7 @@
    <!DOCTYPE html>
    <meta charset="utf-8">
    <body>
    <script src="http://d3js.org/d3.v3.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.5/d3.min.js"></script>
    <script>

    var width = 960,
  5. @mbostock mbostock revised this gist Jun 9, 2014. 1 changed file with 7 additions and 6 deletions.
    13 changes: 7 additions & 6 deletions index.html
    Original file line number Diff line number Diff line change
    @@ -9,19 +9,20 @@

    var sample = poissonDiscSampler(width, height, 10);

    var canvas = d3.select("body").append("canvas")
    var svg = d3.select("body").append("svg")
    .attr("width", width)
    .attr("height", height);

    var context = canvas.node().getContext("2d");

    d3.timer(function() {
    for (var i = 0; i < 10; ++i) {
    var s = sample();
    if (!s) return true;
    context.beginPath();
    context.arc(s[0], s[1], 2, 0, 2 * Math.PI);
    context.fill();
    svg.append("circle")
    .attr("cx", s[0])
    .attr("cy", s[1])
    .attr("r", 0)
    .transition()
    .attr("r", 2);
    }
    });

  6. @mbostock mbostock revised this gist Jun 7, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion README.md
    Original file line number Diff line number Diff line change
    @@ -1 +1 @@
    Based on [Jason Davies’ implementation](https://www.jasondavies.com/poisson-disc/) of Bridson’s algorithm.
    Based on [Jason Davies’ implementation](https://www.jasondavies.com/poisson-disc/) of Bridson’s algorithm. See the [related explanation](/mbostock/dbb02448b0f93e4c82c3).
  7. @mbostock mbostock revised this gist Jun 7, 2014. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1 @@
    Based on [Jason Davies’ implementation](https://www.jasondavies.com/poisson-disc/) of Bridson’s algorithm.
  8. @mbostock mbostock revised this gist Jun 6, 2014. 1 changed file with 6 additions and 7 deletions.
    13 changes: 6 additions & 7 deletions index.html
    Original file line number Diff line number Diff line change
    @@ -7,7 +7,7 @@
    var width = 960,
    height = 500;

    var sample = poissonDiscSampler(width, height, 5.23);
    var sample = poissonDiscSampler(width, height, 10);

    var canvas = d3.select("body").append("canvas")
    .attr("width", width)
    @@ -28,10 +28,9 @@
    // Based on https://www.jasondavies.com/poisson-disc/
    function poissonDiscSampler(width, height, radius) {
    var k = 30, // maximum number of samples before rejection
    diameter = 2 * radius,
    diameter2 = diameter * diameter,
    R = 3 * diameter2,
    cellSize = diameter * Math.SQRT1_2,
    radius2 = radius * radius,
    R = 3 * radius2,
    cellSize = radius * Math.SQRT1_2,
    gridWidth = Math.ceil(width / cellSize),
    gridHeight = Math.ceil(height / cellSize),
    grid = new Array(gridWidth * gridHeight),
    @@ -50,7 +49,7 @@
    // Make a new candidate between [radius, 2 * radius] from the existing sample.
    for (var j = 0; j < k; ++j) {
    var a = 2 * Math.PI * Math.random(),
    r = Math.sqrt(Math.random() * R + diameter2),
    r = Math.sqrt(Math.random() * R + radius2),
    x = s[0] + r * Math.cos(a),
    y = s[1] + r * Math.sin(a);

    @@ -79,7 +78,7 @@
    var s,
    dx = s[0] - x,
    dy = s[1] - y;
    if (dx * dx + dy * dy < diameter2) return false;
    if (dx * dx + dy * dy < radius2) return false;
    }
    }
    }
  9. @mbostock mbostock revised this gist Jun 6, 2014. 1 changed file with 25 additions and 31 deletions.
    56 changes: 25 additions & 31 deletions index.html
    Original file line number Diff line number Diff line change
    @@ -37,38 +37,31 @@
    grid = new Array(gridWidth * gridHeight),
    queue = [],
    queueSize = 0,
    samples = [];

    sample(Math.random() * width, Math.random() * height);

    // Pick a random existing sample and remove it from the queue.
    iterate: while (queueSize) {
    var i = Math.random() * queueSize | 0,
    s = queue[i];

    // Make a new candidate between [radius, 2 * radius] from the existing sample.
    for (var j = 0; j < k; ++j) {
    var a = 2 * Math.PI * Math.random(),
    r = Math.sqrt(Math.random() * R + diameter2),
    x = s[0] + r * Math.cos(a),
    y = s[1] + r * Math.sin(a);

    // Reject candidates that are outside the allowed extent,
    // or closer than 2 * radius to any existing sample.
    if (0 <= x && x < width && 0 <= y && y < height && far(x, y)) {
    sample(x, y);
    continue iterate;
    }
    }

    queue[i] = queue[--queueSize];
    queue.length = queueSize;
    }

    samples.reverse();
    sampleSize = 0;

    return function() {
    return samples.pop();
    if (!sampleSize) return sample(Math.random() * width, Math.random() * height);

    // Pick a random existing sample and remove it from the queue.
    while (queueSize) {
    var i = Math.random() * queueSize | 0,
    s = queue[i];

    // Make a new candidate between [radius, 2 * radius] from the existing sample.
    for (var j = 0; j < k; ++j) {
    var a = 2 * Math.PI * Math.random(),
    r = Math.sqrt(Math.random() * R + diameter2),
    x = s[0] + r * Math.cos(a),
    y = s[1] + r * Math.sin(a);

    // Reject candidates that are outside the allowed extent,
    // or closer than 2 * radius to any existing sample.
    if (0 <= x && x < width && 0 <= y && y < height && far(x, y)) return sample(x, y);
    }

    queue[i] = queue[--queueSize];
    queue.length = queueSize;
    }
    };

    function far(x, y) {
    @@ -97,9 +90,10 @@
    function sample(x, y) {
    var s = [x, y];
    queue.push(s);
    samples.push(s);
    grid[gridWidth * (y / cellSize | 0) + (x / cellSize | 0)] = s;
    ++sampleSize;
    ++queueSize;
    return s;
    }
    }

  10. @mbostock mbostock revised this gist Jun 6, 2014. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions index.html
    Original file line number Diff line number Diff line change
    @@ -30,7 +30,7 @@
    var k = 30, // maximum number of samples before rejection
    diameter = 2 * radius,
    diameter2 = diameter * diameter,
    k = 3 * diameter2,
    R = 3 * diameter2,
    cellSize = diameter * Math.SQRT1_2,
    gridWidth = Math.ceil(width / cellSize),
    gridHeight = Math.ceil(height / cellSize),
    @@ -49,7 +49,7 @@
    // Make a new candidate between [radius, 2 * radius] from the existing sample.
    for (var j = 0; j < k; ++j) {
    var a = 2 * Math.PI * Math.random(),
    r = Math.sqrt(Math.random() * k + diameter2),
    r = Math.sqrt(Math.random() * R + diameter2),
    x = s[0] + r * Math.cos(a),
    y = s[1] + r * Math.sin(a);

  11. @mbostock mbostock revised this gist Jun 6, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion index.html
    Original file line number Diff line number Diff line change
    @@ -7,7 +7,7 @@
    var width = 960,
    height = 500;

    var sample = poissonDiscSampler(width, height, 5.66);
    var sample = poissonDiscSampler(width, height, 5.23);

    var canvas = d3.select("body").append("canvas")
    .attr("width", width)
  12. @mbostock mbostock revised this gist Jun 6, 2014. 1 changed file with 10 additions and 5 deletions.
    15 changes: 10 additions & 5 deletions index.html
    Original file line number Diff line number Diff line change
    @@ -30,6 +30,7 @@
    var k = 30, // maximum number of samples before rejection
    diameter = 2 * radius,
    diameter2 = diameter * diameter,
    k = 3 * diameter2,
    cellSize = diameter * Math.SQRT1_2,
    gridWidth = Math.ceil(width / cellSize),
    gridHeight = Math.ceil(height / cellSize),
    @@ -41,23 +42,27 @@
    sample(Math.random() * width, Math.random() * height);

    // Pick a random existing sample and remove it from the queue.
    while (queueSize) {
    iterate: while (queueSize) {
    var i = Math.random() * queueSize | 0,
    s = queue[i];
    queue[i] = queue[--queueSize];
    queue.pop();

    // Make a new candidate between [radius, 2 * radius] from the existing sample.
    for (var j = 0; j < k; ++j) {
    var a = 2 * Math.PI * Math.random(),
    r = diameter * 1 + Math.random(),
    r = Math.sqrt(Math.random() * k + diameter2),
    x = s[0] + r * Math.cos(a),
    y = s[1] + r * Math.sin(a);

    // Reject candidates that are outside the allowed extent,
    // or closer than 2 * radius to any existing sample.
    if (0 <= x && x < width && 0 <= y && y < height && far(x, y)) sample(x, y);
    if (0 <= x && x < width && 0 <= y && y < height && far(x, y)) {
    sample(x, y);
    continue iterate;
    }
    }

    queue[i] = queue[--queueSize];
    queue.length = queueSize;
    }

    samples.reverse();
  13. @mbostock mbostock revised this gist Jun 6, 2014. 1 changed file with 0 additions and 0 deletions.
    Binary file added thumbnail.png
    Loading
    Sorry, something went wrong. Reload?
    Sorry, we cannot display this file.
    Sorry, this file is invalid so it cannot be displayed.
  14. @mbostock mbostock created this gist Jun 6, 2014.
    101 changes: 101 additions & 0 deletions index.html
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,101 @@
    <!DOCTYPE html>
    <meta charset="utf-8">
    <body>
    <script src="http://d3js.org/d3.v3.min.js"></script>
    <script>

    var width = 960,
    height = 500;

    var sample = poissonDiscSampler(width, height, 5.66);

    var canvas = d3.select("body").append("canvas")
    .attr("width", width)
    .attr("height", height);

    var context = canvas.node().getContext("2d");

    d3.timer(function() {
    for (var i = 0; i < 10; ++i) {
    var s = sample();
    if (!s) return true;
    context.beginPath();
    context.arc(s[0], s[1], 2, 0, 2 * Math.PI);
    context.fill();
    }
    });

    // Based on https://www.jasondavies.com/poisson-disc/
    function poissonDiscSampler(width, height, radius) {
    var k = 30, // maximum number of samples before rejection
    diameter = 2 * radius,
    diameter2 = diameter * diameter,
    cellSize = diameter * Math.SQRT1_2,
    gridWidth = Math.ceil(width / cellSize),
    gridHeight = Math.ceil(height / cellSize),
    grid = new Array(gridWidth * gridHeight),
    queue = [],
    queueSize = 0,
    samples = [];

    sample(Math.random() * width, Math.random() * height);

    // Pick a random existing sample and remove it from the queue.
    while (queueSize) {
    var i = Math.random() * queueSize | 0,
    s = queue[i];
    queue[i] = queue[--queueSize];
    queue.pop();

    // Make a new candidate between [radius, 2 * radius] from the existing sample.
    for (var j = 0; j < k; ++j) {
    var a = 2 * Math.PI * Math.random(),
    r = diameter * 1 + Math.random(),
    x = s[0] + r * Math.cos(a),
    y = s[1] + r * Math.sin(a);

    // Reject candidates that are outside the allowed extent,
    // or closer than 2 * radius to any existing sample.
    if (0 <= x && x < width && 0 <= y && y < height && far(x, y)) sample(x, y);
    }
    }

    samples.reverse();

    return function() {
    return samples.pop();
    };

    function far(x, y) {
    var i = x / cellSize | 0,
    j = y / cellSize | 0,
    i0 = Math.max(i - 2, 0),
    j0 = Math.max(j - 2, 0),
    i1 = Math.min(i + 3, gridWidth),
    j1 = Math.min(j + 3, gridHeight);

    for (j = j0; j < j1; ++j) {
    var o = j * gridWidth;
    for (i = i0; i < i1; ++i) {
    if (s = grid[o + i]) {
    var s,
    dx = s[0] - x,
    dy = s[1] - y;
    if (dx * dx + dy * dy < diameter2) return false;
    }
    }
    }

    return true;
    }

    function sample(x, y) {
    var s = [x, y];
    queue.push(s);
    samples.push(s);
    grid[gridWidth * (y / cellSize | 0) + (x / cellSize | 0)] = s;
    ++queueSize;
    }
    }

    </script>