Created
July 8, 2015 09:16
-
-
Save julianwachholz/dbda19c04ba3c2859f06 to your computer and use it in GitHub Desktop.
Generates a Megagon SVG
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env node | |
'use strict'; | |
var size = 10000; | |
var sides = 1000000; | |
var padding = 50; | |
var path = polygon(size / 2 + padding, size / 2 + padding, size / 2, sides) | |
.map(function (p) { return p.x + ',' + p.y; }) | |
.join(' '); | |
process.stdout.write('\ | |
<?xml version="1.0" standalone="yes"?>\n\ | |
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN"\n\ | |
"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">\n\ | |
<svg width="1000" height="1000" viewBox="0 0 ' + (size + padding * 2) + ' ' + (size + padding * 2) + '"\n\ | |
xmlns="http://www.w3.org/2000/svg" version="1.1">\n\ | |
<polygon style="fill:none;stroke:#000000;stroke-width:2.5px"\n\ | |
points="' + path + '" />\n\ | |
</svg>\n\ | |
'); | |
/** | |
* calculate regular polygon coordinates | |
* @returns {Point[]} | |
*/ | |
function polygon(center_x, center_y, radius, sides) { | |
var start_angle, center_angle, vertices; | |
sides = Math.round(sides); | |
center_angle = 2 * Math.PI / sides; | |
if (sides % 2 === 1) { | |
start_angle = Math.PI / 2; // 12 o'clock | |
} else { | |
start_angle = Math.PI / 2 - center_angle / 2; | |
} | |
return times(sides, function (i) { | |
var angle = start_angle + (i * center_angle); | |
return new Point( | |
get_x(center_x, radius, angle), | |
get_y(center_y, radius, angle) | |
); | |
}); | |
} | |
function times(n, iterator) { | |
var accum = Array(Math.max(0, n)); | |
for (var i = 0; i < n; i++) accum[i] = iterator.call(undefined, i); | |
return accum; | |
} | |
function Point(x, y) { | |
this.x = x; | |
this.y = y; | |
} | |
function get_x(center_x, radius, angle) { | |
return center_x + radius * Math.cos(angle); | |
} | |
function get_y(center_y, radius, angle) { | |
return center_y - radius * Math.sin(angle); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment