In order to test out my new BT keyboard (and given that Codea was designed to look like the Processing language) I decided to convert one of the Processing trigonemtry examples to Lua.
The orginal example can be found at http://processing.org/learning/trig/ and is a useful guide to anyone learning about sine, cosine etc, the lua code is given below.
--[[ Sine Console
* Processing: Creative Coding and
* Computational Art
* By Ira Greenberg */
* Ported to Lua by Jon Howard (techdojo@gmail.com)
* Original post http://processing.org/learning/trig/
--]]
local px, py, px2, py2
local angle = 0
local angle2 = 0
local radius = 100
local frequency = 2
local frequency2 = 2
local x=0
local x2=0
function setup()
displayMode(FULLSCREEN)
end
function draw()
translate(-50,(HEIGHT/2)-radius/2) -- move the drawing to the center of the screen
background (127)
noStroke()
fill(255)
ellipse(WIDTH/8, 75, radius, radius)
--rotates rectangle around circle
px = WIDTH/8 + math.cos(math.rad(angle))*(radius/2);
py = 75 + math.sin(math.rad(angle))*(radius/2);
rectMode(CENTER)
fill(255, 0, 0, 255)
rect (px, py, 10, 10) -- draw the rect on the edge of the circle
stroke(222, 0, 255, 255)
strokeWidth(5)
line(WIDTH/8, 75, px, py)
angle2 = 0
fill(0)
-- draw static curve - y = sin(x)
for i=1,WIDTH do
px2 = WIDTH/8 + math.cos(math.rad(angle2))*(radius/2);
py2 = 75 + math.sin(math.rad(angle2))*(radius/2);
point(WIDTH/8+radius/2+i, py2)
angle2 = angle2 - frequency2
end
-- send small ellipse along sine curve
-- to illustrate relationship of circle to wave
noStroke()
fill(8, 0, 255, 255)
ellipse(WIDTH/8+radius/2+x, py, 15, 15)
angle = angle - frequency
x = x + 1
-- when little ellipse reaches end of window
-- reinitialize some variables
if (x>= WIDTH-60) then
x = 0
angle = 0
end
-- draw dynamic line connecting circular
-- path with wave
strokeWidth(3)
stroke(0, 255, 66, 255)
line(px, py, WIDTH/8+radius/2+x, py)
-- output some calculations
fill(0)
textAlign(LEFT)
text("y = sin x", 180, 225)
text("px = " .. px, 180, 205)
text("py = " .. py, 180, 185)
end