Current Location: Home> Latest Articles> How to calculate regular polygon vertex coordinates for imageopenpolygon()

How to calculate regular polygon vertex coordinates for imageopenpolygon()

M66 2025-05-29

In PHP, using the GD library to handle graphics is a very common requirement. Especially in graphical programming, drawing regular polygons is a basic operation. This article will explain how to calculate the vertex coordinates of a regular polygon and use the imageopenpolygon function to draw a regular polygon.

1. Calculate the coordinates of regular polygon vertexes

A regular polygon refers to a polygon with equal edges and angles. For a regular polygon, we can calculate the coordinates of each vertex through a polar coordinate system.

Suppose we want to draw a regular n- side, and the center point coordinate of this polygon is (cx, cy) and the radius is r , we can calculate the coordinates of each vertex through the following formula:

  • The angle interval of each vertex is 360/n degrees

  • The angle of the kth vertex is 2 * pi * k / n

  • The coordinates of the vertex are:

    • x = cx + r * cos(angle)

    • y = cy + r * sin(angle)

2. PHP implementation code

PHP is used to calculate the vertex coordinates and draw regular polygons using the imageopenpolygon function. Here is the complete code for the implementation:

 <?php
// Set the width and height of the image
$width = 400;
$height = 400;

// Create image resources
$image = imagecreatetruecolor($width, $height);

// Set background colors
$bgColor = imagecolorallocate($image, 255, 255, 255); // White
imagefill($image, 0, 0, $bgColor);

// Set the color of the polygon
$polygonColor = imagecolorallocate($image, 0, 0, 255); // blue

// The center and radius of the polygon
$cx = $width / 2;
$cy = $height / 2;
$r = 100; // radius
$n = 6; // Number of edges(Regular hexagon)

// Calculate the coordinates of each vertex
$points = [];
for ($i = 0; $i < $n; $i++) {
    $angle = 2 * pi() * $i / $n;
    $x = $cx + $r * cos($angle);
    $y = $cy + $r * sin($angle);
    $points[] = $x;
    $points[] = $y;
}

// useimageopenpolygonFunction drawing regular polygons
imagepolygon($image, $points, $n, $polygonColor);

// Output image to browser
header('Content-Type: image/png');
imagepng($image);

// Release image resources
imagedestroy($image);
?>

3. Code analysis

  1. Image creation and background settings : First, an image with a width and height of 400x400 is created through the imagecreatetruecolor function. Then, use imagecolorallocate to set the background color to white.

  2. Vertex coordinate calculation : We calculate the coordinates of each vertex of a regular polygon based on the formula mentioned above. Calculate the x and y coordinates of each vertex through the cos and sin functions and save these coordinates in an array $points .

  3. Draw regular polygons : The imagepolygon function takes an array containing all vertex coordinates and draws a polygon based on these coordinates.

  4. Output image : Use imagepng to output the image to the browser and finally view the drawn regular polygon in the browser.

4. Summary

Through the above steps, we successfully compute the vertex coordinates of the regular polygon and draw the regular polygon using the imagepolygon function in PHP. This method can be extended to any regular polygon, and you can draw polygons of different sizes and shapes by simply adjusting the number of edges n and radius r .