-
Notifications
You must be signed in to change notification settings - Fork 0
/
moca4.php
94 lines (76 loc) · 2.01 KB
/
moca4.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
<?php
class Pixel {
public $x;
public $y;
public $r;
public $g;
public $b;
function __construct( $x, $y, $r, $g, $b ) {
$this->x = $x;
$this->y = $y;
$this->r = $r;
$this->g = $g;
$this->b = $b;
}
function draw() {
global $image;
$color = imagecolorallocate( $image, $this->r, $this->g, $this->b );
imagesetpixel( $image, $this->x, $this->y, $color );
}
}
$width = 1000;
$height = 1000;
$image = imagecreatetruecolor( $width, $height );
$firstPixel = new Pixel( 0, 0, 0, 0, 0 );
$firstPixel->draw();
$currentRow = array( $firstPixel );
for ( $y = 0; $y < $height; $y++ ) {
$previousRow = $currentRow;
$currentRow = array();
// Calculate the current row
foreach ( $previousRow as $x => $previousPixel ) {
// Left pixel
if ( array_key_exists( $x - 1, $currentRow ) ) {
$leftPixel = $currentRow[ $x - 1 ];
$leftPixel->r++;
} else {
$leftPixel = clone $previousPixel;
$leftPixel->x--;
$leftPixel->y++;
$leftPixel->r++;
}
$currentRow[ $leftPixel->x ] = $leftPixel;
// Middle pixel
if ( array_key_exists( $x, $currentRow ) ) {
$middlePixel = $currentRow[ $x ];
$middlePixel->g++;
} else {
$middleX = $previousPixel->x;
$middleY = $previousPixel->y + 1;
$middleR = $previousPixel->r;
$middleG = $previousPixel->g + 1;
$middleB = $previousPixel->b;
$middlePixel = new Pixel( $middleX, $middleY, $middleR, $middleG, $middleB );
}
$currentRow[ $middleX ] = $middlePixel;
// Right pixel
if ( array_key_exists( $x + 1, $currentRow ) ) {
$rightPixel = $currentRow[ $x + 1 ];
$rightPixel->b++;
} else {
$rightX = $previousPixel->x + 1;
$rightY = $previousPixel->y + 1;
$rightR = $previousPixel->r;
$rightG = $previousPixel->g;
$rightB = $previousPixel->b + 1;
$rightPixel = new Pixel( $rightX, $rightY, $rightR, $rightG, $rightB );
}
$currentRow[ $rightX ] = $rightPixel;
}
foreach ( $currentRow as $currentPixel ) {
$currentPixel->draw();
}
$previousRow = $currentRow;
}
header( 'Content-Type: image/png' );
imagepng( $image );