Skip to content

Instantly share code, notes, and snippets.

@curran
Last active May 22, 2018 09:27
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save curran/8b4b7791fc25cfd2c459e74f3d0423f2 to your computer and use it in GitHub Desktop.
Save curran/8b4b7791fc25cfd2c459e74f3d0423f2 to your computer and use it in GitHub Desktop.
Pentagon
license: mit
<!DOCTYPE html>
<head>
<meta charset="utf-8">
<script src="https://d3js.org/d3.v4.min.js"></script>
</head>
<body>
<svg width="960" height="500"></svg>
<script>
const svg = d3.select('svg');
const width = svg.attr('width');
const height = svg.attr('height');
const centerX = width / 2;
const centerY = height / 2;
const numPoints = 5;
const radius = height / 3;
const points = d3.range(numPoints)
.map(i => {
const angle = i / numPoints * Math.PI * 2 + Math.PI;
return {
x: Math.sin(angle) * radius + centerX,
y: Math.cos(angle) * radius + centerY
};
});
const spokes = points.map(point => ({
x1: centerX,
y1: centerY,
x2: point.x,
y2: point.y
}));
const wheelLines = d3.range(numPoints).map(i => ({
x1: points[i].x,
y1: points[i].y,
x2: points[(i + 1) % numPoints].x,
y2: points[(i + 1) % numPoints].y
}));
const lines = spokes.concat(wheelLines);
svg.selectAll('line').data(lines)
.enter().append('line')
.attr('x1', d => d.x1)
.attr('y1', d => d.y1)
.attr('x2', d => d.x2)
.attr('y2', d => d.y2)
.attr('stroke', 'black')
.attr('stroke-width', 3);
</script>
</body>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment