Trivial distraction

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.

-- 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
 

Bri_G

:slight_smile:

The original rosettacode.org version of this code is the base code I used for my sphere drawing in my Pic class, and much-modified is what I’m using for the Procedural Planets stuff.

Nice. Thanks for sharing your “tickled” code.

Newbie question:

How do you determine which lines not to include in Codea when using Lau examples?

Hi Keebo,

I think it’s just down to experience. Working on small examples building up your familiarity with the language features and syntax. Together with understanding the system used by Lua in it’s operating cycle and the three main functions provided - Setup(), Draw() and Touch(). Experience in other programming helps - like Basic, JavaScript and others.

Probably the best resource you get is the support on this forum from other users, learners and experts alike. Don’t hesitate to ask for help and contribute any code you write.

Good luck with it.

Bri_G

:wink:

I notice no-one’s put a TeX version there. Something to work on, perhaps.

There’s sphere-drawing code in my 3D shape explorer. I think that this is where 3D meshes really come into their own.

Hi Bortels,

When you say original code, do you mean the C code written earlier. I’ve trailed the net and found lots of interesting code, but like many things, the contributors have all had different approaches which can confuse. Translating from different languages doesn’t help.

Once I’ve cracked my sphere problem I can move onto other ideas. At least with each problem I dig deeper into Lua. Can’t wait for 1.4 tho’ so that I can tidy up my projects. From the hints given by Simeon 1.4 will be a big step forward.

By the way I like your Planet work, very impressive.

Bri_G

:slight_smile:

Thanks for the reply, Bri_G.

I have used only VB5 and WDL, C-Sript, and C-Lite from 3D GameStudio before trying to learn Codea and Lua.

Here’s to hoping for the best.

Cool text shader ^:)^

Hi Andrew,

Downloaded your 3D shape explorer - very impressive. It seems to have all I’ll need to complete my project, but will take a bit of digesting.

Do you ever get cramp from typing - the scripts are excellent but so long. Have you any copies without annotation? My iPad groaned a little when running, must be pushing the memory limit.

Thanks for the code - I’ll post my results, hopefully soon.

Bri_G

=D>

Hi Guys an’ Gals,

Back with the trivia I’m afraid. Picked up on a cute printing capability, which I’ll explore later - when I get time, and tried it on the pad. It worked so I built a little flash screen and added colour cycling to effect. Again - it’s trivial but I liked it. Code below.

I’m sure many of you can improve on it, especially the colour cycling. I remember the days when you booted up the Atari ST and were presented with the Atari logo with a colour cycled background. Similar, but better effects generated on the Amiga.

So, have a play and if you produce a masterpiece post it for us to view.

-- Use this function to perform your initial setup

function setup()
	saveProjectInfo("Description", "Flash Screen")
	saveProjectInfo("Version", "1.001 beta")
	saveProjectInfo("Author", "Bri_G")     
    print(string.char(0xe2,0x98,0x83))
    snow = string.char(0xe2,0x98,0x83)
    displayMode(FULLSCREEN)
	-- iparameter("posX",40,100,60)
	-- iparameter("posY",40,100,60)   
    posX = 80  
    posY = 95     
    counter = 0
    increment = 3
end

-- This function gets called once every frame
function draw()
	-- This sets a dark background color  
	background(counter, counter, counter, 255)
	fill(240, 240, 240, 255)
	fontSize(102)
	-- This sets the line thickness
	strokeWidth(5)
	for loop = 1,9 do
		for loop2 = 1,10 do
			fill(loop*20+70,loop2*20+50,loop*loop2*3)
			text(snow,posX*loop,posY*loop2)
		end
	end
	counter = counter + increment
	if counter > 255 then
		counter = 255
		increment = -3
	elseif counter < 0 then
		counter = 0
		increment = 3
	end           
end

Have fun.

Bri_G

:wink:

@bri_g - by “original code”, I mean the ASCII-text Lua sphere drawing example on rosettacode - the same one you adapted. That’s what I found when I was looking to draw a sphere, and it was straightforward to replace the text output with img:set and colors.