Skip to content

Instantly share code, notes, and snippets.

@sfaut
Last active January 9, 2023 07:15
Show Gist options
  • Save sfaut/dc24360dbc10f1b4374e24d94b8eab79 to your computer and use it in GitHub Desktop.
Save sfaut/dc24360dbc10f1b4374e24d94b8eab79 to your computer and use it in GitHub Desktop.
Creates a polygon SVG with n vertices and a top apex
<?php
// Author : sfaut <https://github.com/sfaut>
// Publication date : 2022-09-06
// Usage :
// <img src="polygon-svg.php?vertices=5" alt="A SVG pentagon!">
$vertices = (int)$_GET['vertices'] ?? 5;
$start = 3 * M_PI / 2; // Apex en haut, pas M_PI / 2 car coordonnées SVG (-1, -1) en haut à gauche
$angle = $start;
$points = [];
for ($i = 0; $i < $vertices; $i++) {
$points[] = [cos($angle), sin($angle)];
$angle += (2 * M_PI) / $vertices;
}
$points = implode(' ', array_map(fn ($p) => implode(',', $p), $points));
header('Content-Type: image/svg+xml');
?>
<?xml version="1.1" standalone="yes"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
<svg version="1.1" xmlns="http://www.w3.org/2000/svg" height="500" width="500" viewBox="-1 -1 2 2">
<polygon points="<?= $points ?>"/>
</svg>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment