Tag Archives: function

How to add your own functions to “gc”

Hi all,

As you know, in order to do graphical things in Lua on the Nspire platform, you have to deal with gc and its methods TI created, like drawString, drawRect, fillArc, etc.

Well, what if you wanted to make a drawRoundRect routine ?

You could certainely do something like :

function drawRoundRect(gc, x, y, wd, ht, rd)
        if rd > ht/2 then rd = ht/2 end
        gc:drawLine(x + rd, y, x + wd - (rd), y)
        gc:drawArc(x + wd - (rd*2), y + ht - (rd*2), rd*2, rd*2, 270, 90)
        gc:drawLine(x + wd, y + rd, x + wd, y + ht - (rd))
        gc:drawArc(x + wd - (rd*2), y, rd*2, rd*2,0,90)
        gc:drawLine(x + wd - (rd), y + ht, x + rd, y + ht)
        gc:drawArc(x, y, rd*2, rd*2, 90, 90)
        gc:drawLine(x, y + ht - (rd), x, y + rd)
        gc:drawArc(x, y + ht - (rd*2), rd*2, rd*2, 180, 90)
end
 
function on.paint(gc)
        drawRoundRect(gc, 100, 50, 20, 15, 5)
        ...
end

Indeed, that works.

But wouldn’t it be cool to actually have it like, “gc:drawRoundRect(100,50,20,15,5)”, so it can feel way more natural and wouldn’t need you to explicitely pass gc as an argument ? 😉
Well, here is the definitive solution to this, by Jim Bauwens 😉

function AddToGC(key, func)
        local gcMetatable = platform.withGC(getmetatable)
        gcMetatable[key] = func
end
 
local function drawRoundRect(gc, x, y, wd, ht, rd)
        -- the code above
end
 
AddToGC("drawRoundRect", drawRoundRect)
 
function on.paint(gc)
        gc:drawRoundRect(100, 50, 20, 15, 5)
        ...
end

One more thing :

You may have noticed the use platform.withGC, which is an API “2.0”+ (OS 3.2+) function. Here’s how to “recreate” it for earlier versions :

if not platform.withGC then
        platform.withGC = function(func, ...)
            local gc = platform.gc()
            gc:begin()
            func(..., gc)
            gc:finish()
        end
    end

[Update] : John Powers from TI commented that this definition of platform.withGC has some limitations, and proposed this better version (thanks !) :

if not platform.withGC then
    function platform.withGC(f)
        local gc = platform.gc()
        gc:begin()
        local result = {f(gc)}
        gc:finish()
        return unpack(result)
    end
end

TabVar v3, the ultimate function analysis tool !

Hi everybody,

Today is a great day!

Indeed, for the lucky owners of a TI-Nspire with OS >= 3, JayTe has made an awesome program that combines the simplicity of the TI-Basic with the power of Lua, for our pleasure …
Here is … TabVar 3 !
Yes, the famous program that makes all your work in depth and draws a beautiful variations table (“Tableau de variations” in french), the perfect function analysis tool !!

“Hmm, Doesn’t that exist already ?” You might say.
It does, actually, The idea is not new.
Chronologically, it has indeed been made by Adriweb, the first one for Nspire, and then, TabVar v1 and v2, by JayTe, whose engine is based originally on Adriweb’s program, but everything was done much further by integrating more and better function analysis functions and algorithms.

This program exceeds by far its competitors. No other .tns could can hope to compete so far.

The outstanding feature of this version is that this program (launched exclusively on TI-Planet) deeply uses the features offered by the OS 3 by integrating a dynamic Lua script for an optimal view of the variation table, which for once is completely graphical. No need to have weird graphical hacks to correctly display what we need …

We will not make you wait any longer… Here’s a screenshot of TabVar:

and details of preliminary calculations (click for larger image):

So far, you will not find a more reliable engine (although nothing is perfect, of course, complex functions can be studied incompletely …)
You can, for example, try fairly complex functions, such as polynomial, rational, with or without asymptote(s) and strange behavior with or without limits …
In short, for a high school student (or other!) It’s more than perfect 🙂

Download link: http://tiplanet.org/forum/archives_voir.php?id=3751

Thank you Who?
Thank you Jayte!

Do not forget, this program is by far the most reliable and practical for the studies of functions ..
A small screenshot to prove that point ?
(Example : TabVar v3 against a competitor for the same function f(x)=1/x)

Yep, do not trust bad imitations if you want a good grade!

TI-Planet.org, the reference website to find everything you need and what you dream of!