π
<-

LUA Progress bar

Pour TI-Nspire OS 3.0 ou ultérieur.

LUA Progress bar

Unread postby FrederikTheisen » 30 Oct 2014, 11:14

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
User avatar
FrederikTheisen
Niveau 2: MI2 (Membre Initié)
Niveau 2: MI2 (Membre Initié)
Level up: 13.3%
 
Posts: 4
Joined: 30 Oct 2014, 02:01
Gender: Not specified
Calculator(s):
MyCalcs profile

Re: LUA Progress bar

Unread postby Ti64CLi++ » 30 Oct 2014, 11:26

I don't know.
Why will you do a progress bar?
Image
User avatar
Ti64CLi++Modo
Niveau 16: CC2 (Commandeur des Calculatrices)
Niveau 16: CC2 (Commandeur des Calculatrices)
Level up: 32.5%
 
Posts: 3446
Images: 75
Joined: 04 Jul 2014, 14:40
Location: Clermont-Ferrand 63
Gender: Male
Calculator(s):
MyCalcs profile
Class: ENS Rennes
GitHub: Ti64CLi

Re: LUA Progress bar

Unread postby Bisam » 30 Oct 2014, 11:32

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...
User avatar
BisamAdmin
Niveau 15: CC (Chevalier des Calculatrices)
Niveau 15: CC (Chevalier des Calculatrices)
Level up: 69.6%
 
Posts: 5670
Joined: 11 Mar 2008, 00:00
Location: Lyon
Gender: Male
Calculator(s):
MyCalcs profile

Re: LUA Progress bar

Unread postby Ti64CLi++ » 30 Oct 2014, 11:50

Salut Bisam
pourquoi parle-tu en français alors qu'il ne comprends peut-être pas le français.
Image
User avatar
Ti64CLi++Modo
Niveau 16: CC2 (Commandeur des Calculatrices)
Niveau 16: CC2 (Commandeur des Calculatrices)
Level up: 32.5%
 
Posts: 3446
Images: 75
Joined: 04 Jul 2014, 14:40
Location: Clermont-Ferrand 63
Gender: Male
Calculator(s):
MyCalcs profile
Class: ENS Rennes
GitHub: Ti64CLi

Re: LUA Progress bar

Unread postby Bisam » 30 Oct 2014, 11:52

J'ai répondu en français ET en anglais...
User avatar
BisamAdmin
Niveau 15: CC (Chevalier des Calculatrices)
Niveau 15: CC (Chevalier des Calculatrices)
Level up: 69.6%
 
Posts: 5670
Joined: 11 Mar 2008, 00:00
Location: Lyon
Gender: Male
Calculator(s):
MyCalcs profile

Re: LUA Progress bar

Unread postby Ti64CLi++ » 30 Oct 2014, 11:55

Je ne vois pas d'anglais pourtant.
Image
User avatar
Ti64CLi++Modo
Niveau 16: CC2 (Commandeur des Calculatrices)
Niveau 16: CC2 (Commandeur des Calculatrices)
Level up: 32.5%
 
Posts: 3446
Images: 75
Joined: 04 Jul 2014, 14:40
Location: Clermont-Ferrand 63
Gender: Male
Calculator(s):
MyCalcs profile
Class: ENS Rennes
GitHub: Ti64CLi

Re: LUA Progress bar

Unread postby Bisam » 30 Oct 2014, 11:56

Regarde mieux !
User avatar
BisamAdmin
Niveau 15: CC (Chevalier des Calculatrices)
Niveau 15: CC (Chevalier des Calculatrices)
Level up: 69.6%
 
Posts: 5670
Joined: 11 Mar 2008, 00:00
Location: Lyon
Gender: Male
Calculator(s):
MyCalcs profile

Re: LUA Progress bar

Unread postby Ti64CLi++ » 30 Oct 2014, 14:11

a oui mais mon ordinateur bug et donc des fois il cache les liens.
Image
User avatar
Ti64CLi++Modo
Niveau 16: CC2 (Commandeur des Calculatrices)
Niveau 16: CC2 (Commandeur des Calculatrices)
Level up: 32.5%
 
Posts: 3446
Images: 75
Joined: 04 Jul 2014, 14:40
Location: Clermont-Ferrand 63
Gender: Male
Calculator(s):
MyCalcs profile
Class: ENS Rennes
GitHub: Ti64CLi

Re: LUA Progress bar

Unread postby Jens_K » 30 Oct 2014, 14:35

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 ;)
Last edited by Jens_K on 30 Oct 2014, 20:43, edited 1 time in total.
User avatar
Jens_K
Niveau 0: MI (Membre Inactif)
Niveau 0: MI (Membre Inactif)
Level up: 0%
 
Posts: 7
Joined: 16 Oct 2014, 11:07
Gender: Male
Calculator(s):
MyCalcs profile

Re: LUA Progress bar

Unread postby FrederikTheisen » 30 Oct 2014, 17:12

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.
User avatar
FrederikTheisen
Niveau 2: MI2 (Membre Initié)
Niveau 2: MI2 (Membre Initié)
Level up: 13.3%
 
Posts: 4
Joined: 30 Oct 2014, 02:01
Gender: Not specified
Calculator(s):
MyCalcs profile

Next

Return to Nspire-Lua

Who is online

Users browsing this forum: No registered users and 4 guests

-
Search
-
Social TI-Planet
-
Featured topics
Comparaisons des meilleurs prix pour acheter sa calculatrice !
"1 calculatrice pour tous", le programme solidaire de Texas Instruments. Reçois gratuitement et sans aucune obligation d'achat, 5 calculatrices couleur programmables en Python à donner aux élèves les plus nécessiteux de ton lycée. Tu peux recevoir au choix 5 TI-82 Advanced Edition Python ou bien 5 TI-83 Premium CE Edition Python.
Enseignant(e), reçois gratuitement 1 exemplaire de test de la TI-82 Advanced Edition Python. À demander d'ici le 31 décembre 2024.
Aidez la communauté à documenter les révisions matérielles en listant vos calculatrices graphiques !
1234
-
Donations / Premium
For more contests, prizes, reviews, helping us pay the server and domains...
Donate
Discover the the advantages of a donor account !
JoinRejoignez the donors and/or premium!les donateurs et/ou premium !


Partner and ad
Notre partenaire Jarrety Calculatrices à acheter chez Calcuso
-
Stats.
863 utilisateurs:
>814 invités
>43 membres
>6 robots
Record simultané (sur 6 mois):
7582 utilisateurs (le 25/06/2025)
-
Other interesting websites
Texas Instruments Education
Global | France
 (English / Français)
Banque de programmes TI
ticalc.org
 (English)
La communauté TI-82
tout82.free.fr
 (Français)