How to resolve the algorithm Draw a sphere step by step in the D programming language

Published on 12 May 2024 09:40 PM
#D

How to resolve the algorithm Draw a sphere step by step in the D programming language

Table of Contents

Problem Statement

Draw a sphere. The sphere can be represented graphically, or in ASCII art, depending on the language capabilities. Either static or rotational projection is acceptable for this task.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Draw a sphere step by step in the D programming language

Source code in the d programming language

import std.stdio, std.math, std.algorithm, std.numeric;

alias V3 = double[3];
immutable light = normalize([30.0, 30.0, -50.0]);

V3 normalize(V3 v) pure @nogc {
    v[] /= dotProduct(v, v) ^^ 0.5;
    return v;
}

double dot(in ref V3 x, in ref V3 y) pure nothrow @nogc {
    immutable double d = dotProduct(x, y);
    return d < 0 ? -d : 0;
}

void drawSphere(in double R, in double k, in double ambient) @nogc {
    enum shades = ".:!*oe&#%@";
    foreach (immutable i; cast(int)floor(-R) .. cast(int)ceil(R) + 1) {
        immutable double x = i + 0.5;
        foreach (immutable j; cast(int)floor(-2 * R) ..
                              cast(int)ceil(2 * R) + 1) {
            immutable double y = j / 2. + 0.5;
            if (x ^^ 2 + y ^^ 2 <= R ^^ 2) {
                immutable vec = [x, y, (R^^2 - x^^2 - y^^2) ^^ 0.5]
                                .normalize;
                immutable double b = dot(light, vec) ^^ k + ambient;
                int intensity = cast(int)((1 - b) * (shades.length-1));
                intensity = min(shades.length - 1, max(intensity, 0));
                shades[intensity].putchar;
            } else
                ' '.putchar;
        }
        '\n'.putchar;
    }
}

void main() {
    drawSphere(20, 4, 0.1);
    drawSphere(10, 2, 0.4);
}


  

You may also check:How to resolve the algorithm Apply a callback to an array step by step in the Wart programming language
You may also check:How to resolve the algorithm Sockets step by step in the Raku programming language
You may also check:How to resolve the algorithm Tau number step by step in the Phix programming language
You may also check:How to resolve the algorithm Minesweeper game step by step in the Common Lisp programming language
You may also check:How to resolve the algorithm Count in octal step by step in the Batch File programming language