Skip to content

Instantly share code, notes, and snippets.

@stared
Last active October 20, 2021 17:30
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 stared/b8440943a1d72aed3033ea092a2a2902 to your computer and use it in GitHub Desktop.
Save stared/b8440943a1d72aed3033ea092a2a2902 to your computer and use it in GitHub Desktop.
Sierpinski Eye of Providence
<!DOCTYPE html>
<meta charset="utf-8">
<svg id="chart"></svg>
<script src="//d3js.org/d3.v3.min.js"></script>
<script>
// it's ES6 (aka ES2015)
const margin = 20;
const a = 500;
const h = a * Math.sqrt(3) / 2;
const eyeH = 0.35 * a;
const eyeR = 0.35 * a;
const eyeDH = -0.15 * h;
const irisR = 0.1 * eyeH;
const g = d3.select('#chart')
.attr('width', a + 2*margin)
.attr('height', h + 2*margin)
.append('g')
.attr('transform', `translate(${a/2 + margin}, ${3*h/4 + margin})`);
const drawSierpinski = (parentSelector, n_more) => {
parentSelector.append('circle')
.attr('cy', eyeDH)
.attr('r', irisR);
parentSelector.append('path')
.style('fill', 'none')
.style('stroke', 'black')
.style('stroke-width', '3px')
.attr('d', `M ${-eyeH/2} ${eyeDH}
a ${eyeR} ${eyeR} 0 0 1 ${eyeH} 0
a ${eyeR} ${eyeR} 0 0 1 ${-eyeH} 0
Z`);
if (n_more > 0) {
parentSelector.selectAll('g')
.data([
{dx: 0, dy: -3*h/8},
{dx: a/4, dy: h/8},
{dx: -a/4, dy: h/8}
])
.enter().append('g')
.attr('transform', (d) =>
`translate(${d.dx},${d.dy}) scale(0.5)`
)
.each(function() {
drawSierpinski(d3.select(this), n_more - 1);
});
}
}
drawSierpinski(g, 5);
</script>
The MIT License (MIT)
Copyright (c) 2016 Piotr Migdał
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment