2015-09-27 57 views
-1

我想繪製一個使用PHP的十六進制。我一直在努力遵循http://www.redblobgames.com/grids/hexagons/的手冊,支持自己使用leland hex代類(https://github.com/pushcx/leland/blob/master/class_hex_image.php)。如何在PHP中繪製扁平十六進制(標題)圖像?

不幸我的「十六進制」看起來是這樣的:

enter image description here

能否請您指點我究竟做錯了,或者告訴我怎樣才能建立一個適當的十六進制?

在我看來,那功能,抓住六角角落無法正常工作:

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

我的錯誤是微不足道的。

在線路

$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);