Skip to content

Instantly share code, notes, and snippets.

@fitnr
Last active December 18, 2016 02:56
Show Gist options
  • Select an option

  • Save fitnr/78eae04eafd7b95fa8d8 to your computer and use it in GitHub Desktop.

Select an option

Save fitnr/78eae04eafd7b95fa8d8 to your computer and use it in GitHub Desktop.
Hypocycloids
<!DOCTYPE html>
<meta charset="utf-8">
<html>
<head>
<title>hypotrochoids or epitrochoid</title>
<script src="http://d3js.org/d3.v3.min.js"></script>
<script src="https://raw.githubusercontent.com/mbostock/d3/master/lib/colorbrewer/colorbrewer.js"></script>
<style type="text/css">
.hypo {
fill: none;
stroke-width: 1px;
}
.circ {
fill: none;
stroke: #222;
stroke-width: 0.25px;
}
</style>
</head>
<body>
<script type="text/javascript">
var
R = 365.242374,
r = -29.53059,
// r = 365,
h = r,
// rose
// n = 10,
// R = (2 * n * h) / (n + 1),
// r = (n - 1) * h / (n + 1),
width = 1000,
height = width,
scale = 1.05;
var line = d3.svg.line()
.x(function(d) { return d.x; })
.y(function(d) { return d.y; })
.interpolate("monotone");
var colorscale = d3.scale.ordinal()
.domain([0, 2 * Math.PI])
.range(colorbrewer.RdYlBu[11]);
var color = d3.scale.category20();
var metonic = d3.range(0, 235);
var
epitrochoid_x = function(phi) { return (R + r) * Math.cos(phi) - h * Math.cos((R + r) * phi / r); },
epitrochoid_y = function(phi) { return (R + r) * Math.sin(phi) - h * Math.sin((R + r) * phi / r); },
hypotrochoid_x = function(phi) { return (R - r) * Math.cos(phi) + h * Math.cos((R - r) * phi / r); },
hypotrochoid_y = function(phi) { return (R - r) * Math.sin(phi) - h * Math.sin((R - r) * phi / r); };
var choid = function(type) {
var x, y;
if (type === 'hypotrochoid') {
x = hypotrochoid_x;
y = hypotrochoid_y;
}
else if (type === 'epitrochoid') {
x = epitrochoid_x;
y = epitrochoid_y;
}
return function(n) {
var start = 2 * n * Math.PI * r / R,
end = 2 * (n + 1) * Math.PI * r / R;
return line(
d3.range(start, end, 0.01).map(function(phi) {
return {
x: x(phi),
y: y(phi),
};
})
);
};
};
var epi = choid('epitrochoid'),
hypo = choid('hypotrochoid');
var svg = d3.select("body").append("svg")
.attr('class', 'content')
.attr("width", width)
.attr("height", height);
var g1 = svg
.append("g")
.attr("transform", "translate(" + width / 2 + "," + height / 2 + ")" + " scale(" + scale + ")");
var g2 = svg
.append("g")
.attr("transform", "translate(" + width / 2 + "," + height / 2 + ")" + " scale(" + scale + ")");
g1
.selectAll('path')
.data(metonic)
.enter()
.append('path')
.attr('class', 'hypo')
.attr('stroke', function(d){ return color(d); })
.attr('d', function(d) { return hypo(d); });
// g2
// .selectAll('path')
// .data(metonic)
// .enter()
// .append('path')
// .attr('class', 'hypo')
// .attr('stroke', function(d){ return color(d); })
// .attr('d', function(d) { return epi(d); });
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment