Skip to content

Instantly share code, notes, and snippets.

@d2fn
Last active August 29, 2015 14:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save d2fn/cc739c2c52538790f2bd to your computer and use it in GitHub Desktop.
Save d2fn/cc739c2c52538790f2bd to your computer and use it in GitHub Desktop.
Uniform Random Point Distribution

Demonstration of unevenness of uniform random distributions for point scattering.

<!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