note 49429 added to function.imageline
| From: | eviloverlord at gmail dot com | Date: | Thu, 27 Jan 2005 15:50:04 +0000 |
| Subject: | note 49429 added to function.imageline | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-84079@lists.php.net to get a copy of this message | ||
This code is used to draw a board of hexagons (for games, classes, etc.)
<?php
//Draws a hexagonal board
// User-defined values
$maxTiles = 7; //The number of tiles at the center (widest part) of the board
$minTiles = 4; //The number of tiles at the edges of the board
$side = 30; //The length of the sides of the tiles in pixels
$bgColor = array(0, 0, 0); //The background color in RGB format
$fgColor = array(255, 255, 255);//The foreground color in RGB format
//Calculated values
$widthInTiles = range($maxTiles, $minTiles); //In our example: 7, 6, 5, 4
$rowsInTiles = count($widthInTiles)*2-1; //the total number of rows on our board
$xSide = $side*sin(deg2rad(60)); //the length of the x-part of the angled sides
$ySide = $side*sin(deg2rad(30)); //the length of the y-part of the angled sides
$boardWidth = $xSide*$widthInTiles[0]*2; //The entire width of the board
$boardHeight = $rowsInTiles*($side + $ySide) + $ySide; //The entire height of the board
// create a blank image and allocate the foreground, background colors
$image = imagecreate($boardWidth, $boardHeight);
$bg = imagecolorallocate($image, $bgColor[0], $bgColor[1], $bgColor[2]);
$fg = imagecolorallocate($image, $fgColor[0], $fgColor[1], $fgColor[2]);
// draw the board
$row = 0;
foreach($widthInTiles as $tiles)
{
for ($i = 0; $i < $tiles+1; $i++)
{
$x1 = $row*$xSide + $i*$xSide*2;
$y1 = $boardHeight/2;
$y1Dif = ($side/2) + $row*($side+$ySide);
$x2 = $x1 + $xSide;
$y2 = $y1;
$y2Dif = $ySide;
$x3 = $x2 + $xSide;
if ($i < $tiles)
{
imageline($image, $x1, $y1 - $y1Dif, $x2, $y2 - $y1Dif - $y2Dif, $fg);
imageline($image, $x1, $y1 + $y1Dif, $x2, $y2 + $y1Dif + $y2Dif, $fg);
imageline($image, $x2, $y2 - $y1Dif - $y2Dif, $x3, $y1 - $y1Dif, $fg);
imageline($image, $x2, $y2 + $y1Dif + $y2Dif, $x3, $y1 + $y1Dif, $fg);
}
imageline($image, $x1, $y1 - $y1Dif, $x1, $y1 - $y1Dif + $side, $fg);
imageline($image, $x1, $y1 + $y1Dif, $x1, $y1 + $y1Dif - $side, $fg);
}
$row++;
}
// output the picture
header("Content-type: image/png");
imagepng($image);
imagedestroy($image);
?>
----
Manual Page -- http://www.php.net/manual/en/function.imageline.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+49429
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+49429&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+49429&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+49429&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+49429&report=yes&reason=useless
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+49429&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+49429&report=yes
Search -- http://master.php.net/manage/user-notes.php