Page 1 of 2

LUA Progress bar

Unread postPosted: 30 Oct 2014, 11:14
by FrederikTheisen
Hi there

I'm working on a project where I need to find the distance between up to 2000 points in 3D space.
It takes a lot of time on the nSpire CX calculator, so I wanted to make a progress bar, showing how far it was in the process.

Here is the code for the loading:
Code: Select all
function getMaxDist()
   maxDist = 0
   for i = 1,molTable["nAtoms"] do
      for j = 1,molTable["nAtoms"] do
         local dist = math.sqrt((molTable[i]["x"] - molTable[j]["x"])^2  +  (molTable[i]["y"] - molTable[j]["y"])^2  +  (molTable[i]["z"] - molTable[j]["z"])^2)
         if dist > maxDist then
            maxDist = dist
         end
      end
   end
end


It's not finished, but you get the idea.

But I'm not able to update the screen while I'm inside the loop.
It only updates once it has finished.

Is there any possible way to make a loading bar in LUA on the nSpire CX?

regards
Frederik

Re: LUA Progress bar

Unread postPosted: 30 Oct 2014, 11:26
by Ti64CLi++
I don't know.
Why will you do a progress bar?

Re: LUA Progress bar

Unread postPosted: 30 Oct 2014, 11:32
by Bisam
1) Ton algorithme de recherche est inefficace : tu fais 2 fois plus de boulot que nécessaire. Tu peux t'arrêter à j = i-1 dans ta 2ème boucle puisque la distance de i à j est la même que celle de j à i.
2) Tu devrais localiser les fonctions que tu utilises souvent, comme la fonction "math.sqrt".
3) Encore mieux, tu devrais créer une fonction qui renvoie la distance entre 2 atomes, directement...

1) Your search algorithm is inefficient : you work twice as needed. You can stop your second loop at j=i-1 as the distance between i and j is the same as the distance between j and i.
2) You should localize the functions used very often, as for "math.sqrt".
3) Even better, you should create a function returning the distance between 2 atoms, directly...

Re: LUA Progress bar

Unread postPosted: 30 Oct 2014, 11:50
by Ti64CLi++
Salut Bisam
pourquoi parle-tu en français alors qu'il ne comprends peut-être pas le français.

Re: LUA Progress bar

Unread postPosted: 30 Oct 2014, 11:52
by Bisam
J'ai répondu en français ET en anglais...

Re: LUA Progress bar

Unread postPosted: 30 Oct 2014, 11:55
by Ti64CLi++
Je ne vois pas d'anglais pourtant.

Re: LUA Progress bar

Unread postPosted: 30 Oct 2014, 11:56
by Bisam
Regarde mieux !

Re: LUA Progress bar

Unread postPosted: 30 Oct 2014, 14:11
by Ti64CLi++
a oui mais mon ordinateur bug et donc des fois il cache les liens.

Re: LUA Progress bar

Unread postPosted: 30 Oct 2014, 14:35
by Jens_K
Hi, I once messed around with progressbars using coroutines. I tried to use that principle with your algorithm (and I optimized it a bit ;) ) and it seems to work:
Code: Select all
function getMaxDist()
   local sqrt = math.sqrt
   local maxDist = 0
   local dist
   local nAtoms = molTable["nAtoms"]
   for i = 1,nAtoms-1 do
      for j = i+1,nAtoms do
         dist = sqrt((molTable[i]["x"] - molTable[j]["x"])^2  +  (molTable[i]["y"] - molTable[j]["y"])^2  +  (molTable[i]["z"] - molTable[j]["z"])^2)
         if dist > maxDist then
            maxDist = dist
         end
      end
      coroutine.yield(i/(nAtoms-1))
   end
end

CRgetMaxDist=coroutine.create(getMaxDist)
local progress=0
local finished=false

timer.start(0.01)
function on.timer()
    platform.window:invalidate()
    _, progress = coroutine.resume(CRgetMaxDist)
    if progress==1 then
        timer.stop()
        finished=true
    end
end

function on.paint(gc)
    if finished then
        gc:drawString("finished",50,50)
    else
        gc:fillRect(0,100,318*progress,10)
    end
end


Edit: It's now optimized, so that it measures the distanced between the same 2 points only once ;)

Re: LUA Progress bar

Unread postPosted: 30 Oct 2014, 17:12
by FrederikTheisen
Wow, thanks a bunch! :)
I'm going to work on this once I get home.

I know that the code is not optimized (not checking double will be a nice fix for sure).
Though many o the constants are declared (as local) outside the code snippet i shared (program is currently 700 lines + 8000 lines database)

I'm not familiar with the coroutine 'function', but I hope that it will address the problem.