LoginSignup
14

More than 5 years have passed since last update.

posted at

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

ん?流行ってんの?

シダ

<?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);

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
What you can do with signing up
14