Anonymous user
Draw a sphere: Difference between revisions
Improved formatting D code
(Added D version) |
(Improved formatting D code) |
||
Line 157:
{{trans|C}}
<lang d>import std.stdio, std.math, std.algorithm, std.numeric;
enum shades = ".:!*oe&#%@";▼
double[3] light = [30, 30, -50];
void normalize(ref double[3] v) {
double len = sqrt(dotProduct(v[], v[]));
v[0] /= len; v[1] /= len; v[2] /= len;
}
double dot(const ref double[3] x, const ref double[3] y) {
double d = dotProduct(x[], y[]);
return d < 0 ? -d : 0;
}
void
▲ enum shades = ".:!*oe&#%@";
foreach (int i; cast(int)floor(-R) .. cast(int)ceil(R) + 1) {
const double x = i + 0.5;
foreach (int j; cast(int)floor(-2
const double y = j / 2. + 0.5;
if (x
double[3] vec = [x, y,
vec.normalize();
const double b = dot(light, vec) ^^ k + ambient;
int intensity = cast(int)((1
intensity = min(shades.length-1, max(intensity, 0));
putchar(shades[intensity]);
Line 191 ⟶ 189:
}
}
void main() {
normalize(light);
}</lang>
|