Created
May 30, 2012 12:29
-
-
Save Bri-G/2835952 to your computer and use it in GitHub Desktop.
Text genertion of a sphere
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- code from http://rosettacode.org/wiki/Draw_a_sphere | |
-- tickled slightly for Codea by Bri_G | |
-- various authors under GNU Free Documentation Licence 1.2 | |
-- http://www.gnu.org/licenses/fdl-1.2.html | |
-- Use this function to perform your initial setup | |
saveProjectInfo("Description", "Text Sphere") | |
saveProjectInfo("Version", "1.001 beta") | |
saveProjectInfo("Author", "Bri_G") | |
function setup() | |
displayMode(FULLSCREEN) | |
shades = {'.', ':', '!', '*', 'o', 'e', '&', '#', '%', '@'} | |
light = normalize{30, 30, -50} | |
end | |
-- This function gets called once every frame | |
function draw() | |
-- This sets a dark background color | |
background(0, 0, 0, 255) | |
-- Do your drawing here | |
draw_sphere (20, 4, 0.1) | |
-- draw_sphere (10, 2, 0.4) | |
end | |
function normalize (vec) | |
len = math.sqrt(vec[1]^2 + vec[2]^2 + vec[3]^2) | |
return {vec[1]/len, vec[2]/len, vec[3]/len} | |
end | |
function dot (vec1, vec2) | |
d = vec1[1]*vec2[1] + vec1[2]*vec2[2] + vec1[3]*vec2[3] | |
return d < 0 and -d or 0 | |
end | |
function draw_sphere (radius, k, ambient) | |
-- starting height for printing | |
ln = 950 | |
for i = math.floor(-radius),-math.floor(-radius) do | |
x = i + .5 | |
local line = '' | |
for j = math.floor(-2*radius),-math.floor(-2*radius) do | |
y = j / 2 + .5 | |
if x^2 + y^2 <= radius^2 then | |
vec = normalize{x, y, math.sqrt(radius^2 - x^2 - y^2)} | |
b = dot(light,vec) ^ k + ambient | |
intensity = math.floor ((1 - b) * #shades) | |
line = line .. (shades[intensity] or shades[1]) | |
else | |
line = line .. ' ' | |
end | |
end | |
ln = ln - 14 | |
font("courier") | |
fill(255, 255, 255, 255) | |
fontSize(12) | |
text(line,384,ln) | |
end | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi All,
Whilst struggling, as ever, in getting my head round spatial geometry (trying to draw a sphere in 3D) - I stumbled on something that amused me (http://rosettacode.org/wiki/Draw_a_sphere). Whilst digesting the reference I noted someone had kindly posted a Lua version of the code involved. This was virtually 100 percent transferable to Codea - just a little tickling from me. I hope you find the code of interest.