/
mandlebrot_terminal.php
executable file
·62 lines (52 loc) · 1.37 KB
/
mandlebrot_terminal.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
<?php
$maxIterations = 50;
$step = 1/20;
$topLeft = array(-1.5, 1);
$bottomRight = array(0.5, -1);
if (isset($_GET['zoom'])) {
$zoom = (float) $_GET['zoom'];
$topLeft = zoom($topLeft, $zoom);
$topLeft = panLeft($topLeft, $zoom*0.25);
$bottomRight = zoom($bottomRight, $zoom);
$bottomRight = panLeft($bottomRight, $zoom*0.25);
$step = $step / $zoom;
echo '<head><META
HTTP-EQUIV="Refresh"
CONTENT="1; URL=mandlebrot_terminal.php?zoom='.($zoom+0.1).'" > </head>';
echo '<body>';
}
if ('cli' != php_sapi_name()) {
echo "<pre><center>";
}
for ($imaginary = $topLeft[1]; $imaginary > $bottomRight[1]; $imaginary = $imaginary - $step) {
for ($real = $topLeft[0] ; $real < $bottomRight[0]; $real = $real + $step) {
$z = new ComplexNumber(0,0);
$c = new ComplexNumber($real, $imaginary);
$iterationCount = 0;
while ($z->lessThanTwo() && $iterationCount < $maxIterations) {
$z->square();
$z->add($c);
++$iterationCount;
}
if ($iterationCount >= $maxIterations) {
echo '*';
} else {
echo ' ';
}
}
echo PHP_EOL;
}
if ('cli' != php_sapi_name()) {
echo "</centre></pre>";
}
function zoom($coordinates, $factor) {
$result = array();
foreach ($coordinates as $key => $coord) {
$result[$key] = $coord / $factor;
}
return $result;
}
function panLeft($coordinates, $offset) {
$coordinates[0] = $coordinates[0] - $offset;
return $coordinates;
}