如何在 PHP 中绘制平顶十六进制(标题)图像?

How to draw flat topped hex (title) image in PHP?

我正在尝试使用 PHP 绘制一个十六进制。我一直在尝试遵循 http://www.redblobgames.com/grids/hexagons/ supporting myself with leland hex generation class (https://github.com/pushcx/leland/blob/master/class_hex_image.php) 的手册。

不幸的是我的 "hex" 看起来像这样:

你能告诉我我做错了什么或者告诉我如何创建一个合适的十六进制吗?

在我看来,抓取六角的函数不能正常工作:

  function hex_corners($center, $size)
    {
        $points = array();
        for($i=0; $i <= 5; $i++)
        {
            $deg = 60 * $i; // Oblicz kąt, w którym znajduje sie róg hexu
            $rad = pi() / 180 * $deg; // Przelicz kąt na radiany
            $points[$i] = array_push($points, $center['x'] + $this->size / 2 * cos($rad), $center['y'] + $this->size / 2 * sin($rad)); 
        }
        return($points);
    }

但我试图模仿 http://www.redblobgames.com/grids/hexagons/ 手册中描述的那个:

function hex_corner(center, size, i):
    var angle_deg = 60 * i  
    var angle_rad = PI / 180 * angle_deg
    return Point(center.x + size * cos(angle_rad),
                 center.y + size * sin(angle_rad))

我正在使用以下绘制函数:

public function hex_draw($x, $y)
    {
        $this->hex = imagecreatetruecolor ($this->size , $this->size);
        $center['x'] = $this->size / 2;
        $center['y'] = $this->size / 2;
        $blue = imagecolorallocate($this->hex, 0, 0, 255);
        // Get points
        $points = $this->hex_corners($center, $this->size);
        //die($print_r($points);
        imagefilledpolygon($this->hex, $points, count($points)/2,  $blue);
        // flush image
        header('Content-type: image/png');
        imagepng($this->hex);   
        imagedestroy($this->hex);
    }   

我的错误是微不足道的。

排队

 $points[$i] = array_push($points, $center['x'] + $this->size / 2 * cos($rad), $center['y'] + $this->size / 2 * sin($rad));

有不需要的 [$i] 导致了问题。

可以使用以下代码正确生成十六进制:

<?php

class hexmapimage
{

    private $size = 100;
    private $hex;

    public function hex_draw($x, $y)
    {
        $this->hex = imagecreatetruecolor ($this->size , $this->size);
        $center['x'] = $this->size / 2;
        $center['y'] = $this->size / 2;
        $black = imagecolorallocate($this->hex, 0, 0, 0);
        $blue = imagecolorallocate($this->hex, 0, 0, 255);
        $transparent =  imagecolortransparent ($this->hex, $black);
        // Get points
        $points = $this->hex_corners($center, $this->size);
        imagefilledrectangle($this->hex, 0, 0, $this->size, $this->size, $transparent);
        imagefilledpolygon($this->hex, $points, count($points)/2,  $blue);
        // flush image
        header('Content-type: image/png');
        imagepng($this->hex);   
        imagedestroy($this->hex);
    }   

    /*
    * $center = array(x coordinate of center of hex, y coordinate of center of hex)
    * $size = int (size of hex)
    */
    function hex_corners($center, $size)
    {
        $points = array();
        for($i=0; $i <= 5; $i++)
        {
            $deg = 60 * $i; // Oblicz kąt, w którym znajduje sie róg hexu
            $rad = deg2rad($deg); // Przelicz kąt na radiany
            array_push($points, $center['x'] + $this->size / 2 * cos($rad), $center['y'] + $this->size / 2 * sin($rad)); 
        }
        return($points);
    }
}

$hex = new hexmapimage();
$hex->hex_draw(0, 0);