-
Notifications
You must be signed in to change notification settings - Fork 0
/
visual.php
40 lines (33 loc) · 938 Bytes
/
visual.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
<?php
ini_set('display_errors', 'On');
require_once __DIR__ . '/../src/DiamondAndSquare.php';
$gen = new MapGenerator\DiamondAndSquare();
$size = 8;
$gen->setPersistence(1.8);
$gen->setSize($size);
$gen->setMapSeed(uniqid());
$map = $gen->generate();
$image = imagecreatetruecolor($gen->getSize(), $gen->getSize());
$max = 0;
$min = PHP_INT_MAX;
for ($iy = 0; $iy < $gen->getSize(); $iy++) {
for ($ix = 0; $ix < $gen->getSize(); $ix++) {
$h = $map[$iy][$ix];
if ($min > $h) {
$min = $h;
}
if ($max < $h) {
$max = $h;
}
}
}
$diff = $max - $min;
for ($iy = 0; $iy < $gen->getSize(); $iy++) {
for ($ix = 0; $ix < $gen->getSize(); $ix++) {
$h = 255 * ($map[$iy][$ix] - $min) / $diff;
$color = imagecolorallocate($image, $h, $h, $h);
imagesetpixel($image, $ix, $iy, $color);
}
}
imagepng($image, 'visual.png');
imagedestroy($image);