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


V shades = [‘.’, ‘:’, ‘!’, ‘*’, ‘o’, ‘e’, ‘&’, ‘#’, ‘%’, ‘@’]
 
F dotp(v1, v2)
   V d = dot(v1, v2)
   R I d < 0 {-d} E 0.0
 
F draw_sphere(r, k, ambient, light)
   L(i) Int(floor(-r)) .< Int(ceil(r) + 1)
      V x = i + 0.5
      V line = ‘’
 
      L(j) Int(floor(-2 * r)) .< Int(ceil(2 * r) + 1)
         V y = j / 2 + 0.5
         I x * x + y * y <= r * r
            V vec = normalize((x, y, sqrt(r * r - x * x - y * y)))
            V b = dotp(light, vec) ^ k + ambient
            V intensity = Int((1 - b) * (:shades.len - 1))
            line ‘’= I intensity C 0 .< :shades.len {:shades[intensity]} E :shades[0]
         E
            line ‘’= ‘ ’
 
      print(line)
 
V light = normalize((30.0, 30.0, -50.0))
draw_sphere(20, 4, 0.1, light)
draw_sphere(10, 2, 0.4, light)