Skip to content

Instantly share code, notes, and snippets.

@munaf-zz
Created October 13, 2013 05:37
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 munaf-zz/6958534 to your computer and use it in GitHub Desktop.
Save munaf-zz/6958534 to your computer and use it in GitHub Desktop.
Hexagon Construction
{"description":"Hexagon Construction","endpoint":"","display":"svg","public":true,"require":[],"fileconfigs":{"inlet.js":{"default":true,"vim":false,"emacs":false,"fontSize":12},"_.md":{"default":true,"vim":false,"emacs":false,"fontSize":12},"config.json":{"default":true,"vim":false,"emacs":false,"fontSize":12}},"fullscreen":false,"play":false,"loop":false,"restart":false,"autoinit":true,"pause":true,"loop_type":"pingpong","bv":false,"nclones":15,"clone_opacity":0.4,"duration":3000,"ease":"linear","dt":0.01,"thumbnail":"http://i.imgur.com/dr7BNup.png"}
var svg = d3.select('svg');
var x = 269,
y = 186,
r = 120;
var centers = [
{x: x, y: y},
{x: x + r, y: y},
{x: x + 2*r, y: y},
{x: x + r/2, y: y + Math.sqrt((r*r)-(r/2)*(r/2))},
{x: x + 3*r/2, y: y + Math.sqrt((r*r)-(r/2)*(r/2))}
];
var guideCircles = svg.selectAll('circle').data(centers);
guideCircles.enter()
.append('circle')
.attr({
cx: function(d) { return d.x },
cy: function(d) { return d.y },
r: r,
fill: 'rgba(255,255,255,0.1)',
stroke: '#888888',
'stroke-width': 1
});
// Draw the connecting lines
var lineColor = '#15C981',
lineWidth = 3;
var points = [
{x: x, y: y},
{x: x+r/2, y: y - Math.sqrt((r*r)-(r/2)*(r/2))},
{x: x+3*r/2, y: y - Math.sqrt((r*r)-(r/2)*(r/2))},
{x: x+2*r, y: y},
{x: x+3*r/2, y: y + Math.sqrt((r*r)-(r/2)*(r/2))},
{x: x+r/2, y: y + Math.sqrt((r*r)-(r/2)*(r/2))},
{x: x, y: y}
];
var lineFunction = d3.svg.line()
.x(function(d) { return d.x; })
.y(function(d) { return d.y; });
svg.append('path')
.attr({
'd': lineFunction(points),
'fill': 'none',
'stroke': lineColor,
'stroke-width': '4'
});
var dots = svg.selectAll('circle.points').data(points);
dots.enter().append('circle').classed('points', true)
.attr({
cx: function(d) { return d.x; },
cy: function(d) { return d.y; },
r: 5,
fill: 'none',
'stroke': '#000000',
'stroke-width': 2
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment