Skip to content

Instantly share code, notes, and snippets.

@jsharkey13
Created March 30, 2020 13:06
Show Gist options
  • Save jsharkey13/2558f5bb9ff65479098c5be68def2987 to your computer and use it in GitHub Desktop.
Save jsharkey13/2558f5bb9ff65479098c5be68def2987 to your computer and use it in GitHub Desktop.
Generate the points for a hexagon in an SVG image
// See https://stackoverflow.com/questions/52172067/create-svg-hexagon-points-with-only-only-a-length
const round_4dp = function(x) {return Math.round(x*10000)/10000};
const radius = 125;
const height = 260;
const width = 220;
const points = [0, 1, 2, 3, 4, 5, 6].map((n, i) => {
var angle_deg = 60 * i - 30;
var angle_rad = Math.PI / 180 * angle_deg;
return [round_4dp(width/2 + radius * Math.cos(angle_rad)), round_4dp(height/2 + radius * Math.sin(angle_rad))];
});
const hex1 = points.map((p) => [p[0], p[1]].join(',')).join(' ');
console.log(hex1);
// Then create a <polygon points="..." />
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment