Last active
August 22, 2016 02:23
-
-
Save 1wheel/c9e35bd69012dd9adb48 to your computer and use it in GitHub Desktop.
random-simple-poly-circle
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> | |
path{ | |
fill-opacity: .3; | |
} | |
html{ | |
width: 960px; | |
height: 500px; | |
} | |
</style> | |
<script src="/1wheel/raw/67b47524ed8ed829d021/d3-3.5.5.js"></script> | |
<script src="/1wheel/raw/67b47524ed8ed829d021/lodash-3.8.0.js"></script> | |
<script src='/1wheel/raw/1b6758978dc2d52d3a37/d3-jetpack.js'></script> | |
<script src='/1wheel/raw/1b6758978dc2d52d3a37/d3-starterkit.js'></script> | |
<script src='/1wheel/raw/5d32ecb8a54b42f53646/geometry.js'></script> | |
<script src='rand-poly.js'></script> | |
<script> | |
var size = 160, | |
rows = Math.floor(960/size), | |
cols = Math.floor(500/size) | |
var s = d3.scale.linear().domain([-1, 1]).range([0, size]) | |
var polys = d3.range(rows*cols).map(function(d){ | |
return { | |
x: d % rows, | |
y: Math.floor(d/rows), | |
points: randPoly(100).map(function(d){ | |
return P(s(d.x), s(d.y)) | |
}) | |
} | |
}) | |
var svg = d3.select('html').append('svg') | |
.attr({width: 960, height: 500}) | |
var polyG = svg.dataAppend(polys, 'g') | |
.translate(function(d){ return [d.x*size, d.y*size] }) | |
polyG.dataAppend(ƒ('points'), 'circle') | |
.translate(ƒ()) | |
.attr('r', 1.5) | |
polyG.append('path.poly') | |
.attr('d', function(d){ | |
return 'M' + d.points.join('L') }) | |
</script> |
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
function randPoly(n){ | |
var points = d3.range(n).map(function(d){ | |
return Math.random()*Math.PI*2 | |
}) | |
points = _.sortBy(points) | |
return points.map(function(d){ | |
var r = Math.random()*(1 + (Math.sqrt(2) -1)*((d) % Math.PI/4)) | |
return P(Math.cos(d)*r, Math.sin(d)*r) | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment