使用PHP显示动态八边形

时间:2011-04-07 21:02:14

标签: php graphics gd formula gdlib

Hello Stackoverflow社区,

对于一个小游戏我需要显示一个八角形(Like this one

形状适应我从数据库获得的某些值。我的问题是,我完全不知道如何进入它。我既不知道我的目的公式,也不知道如何用PHP绘制这样的形状。

总的来说,我比较擅长PHP。所以我对解决方案的理论方法感到高兴,而不一定是代码=)

提前致谢

3 个答案:

答案 0 :(得分:1)

我无法为你提供一个公式,但是一旦你找到了一个,你可以使用GD扩展并画出你的形状。

答案 1 :(得分:1)

鞭打了这个。它已经为您计算了坐标,但您可以在 $ vertices 数组中轻松指定自己的坐标(并删除生成)。

<?php
$radius = 100;
$sides = 8;

$points = array();
for ($i = 1; $i <= $sides; $i++) {
    $points[] = round( $radius * cos($i*2 * pi() / $sides) + $radius );  // x
    $points[] = round( $radius * sin($i*2 * pi() / $sides) + $radius );  // y
}



// Draw the image.
$im = imagecreate($radius*2 + 10, $radius*2 + 10);
$black = imagecolorallocate($im, 0, 0, 0);
$white = imagecolorallocate($im, 255, 255, 255);

imagefill($im, 0, 0, $white);  // White background

imagefilledpolygon($im, $points, $sides, $black);

header('Content-type: image/png');
imagepng($im);

答案 2 :(得分:0)

Google Charts API支持这一点,而且相当容易使用。 Example