你好 Stackoverflow 社区,
对于一个小游戏,我需要显示一个八边形(像这个)
形状会自行适应我从数据库中获得的某些值。我的问题是,我完全不知道如何启动它。我既不知道我的目的的公式,也不知道如何在 PHP 中绘制这样的形状。
一般来说,我比较擅长 PHP。所以我会对解决方案的理论方法而不一定是代码感到高兴=)
提前致谢
把这个搞砸了。它已经为您计算了坐标,但您可以轻松地在$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);
我不能给你一个公式,但是一旦你想出了一个公式,你就可以使用GD扩展并绘制你的形状。
Google Charts API支持这一点,并且相当容易使用。 例子