我正在尝试使用PHP绘制十六进制。我一直在努力遵循http://www.redblobgames.com/grids/hexagons/的手册来支持我自己的leland hex generation class(https://github.com/pushcx/leland/blob/master/class_hex_image.php)。
不幸的是,我的“十六进制”看起来像这样:
你能告诉我我做错了什么或告诉我如何创建一个合适的十六进制?
在我看来,抓住十六角的功能无法正常工作:
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);
}
答案 0 :(得分:0)
我的错误很简单。
排队
$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);