Skip to content

Instantly share code, notes, and snippets.

@sxywu
Last active December 29, 2015 07:48
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 sxywu/f8e9ae0a4c101d978f84 to your computer and use it in GitHub Desktop.
Save sxywu/f8e9ae0a4c101d978f84 to your computer and use it in GitHub Desktop.
Art in Pi, Experiment #1
<!DOCTYPE html>
<head>
<meta charset="utf-8">
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.5/d3.min.js"></script>
<script src="http://underscorejs.org/underscore-min.js"></script>
<style>
body { margin:0;position:fixed;top:0;right:0;bottom:0;left:0; }
svg { width: 100%; height: 100%; }
</style>
</head>
<body>
<svg></svg>
<script>
var piString = "3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342117067982148086513282306647093844609550582231725359408128481";
var width = 900;
var height = 900;
var length = 50;
d3.select('svg')
.append('path')
.attr('d', function() {
var x = width / 2;
var y = height / 2;
var d = 'M' + x + ',' + y;
_.each(piString, function(int) {
int = parseInt(int);
if (_.isNaN(int)) return;
var radian = (2 * Math.PI) / 10 * int - (Math.PI / 2);
x += length * Math.cos(radian);
y += length * Math.sin(radian);
d += ' L' + x + ',' + y;
});
return d;
}).attr('fill', 'none')
.attr('stroke', '#666')
.attr('stroke-width', 2);
</script>
</body>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment