Task

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.



Related tasks

 draw a cuboid
 draw a rotating cube
 write language name in 3D ASCII
 draw a Deathstar


var shades = ".:!*oe&#\%@"
var light = [30, 30, -50]
 
var normalize = Fn.new { |v|
    var len = (v[0]*v[0] + v[1]*v[1] + v[2]*v[2]).sqrt
    for (i in 0..2) v[i] =  v[i] / len
}
 
var dot = Fn.new { |x, y|
    var d = x[0]*y[0] + x[1]*y[1] + x[2]*y[2]
    return (d < 0) ? -d : 0
}
 
var drawSphere = Fn.new { |r, k, ambient|
    var vec = [0] * 3
    for (i in (-r).floor..r.ceil) {
        var x = i + 0.5
        for (j in (-2*r).floor..(2*r).ceil) {
            var y = j/2 + 0.5
            if (x*x + y*y <= r*r) {
                var vec = [x, y, (r*r - x*x - y*y).sqrt]
                normalize.call(vec)
                var b = dot.call(light, vec).pow(k) + ambient
                var intensity = ((1 - b) * (shades.count - 1)).truncate
                if (intensity < 0) intensity = 0
                if (intensity >= shades.count - 1) intensity = shades.count - 2
                System.write(shades[intensity])
            } else {
                System.write(" ")
            }
        }
        System.print()
    }
}
 
normalize.call(light)
drawSphere.call(20, 4, 0.1)
drawSphere.call(10, 2, 0.4)