How to resolve the algorithm Draw a sphere step by step in the Wren programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Draw a sphere step by step in the Wren 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 Wren programming language
Source code in the wren programming language
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)
You may also check:How to resolve the algorithm Accumulator factory step by step in the Phix programming language
You may also check:How to resolve the algorithm Non-decimal radices/Convert step by step in the Icon and Unicon programming language
You may also check:How to resolve the algorithm Roots of a function step by step in the C programming language
You may also check:How to resolve the algorithm Forward difference step by step in the AutoHotkey programming language
You may also check:How to resolve the algorithm Babbage problem step by step in the PL/I programming language