Anonymous user
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]
V3 light = [30, 30, -50];
void normalize(ref
}
double dot(const ref
immutable double d = dotProduct(x
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) {
foreach (int j; cast(int)floor(-2*R)..cast(int)ceil(2*R)+1){
if (x ^^ 2 + y ^^ 2 <= R ^^ 2) {
vec.normalize();
int intensity = cast(int)((1-b) * (shades.length-1));
intensity = min(shades.length-1, max(intensity, 0));
Line 191:
void main() {
light.normalize(
drawSphere(20, 4, 0.1);
drawSphere(10, 2, 0.4);
|