# Trig example

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

--rotates rectangle around circle
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
angle2 = angle2 - frequency2
end

-- send small ellipse along sine curve
-- to illustrate relationship of circle to wave
noStroke()
fill(8, 0, 255, 255)
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)

-- output some calculations
fill(0)
textAlign(LEFT)
text("y = sin x",   180, 225)
text("px = " .. px, 180, 205)
text("py = " .. py, 180, 185)
end
``````

Very nice trigonometry example. Thanks for sharing it!

A good understanding of trig is essential for many sorts of real-world coding problems - excellent. I wish I had these sorts of things when I was in high schoolâŚ

Simpoll but coooool :-bd

Yes, thanksâanswers my rhetorical question elsewhere of what âpointâ does. (Neither Lua nor Codea references contain it, so Processing is where Iâd have had to look for a definition. And yet âlineâ, âellipseâ, ârectâ appear in the online reference.)

Yeah that confused me for a while as well, I just copied the code and it ran, it was only when I was dissecting it after that I discovered point was undocumented, although youâll notice a MASSIVE speed up if you just comment the point call out.

I completely forgot that I implemented point and pointSize. I am now reminded to add the documentation.

(point was always on the verge of being removed â given that you can just use an ellipse, but maybe itâs convenient to have)

Just a thought, but I remember correctly isnât the ellipse drawn using a shader?
Would it be faster to implement point either as a simple rectangle or use a fixed texture of a circle - would that make it faster?

@Simeon, thanks. (and Iâll utilize the bug list in future!)

I suppose, as my graphics textbook points out, you could always draw a point as a line between the same points. But that might have a performance hit too, or other unintended consequences, or limit capabilities that a point has over a line. Depends on the contextâdonât know what it would do here, I havenât tried it. Still just at the learning stage and playing with other things!

A line between two identical points may, at some point (HA!) be opimized-away.

I just use a tiny rect. Simple, quick, and you know it wonât get nuked.

@Bortels,@SimeonâIâm a little worried with the discussion of what might get dropped/unimplemented/optimized out of Codea. If I write code that relies on these features (whether optimized, intended or otherwise) and you nuke the supporting structures or methods, you nuke my code tooânot just clean up Codea. Fixing bugs and optimizing code is one thing.

@kfin63 we definitely keep this in mind when making changes to the API. Generally if something is deprecated it is left in but removed from the documentation and autocomplete to discourage its use.

It is less an issue of TLL dropping something, and more an issue of âFuture versions of OpenGL may behave differentlyâ. Point being - if you want a dot, draw a dot; avoid depending on quirks of the current OpenGL implementation.

Ah gotcha, thanks, I will keep this in mind. Just never sure whatâs Codea, OpenGL, Lua, Processing, C (math.h?)

(@Bortels, I see what you did there with âpoint beingâ

PointâŚ HA! I didnât even notice that when I typed it. I SLAY ME.

Surely there is irony in whether line, rect, or ellipse is the best way to draw a point.

The one that takes the least time to execute or calculate is the best. To heck with standards and âsimplifyingâ things, I donât really care who does it. Deprecating âpointâ because an ellipse can draw one is not maybe the best reason to do so.

But removing or deprecating the ability to draw a âlineâ to two identical points, and yet still let ârectâ (and âellipseâ?) do that very thingâwell, thatâs just crazy, unless thereâs some good reason for it. How would I find out that suspected reason? Why should I reasonably expect that ârectâ will not get treated in that manner, but âlineâ might?

Itâs just far from obvious to a beginner why one should be encouraged to use one method that way, and yet another even seemingly closer in concept would be discouraged.

Then draw a line. If Iâm wrong, youâre good to go.

I will continue to draw something most definitely non-zero in size, because Iâm old.

I do appreciate the help, I also want to future-proof anything I may come up with, but ensure if possible that I am using the âbestâ or most efficient method to draw a point when I choose to do so. (Iâm still futzing with my mistakes with scope.)

That the âpointâ really wasnât required because âellipseâ can do it then raised the question (someone elseâs) whether ellipse was suitable due to shading was raised by someone else, and then ârectâ came into the picture.

At the end of itâis ârectâ the approved, not-likely-to-be-deprecated, way to draw a point outside of a specific image context? (or just stick to images where Get and Set are available? Iâm old too, I have much less time to spend on this than Iâd like!)

Thereâs no real âapprovedâ, really, other than avoids ellipses in codea because they can be quite slow.

@kfin63 we will keep point. I like having the function. I think we might change the shader for it to something more efficient in the future, though (at the moment I believe it uses the ellipse shader).