Log In  

Hi, I was wondering if someone could explain this code from the manual for converting the cos and sin functions into ones which use radiens rather than gradiens and uninvert sin.

cos1 = cos function cos(angle) return cos1(angle/(3.1415*2)) end
        sin1 = sin function sin(angle) return sin1(-angle/(3.1415*2)) end

I understand the maths elements it's more just the lua syntax here. And does this replace the functionality of sin and cos or add new functions cos1 and sin1 with the new functionality?
Many thanks

P#37641 2017-02-20 11:22 ( Edited 2017-02-21 02:41)

It might help to split it up:

cos1 = cos

Copies cos() to cos1()

function cos(angle) return cos1(angle/(3.1415*2)) end

Now that cos() has been copied, we can safely override the standard functionality. We use the old cos (now cos1) to convert to radians.

I have no idea why the two lines they can be squished to fit one line.
And to answer your question: Yes; this replaces the standard cos() and sin()

P#37642 2017-02-20 11:47 ( Edited 2017-02-20 16:48)

lua doesn't actually need line separators or semicolons (usually) because its syntax is designed so that it's unambiguous (usually) when one statement ends and the next begins. line breaks are for the benefit of human readers.

you can read more about lua's syntax in section 3 of the lua manual. you can find specific mention of the only (as far as i'm aware) ambiguity in section 3.3.1.

P#37650 2017-02-20 13:19 ( Edited 2017-02-20 18:19)

Thanks a lot that makes it a lot clearer thank you.

P#37665 2017-02-20 20:01 ( Edited 2017-02-21 01:01)

@helado oh, i wasn't aware of that. Only just learned lua for Pico 8. That explains quite a lot of what i thought was weird one-of syntax.

P#37671 2017-02-20 21:41 ( Edited 2017-02-21 02:41)

[Please log in to post a comment]

Follow Lexaloffle:          
Generated 2024-04-19 14:28:12 | 0.009s | Q:14