【问题标题】:displaying a dynamic octagon using PHP使用 PHP 显示动态八边形
【发布时间】:2011-04-07 21:02:13
【问题描述】:

你好 Stackoverflow 社区,

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

形状会自动适应我从数据库中获取的某些值。我的问题是,我完全不知道如何启动它。我既不知道我的目的的公式,也不知道如何在 PHP 中绘制这样的形状。

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

提前致谢

【问题讨论】:

  • google 是你的朋友我的朋友 :-)
  • @Neal 我先尝试了 google =) 找不到有关此主题的任何内容。你能给我一个提示吗=D?

标签: php graphics gd formula gdlib


【解决方案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】:

    我不能给你一个公式,但是一旦你想出了一个公式,你就可以使用GD 扩展并绘制你的形状。

    【讨论】:

      【解决方案3】:

      Google Charts API 支持这个,并且相当容易使用。 Example

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-01-26
        • 1970-01-01
        • 2022-01-09
        • 2015-01-24
        • 1970-01-01
        • 2021-08-06
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多