Demonstration of unevenness of uniform random distributions for point scattering.
Last active
August 29, 2015 14:03
-
-
Save d2fn/cc739c2c52538790f2bd to your computer and use it in GitHub Desktop.
Uniform Random Point Distribution
This file contains 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 characters
<!DOCTYPE html> | |
<meta charset="utf-8"> | |
<style> | |
body { | |
background: rgb(255, 255, 255); | |
} | |
</style> | |
<body> | |
<script src="http://d3js.org/d3.v3.min.js"></script> | |
<script> | |
var width = 960, | |
height = 500; | |
var svg = d3.select("body") | |
.append("svg") | |
.attr({width: width, height: height}); | |
var sample = uniformRandomSampler(width, height, 20000); | |
d3.timer(function() { | |
for(var i = 0; i < 100; i++) { | |
var s = sample(); | |
if(!s) return true; | |
svg.append("circle") | |
.attr({ | |
r: 1.5, | |
cx: s[0], | |
cy: s[1], | |
opacity: 0.0 | |
}) | |
.transition() | |
.attr({ | |
opacity: 1.0 | |
}); | |
} | |
}); | |
function uniformRandomSampler(width, height, maxSamples) { | |
var samples = 0; | |
return function() { | |
if(++samples > maxSamples) return; | |
return [ | |
Math.random() * width, | |
Math.random() * height | |
]; | |
}; | |
} | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment