15
14

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

「プログラムでシダを描画する」をPHPで描画する

Posted at

ん?流行ってんの?

シダ

<?php

function W1x($x, $y) { return 0.836 * $x + 0.044 * $y; };
function W1y($x, $y) { return -0.044 * $x + 0.836 * $y + 0.169; };
function W2x($x, $y) { return -0.141 * $x + 0.302 * $y; };
function W2y($x, $y) { return 0.302 * $x + 0.141 * $y + 0.127; };
function W3x($x, $y) { return 0.141 * $x - 0.302 * $y; };
function W3y($x, $y) { return 0.302 * $x + 0.141 * $y + 0.169; };
function W4x($x, $y) { return 0; };
function W4y($x, $y) { return 0.175337 * $y; };

function f($im, $k, $x, $y, $width, $height) {
    if ($k > 0) {
        foreach (range(1, 4) as $i) {
            if ($i === 1 || mt_rand(0, 9) < 3) {
                f(
                    $im,
                    $k - 1,
                    call_user_func("W{$i}x", $x, $y),
                    call_user_func("W{$i}y", $x, $y),
                    $width,
                    $height
                );
            }
        }
    } else {
        imagesetpixel(
            $im,
            $x * 490 + $width * 0.5,
            $height - $y * 490,
            imagecolorallocate($im, 0, 128, 0)
        );
    }
}

$im = imagecreatetruecolor(500, 500);
imagefill($im, 0, 0, imagecolorallocate($im, 255, 255, 255));
f($im, 20, 0, 0, 500, 500);
header('Content-Type: image/png');
imagepng($im);
15
14
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
15
14

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?