Repository navigation
Expand file tree
/
Copy paththreedeeplot_random.d
More file actions
50 lines (44 loc) · 1.51 KB
/
Copy paththreedeeplot_random.d
File metadata and controls
50 lines (44 loc) · 1.51 KB
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
@safe: // Make @safe the default for this file, enforcing memory-safety.
import std.stdio, std.string, std.math, std.range, std.conv, std.random, std.algorithm;
void main()
{
enum width = 80;
writeln(center("3D Plot", width));
writeln(center("(After Creative Computing Morristown, New Jersey)\n\n", width));
enum functions = ["30.0 * exp(-z * z / 100.0)",
"sqrt(900.01 - z * z) * .9 - 2",
"30 * (cos(z / 16.0) + .5)",
"30 - 30 * sin(z / 18.0)",
"30 * exp(-cos(z / 16.0)) - 30",
"30 * sin(z / 10.0)"];
size_t index = uniform(0, functions.length);
writeln(center("f(z) = " ~ functions[index], width), "\n");
float fna(float z)
{
final switch (index)
{
static foreach (i, f; functions)
case i:
mixin("return " ~ f ~ ";");
}
}
char[] row;
for (float x = -30.0; x <= 30.0; x += 1.5)
{
size_t max_z = 0L;
auto y1 = 5 * lrint((sqrt(900 - x * x)) / 5.0);
for (float y = y1; y >= -y1; y -= 5)
{
auto z = to!size_t(max(0, lrint(25 + fna(sqrt(x * x + y * y)) - .7 * y)));
if (z > max_z) // Visible
{
max_z = z;
if (z + 1 > row.length) // row needs to grow
row ~= ' '.repeat(z + 1 - row.length).array;
row[z] = '*';
}
}
writeln(row);
row = null;
}
}