Skip to content

Instantly share code, notes, and snippets.

@kulbakin
Last active December 18, 2015 18:39
Show Gist options
  • Save kulbakin/5826982 to your computer and use it in GitHub Desktop.
Save kulbakin/5826982 to your computer and use it in GitHub Desktop.
JavaScript function to compose coordinates of svg path to represent a star
/**
* Compose coordinates of svg path to represent a star
* @param int size
* @return string
*/
var d3_star = function (size) {
var d18 = Math.PI / 10,
d36 = d18 * 2,
sind18 = Math.sin(d18),
cosd18 = Math.cos(d18),
tand18 = sind18 / cosd18,
sind36 = Math.sin(d36),
cosd36 = Math.cos(d36),
tand36 = sind36 / cosd36;
var a = Math.sqrt(size / (5 * tand18 * (1 + tand18 / tand36))),
b = a * tand18 / tand36,
c = a * tand18,
d = Math.sqrt(a * a + c * c),
e = Math.sqrt(b * b + c * c),
ab = a + b,
cd = c + d;
return 'M0,' + -ab +
'L' + c + ',' + -b +
' ' + cd + ',' + -b +
' ' + e * cosd18 + ',' + e * sind18 +
' ' + ab * sind36 + ',' + ab * cosd36 +
' 0,' + e +
' ' + -ab * sind36 + ',' + ab * cosd36 +
' ' + -e * cosd18 + ',' + e * sind18 +
' ' + -cd + ',' + -b +
' ' + -c + ',' + -b +
'Z';
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment