Skip to content

Instantly share code, notes, and snippets.

@JWally
Last active August 29, 2015 14:07
Show Gist options
  • Save JWally/b511fdd16e57fa442ba3 to your computer and use it in GitHub Desktop.
Save JWally/b511fdd16e57fa442ba3 to your computer and use it in GitHub Desktop.
Geometry Stuff
// Quick and dirty function for calculating the interior angle
// of each bend of a polygon
var polySide = function(sides){
return ((sides - 2) * 180) / sides;
}
// Another geometry bender
// expects starting coordinates
// to dome in a d3 friendly {x: x, y: y} array
var newCoord = function(coords, angle, length, rotation){
var radian = Math.PI / 180,
rotate = 0,
x = coords.x,
y = coords.y;
var y1 = y + Math.sin((angle + rotation) * radian) * length;
var x1 = x + Math.cos((angle + rotation) * radian) * length;
return {x: x1, y: y1};
}
// Make fun shapes
var shaper = function(coords,sides, length){
var angle = polySide(sides),
ary = [coords];
for(var i = 0; i < sides; i++ ){
coords = newCoord(coords, angle, length, angle * i);
ary.push(coords);
}
return ary;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment