Current Location: Home> Latest Articles> Draw coordinate point markings to assist in debugging polygonal graphics

Draw coordinate point markings to assist in debugging polygonal graphics

M66 2025-05-29

When processing images using PHP, the imageopenpolygon function is a very useful tool that can be used to draw polygons. However, when debugging complex polygonal graphics, simply drawing lines may not be clear enough. At this time, marking coordinate point numbers next to each vertex can greatly improve the efficiency of visualization and debugging.

Below we will introduce step by step how to draw dot-numbered polygons using PHP.

1. Preparation

First, you need to make sure that your PHP environment has enabled GD library support. You can confirm by:

 <?php
if (function_exists('gd_info')) {
    echo "GDThe library is enabled";
} else {
    echo "GDLibrary not enabled";
}
?>

If not enabled, open extension=gd in php.ini .

2. Create a canvas and draw a polygon

Here is a complete example of code that draws polygons and labels the sequence number next to each point:

 <?php
// Create a canvas
$width = 500;
$height = 500;
$image = imagecreatetruecolor($width, $height);

// Define the color
$white = imagecolorallocate($image, 255, 255, 255);
$black = imagecolorallocate($image, 0, 0, 0);
$red   = imagecolorallocate($image, 255, 0, 0);

// Fill the background
imagefill($image, 0, 0, $white);

// Define the point of a polygon(Even number,Every two numbers are a groupx, y)
$points = [
    100, 100,
    200, 80,
    300, 150,
    250, 300,
    150, 250
];

// Draw open polygons
imageopenpolygon($image, $points, count($points) / 2, $black);

// Note the point number next to each point
for ($i = 0; $i < count($points); $i += 2) {
    $x = $points[$i];
    $y = $points[$i + 1];
    $label = 'P' . ($i / 2);
    
    // Mark text
    imagestring($image, 3, $x + 5, $y + 5, $label, $red);
    
    // Small dots can be drawn to represent coordinates
    imagefilledellipse($image, $x, $y, 6, 6, $red);
}

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

// Save to file
// imagepng($image, '/path/to/save/mypolygon.png');

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

3. Results analysis

This code will:

  • Create a 500×500 white background canvas

  • Draw an unenclosed polygon using imageopenpolygon

  • Draw a small red dot at each vertex

  • Mark the dot numbers next to the small red dots with red, such as P0, P1, P2, etc.

If you want the image to be saved directly instead of output, you can uncomment the line imagepng($image, '/path/to/save/mypolygon.png'); and modify the save path as needed.

For example, you can change the path to:

 imagepng($image, 'https://m66.net/uploads/mypolygon.png');

(Note: In fact, imagepng can only be saved to the local path of the server. If you want to access it through a URL, the file must be placed in the corresponding server directory.)

4. Practical application scenarios

This technique is ideal for:

  • Develop and debug polygon area division

  • Mark polygonal areas in the map system

  • Comparison of the positions of different vertices when visualizing data

  • Graphic Algorithm Teaching Demonstration

By adding numbers to each point in the polygon, the arrangement order and logical relationship of each point can be intuitively seen, greatly improving the efficiency of development and debugging.

summary

By combining imageopenpolygon , imagestring and imagefilledelipse , we can easily draw polygon figures with coordinate numbers in PHP. This not only improves the readability of the graphics, but also facilitates subsequent logical processing and error troubleshooting.