Pseudo code help

Want to move your code and projects to LiveCode but don't know where to start?

Moderators: FourthWorld, heatherlaine, Klaus, robinmiller

calmrr3
Posts: 100
Joined: Mon Oct 28, 2013 2:39 pm

Re: Pseudo code help

Post by calmrr3 » Mon Jan 26, 2015 4:55 pm

dave.kilroy wrote:hi calmrr3 - from this side of the computer screen I can't see anything wonky in your code - but obviously something is afoot...

What values do you get when you insert a breakpoint and run it? Alternatively you could progressively comment out more and more of the code until you get the repeat structure to run through all the lines in LatList - and at that stage start uncommenting and debugging until you find the problem...

Good luck

Dave
Still no luck, it seems to do the first part fine ie:

creating a graphic at the first location
subtracting 1 from that graphic's blendLevel for each coordinate that is snapped to that graphic's location.
resulting in a single, semi transparent square

But I can't work out why it won't then do the rest of the squares..

dave.kilroy
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 858
Joined: Wed Jun 24, 2009 1:17 pm
Location: Plymouth, UK
Contact:

Re: Pseudo code help

Post by dave.kilroy » Mon Jan 26, 2015 5:07 pm

Hi calmrr3 - it sounds like you haven't quite finished debugging - line-by-line testing!
"...this is not the code you are looking for..."

calmrr3
Posts: 100
Joined: Mon Oct 28, 2013 2:39 pm

Re: Pseudo code help

Post by calmrr3 » Mon Jan 26, 2015 9:36 pm

I can't really see how I could do line by line testing as each part of the code is informed by another part of the code. I've spent the best part of the day trying to solve this (i'm not a programmer) and I can only assume that to get this to work requires something that I have not yet learned..

dave.kilroy
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 858
Joined: Wed Jun 24, 2009 1:17 pm
Location: Plymouth, UK
Contact:

Re: Pseudo code help

Post by dave.kilroy » Mon Jan 26, 2015 9:56 pm

Hi calmrr3

Debugging is a skill that took me quite a while to really get the hang of (and there loads of ways I could do it better I'm sure) - and if you are new to programming it might be worth your while to put down the problems with the app you are currently working on and spend a bit of time on debugging as a process (which will pay back your investment in time many times over).

Have a look at the beautifully written article by http://revolution.byu.edu/debug/Debugging.php on debugging in LiveCode - some of it you are obviously already doing but other parts (such as running in 'script debug mode') may be of help.

If nothing is revealed after you have really debugged your code to within an inch of its life then please either post your stack here or send it to me directly (sometimes I cannot see my own bugs and it needs new pair of eyes to point out the problem...)

Good luck and enjoy the debugging (when you get it right it's one of the best bits of programming)

Dave
"...this is not the code you are looking for..."

calmrr3
Posts: 100
Joined: Mon Oct 28, 2013 2:39 pm

Re: Pseudo code help

Post by calmrr3 » Mon Jan 26, 2015 10:39 pm

dave.kilroy wrote:Hi calmrr3

Debugging is a skill that took me quite a while to really get the hang of (and there loads of ways I could do it better I'm sure) - and if you are new to programming it might be worth your while to put down the problems with the app you are currently working on and spend a bit of time on debugging as a process (which will pay back your investment in time many times over).

Have a look at the beautifully written article by http://revolution.byu.edu/debug/Debugging.php on debugging in LiveCode - some of it you are obviously already doing but other parts (such as running in 'script debug mode') may be of help.

If nothing is revealed after you have really debugged your code to within an inch of its life then please either post your stack here or send it to me directly (sometimes I cannot see my own bugs and it needs new pair of eyes to point out the problem...)

Good luck and enjoy the debugging (when you get it right it's one of the best bits of programming)

Dave
Thanks i'll have look and see how I get on,

thanks for your help!

SparkOut
Posts: 2834
Joined: Sun Sep 23, 2007 4:58 pm

Re: Pseudo code help

Post by SparkOut » Mon Jan 26, 2015 11:16 pm

I don't want to muddy the waters, if things get worse rather than better then this is no good, but I can't help thinking that another approach may help things fall into place.
If you delete all the (appropriate) plot location graphics and recreate the right graphics at the right blend level when you rerun the card (but only a single graphic for each reused plot location) you should be able to definitely loop through the list and have the right graphics created. This would be a great use case for an array of the locations (as the key) and the count (as the array element value) increased each time the key is used.

Code: Select all

global mmLatList
global maxLat
global minLat

global mmLonList
global maxLon
global minLon

global mmAltList
global maxAlt
global minAlt

global LatList
global LonList
global AltList

on openCard
   --clear all the plot graphics on the card from previous runs 
   repeat with i = 1 to the number of graphics of this card
      if the short name of graphic i begins with "grcLoc" then
         delete graphic i
      end if
   end repeat
   
   --set the template graphic style for the whole handler
   set the style of the templateGraphic to rectangle
   set the width of the templateGraphic to 20
   set the height of the templateGraphic to 20
   set the opaque of the templateGraphic to true
   set the blendlevel of the templateGraphic to 60
   set the backgroundcolor of the templateGraphic to black
   
   lock screen
   repeat with itemCount = 1 to the number of lines of LatList
      
      put line itemCount of LatList into xLoc
      put line itemCount of LonList into yLoc
      put line itemCount of AltList into zLoc
      
      put (((xLoc - minLat) * (50 - 320 )) / (maxLat - minLat)) + 320 into yLocNew
      put (((yLoc - minLon) * (320 - 20)) / (maxLon - minLon)) + 20 into xLocNew
      
      put fnSnapNumbers(yLocNew,20) into yLocNew
      put fnSnapNumbers(xLocNew,20) into xLocNew
      
      put xLocNew & comma & yLocNew into tSquareLoc
      
      --increase the count of the "snapped" location now held in tSquareLoc
      -- so we can determine the blendLevel in future
      add 1 to tLocsArr[(tSquareLoc)]
   end repeat
   
   put the keys of tLocsArr into tKeys
   --tKeys is now a list of the unique location coordinates

   --create new graphics for each of the "snapped" locations, the keys of the array will mean
   --it creates once only for each location, but the value in that key will show us how many 
   --times the location was used, so we can set the blendLevel
   repeat for each line tKey in tKeys
      create graphic
      put it into tNewGrcID
      --give each graphic a name prefix (so that we can delete only the right type of graphics next run)
      set the name of the last graphic to ("grcLoc" & tNewGrcID)
      set the loc of graphic ("grcLoc" & tNewGrcID) to (tKey)
      --from the accumulated value in the array we can tell how many times this "snapped" location was used
      --and reduce the blendLevel from 60 by that amount
      set the blendLevel of graphic ("grcLoc" & tNewGrcID) to 60-tLocsArr[(tKey)]
   end repeat
   unlock screen
end openCard

function fnSnapNumbers pNum,pSnapGap
   put pNum mod pSnapGap into tM
   put pSnapGap / 2 into tCentre
   put max(0,round (tM - tCentre,-1)) into tM
   put trunc((pNum-1) / pSnapGap) * pSnapGap into tN
   return tN + tM + tCentre
end fnSnapNumbers
If this helps, great. If not, delete it!

calmrr3
Posts: 100
Joined: Mon Oct 28, 2013 2:39 pm

Re: Pseudo code help

Post by calmrr3 » Tue Jan 27, 2015 2:02 pm

SparkOut wrote:I don't want to muddy the waters, if things get worse rather than better then this is no good, but I can't help thinking that another approach may help things fall into place.
If you delete all the (appropriate) plot location graphics and recreate the right graphics at the right blend level when you rerun the card (but only a single graphic for each reused plot location) you should be able to definitely loop through the list and have the right graphics created. This would be a great use case for an array of the locations (as the key) and the count (as the array element value) increased each time the key is used.

Code: Select all

global mmLatList
global maxLat
global minLat

global mmLonList
global maxLon
global minLon

global mmAltList
global maxAlt
global minAlt

global LatList
global LonList
global AltList

on openCard
   --clear all the plot graphics on the card from previous runs 
   repeat with i = 1 to the number of graphics of this card
      if the short name of graphic i begins with "grcLoc" then
         delete graphic i
      end if
   end repeat
   
   --set the template graphic style for the whole handler
   set the style of the templateGraphic to rectangle
   set the width of the templateGraphic to 20
   set the height of the templateGraphic to 20
   set the opaque of the templateGraphic to true
   set the blendlevel of the templateGraphic to 60
   set the backgroundcolor of the templateGraphic to black
   
   lock screen
   repeat with itemCount = 1 to the number of lines of LatList
      
      put line itemCount of LatList into xLoc
      put line itemCount of LonList into yLoc
      put line itemCount of AltList into zLoc
      
      put (((xLoc - minLat) * (50 - 320 )) / (maxLat - minLat)) + 320 into yLocNew
      put (((yLoc - minLon) * (320 - 20)) / (maxLon - minLon)) + 20 into xLocNew
      
      put fnSnapNumbers(yLocNew,20) into yLocNew
      put fnSnapNumbers(xLocNew,20) into xLocNew
      
      put xLocNew & comma & yLocNew into tSquareLoc
      
      --increase the count of the "snapped" location now held in tSquareLoc
      -- so we can determine the blendLevel in future
      add 1 to tLocsArr[(tSquareLoc)]
   end repeat
   
   put the keys of tLocsArr into tKeys
   --tKeys is now a list of the unique location coordinates

   --create new graphics for each of the "snapped" locations, the keys of the array will mean
   --it creates once only for each location, but the value in that key will show us how many 
   --times the location was used, so we can set the blendLevel
   repeat for each line tKey in tKeys
      create graphic
      put it into tNewGrcID
      --give each graphic a name prefix (so that we can delete only the right type of graphics next run)
      set the name of the last graphic to ("grcLoc" & tNewGrcID)
      set the loc of graphic ("grcLoc" & tNewGrcID) to (tKey)
      --from the accumulated value in the array we can tell how many times this "snapped" location was used
      --and reduce the blendLevel from 60 by that amount
      set the blendLevel of graphic ("grcLoc" & tNewGrcID) to 60-tLocsArr[(tKey)]
   end repeat
   unlock screen
end openCard

function fnSnapNumbers pNum,pSnapGap
   put pNum mod pSnapGap into tM
   put pSnapGap / 2 into tCentre
   put max(0,round (tM - tCentre,-1)) into tM
   put trunc((pNum-1) / pSnapGap) * pSnapGap into tN
   return tN + tM + tCentre
end fnSnapNumbers
If this helps, great. If not, delete it!

That works perfectly! Thank you!

dave.kilroy
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 858
Joined: Wed Jun 24, 2009 1:17 pm
Location: Plymouth, UK
Contact:

Re: Pseudo code help

Post by dave.kilroy » Tue Jan 27, 2015 2:09 pm

Well done calmrr3 and SparkOut!
"...this is not the code you are looking for..."

Post Reply

Return to “Converting to LiveCode”