Polar Graph Drawer

This is my first Codea project, it draws polar graphs. There is limited complexity due to the main purpose of it being for revision. I may add more trigonometric functions in the future, feel free to play around and use my code. Also any tips on how to write neater code would be appreciated.

http://pastebin.com/RWSMf67y

Screenshots
http://imgur.com/tAzRhaQ
http://imgur.com/V4aV0lJ

You can make the images display directly in your post with ![alt text](url)

nice program. Maybe: make it work in landsacpe too?

I think I’m being stupid but I can’t seem to do it right for that to happen ~X(

Thank you! Yeah was something I was thinking about doing just didn’t know where to begin restructuring the layout. Thanks for the suggestion though.

@TheHam_

You can’t get imgur images to display directly (at least, I cant). I use photobucket for that reason.

A couple of suggestions, mainly for speed, because even if it runs fast now, you may add more features later that slow it down (and speed never hurts).

  1. You are always drawing a lot of stuff in the same place on the screen, text and lines. It is more efficient to draw all the permanent stuff to a full screen image in memory as part of setup, then you simply sprite the image for each frame. (The command setContext tells Codea to draw to an image instead of the screen).

  2. Inside the main draw loop, try to minimise unnecessary calculations, especially trig functions. For example

--in setup
pi2=math.pi*2
step=pi2/3600

--in GetPoints
for rad=0,pi2,step do --no need for math.rad conversions now
    local c,s=math.cos(n*rad),math.sin(n*rad)
    --now use c,s everywhere you were calculating cos and sin
    --in other words, only calculate everything once

    --in this case where you have lots of alternatives
    if trigIndex==5 then 
        aMax=5
        nMax=10
        out=a+b*(math.sin(n*rad))^2
--It is neater to put the constants in a table in setup, eg 
nMax={10,10,0,0,10,10}
--so in GetPoints you can just use nMax[trigIndex]
--that is more a neatness thing than efficiency, but..
-- try to avoid power functions, so write 
out=a+b*s*s --uses our s variable from above, times itself, no powers!
  1. There is also a more subtle trick you should try, if and when speed becomes an issue. It is much faster to use local variables than global variables, especially if they have to be looked up in a table, like math.sin and math.cos.

Try setting

local sin,cos=math.sin,math.cos

at the very top of your code, before setup, then use these functions in place of the originals and see what difference it makes. To measure the difference, you need a measure of frame speed. Just search for FPS on the forum for some methods to do this.

Ahh that makes a lot of sense, the drw part was my biggest concern but didn’t know how to deal with it, thank you for the tips they’re very helpful

If you want go even faster, pre calculate all the sin and cos values and put them in a table in setup, then your draw loops just look them up in sequence.