Draw a sphere: Difference between revisions

Stronger D code
(Improved formatting D code)
(Stronger D code)
Line 158:
<lang d>import std.stdio, std.math, std.algorithm, std.numeric;
 
alias double[3] light = [30, 30, -50]V3;
V3 light = [30, 30, -50];
 
void normalize(ref double[3]V3 v) pure {
double lenv[] /= sqrt(dotProduct(v[], v[])) ^^ 0.5;
v[0] /= len; v[1] /= len; v[2] /= len;
}
 
double dot(const ref double[3]V3 x, const ref double[3]V3 y) pure nothrow {
immutable double d = dotProduct(x[], y[]);
return d < 0 ? -d : 0;
}
 
void drawSphere(in double R, in double k, in double ambient) {
enum shades = ".:!*oe&#%@";
foreach (int i; cast(int)floor(-R) .. cast(int)ceil(R) + 1) {
constimmutable double x = i + 0.5;
foreach (int j; cast(int)floor(-2*R)..cast(int)ceil(2*R)+1){
constimmutable double y = j / 2. + 0.5;
if (x ^^ 2 + y ^^ 2 <= R ^^ 2) {
double[3]V3 vec = [x, y, (R^^2 - x^^2 - y^^2) ^^ 0.5];
vec.normalize();
constimmutable double b = dot(light, vec) ^^ k + ambient;
int intensity = cast(int)((1-b) * (shades.length-1));
intensity = min(shades.length-1, max(intensity, 0));
Line 191:
 
void main() {
light.normalize(light);
drawSphere(20, 4, 0.1);
drawSphere(10, 2, 0.4);
Anonymous user