Pseudo code help

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

Moderators: heatherlaine, Klaus, FourthWorld, robinmiller

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

Pseudo code help

Post by calmrr3 » Fri Jan 23, 2015 2:48 pm

Hi there,

I wonder if someone could help me convert the following pseduo code into livecode? :

Code: Select all

  put xLocNew & "," & yLocNew into tSquareLoc // create x and y coordinates variable
      if tSquareLoc = the location of grc 1 to the number of graphics on this card //check to see if there is already a graphic at that location
      minus 1 from the blend level of that graphic //if there is already a graphic there, minus 1 from it's blend level
      
      else // if there isnt a graphic there
      
      create grc itemCount // create a graphic there
      set the location of grc itemCount to tSquareLoc 

Thank you!

Klaus
Posts: 10289
Joined: Sat Apr 08, 2006 8:41 am
Location: Germany
Contact:

Re: Pseudo code help

Post by Klaus » Fri Jan 23, 2015 3:47 pm

Hi calmrr3,

OK, although this is not really difficult, I will give you a helping and 8)
Note my comments!

Code: Select all

...
 put xLocNew & "," & yLocNew into tSquareLoc 
   put the num of grcs into tNumGrcs
   if tNumGrcs = 0 then
      
      ## itemCount is not yet declared, please take care of this!
      create grc itemCount
      set the loc of grc itemCount to tSquareLoc
      exit to top
   end if
   lock screen
   
   repeat with i = 1 to tNumGrcs
      if tSquareLoc = the loc of grc i then

         ## Need to check if BLENDLEVEL is already 0!
         put the blendlevel of grc i into tBlL
         subtract 1 from tBlL
         set the blendlevel of grc i to tBlL
      end if
   end repeat
   unlock screen
...
Best

Klaus

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

Re: Pseudo code help

Post by calmrr3 » Fri Jan 23, 2015 4:33 pm

Hi,

okay so I forgot to mention that I already have a loop set up which counts through all the gps points to be plotted (ive changed it to x = 1000 here for clarity) . So before what it was doing was plotting each and every point as a graphic, but that was very slow. Heres an idea of what the loop is like now:

Code: Select all

on openCard

   put 1000 into x

   repeat with itemCount = 1 to x 

       put xLocNew & "," & yLocNew into tSquareLoc 
      put the num of grcs into tNumGrcs
      if tNumGrcs = 0 then
               
         ## itemCount is not yet declared, please take care of this!
         create grc itemCount
         set the loc of grc itemCount to tSquareLoc
         set the style of grc itemCount to "rectangle"
         set the width of grc itemCount to 20
         set the height of grc itemCount to 20
         set the opaque of grc itemCount  to true
         
         exit to top
      end if
      lock screen
         
      repeat with i = 1 to tNumGrcs
         if tSquareLoc = the loc of grc i then
            
            ## Need to check if BLENDLEVEL is already 0!
            put the blendlevel of grc i into tBlL
            subtract 1 from tBlL
            set the blendlevel of grc i to tBlL
         end if
      end repeat
      unlock screen


   end repeat
end openCard

At the moment it only draws the first square. I assume this is because of the 'exit to top' part (It starts the itemCount repeat again and thus itemCount is always going to be 1 ). I dont know if there is a way to solve this.

Thanks again

Klaus
Posts: 10289
Joined: Sat Apr 08, 2006 8:41 am
Location: Germany
Contact:

Re: Pseudo code help

Post by Klaus » Fri Jan 23, 2015 6:16 pm

So you want to create the graphics INSIDE of the repeat loop?
Could you explain a bit more?

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

Re: Pseudo code help

Post by calmrr3 » Fri Jan 23, 2015 6:21 pm

Hi,

Here is the open card script, the reason for the repeat is to get the location of each point.

Code: Select all

on openCard
   
   repeat with itemCount = 1 to the number of lines in field "manLatEntry" of card "Manual"
      
      put line itemCount of field "manLatEntry" of card "Manual" into xLoc
      put line itemCount of field "manLonEntry" of card "Manual" into yLoc
      put line itemCount of field "manEleEntry" of card "Manual" into zLoc
      
      put (((xLoc - tMin) * (255 - 1 )) / (tMax - tMin)) + 1 into yColour
      put (((yLoc - tMin2) * (255 - 1)) / (tMax2 - tMin2)) + 1 into xColour
      put (((zLoc - tMinZ) * (255 - 1)) / (tMaxZ - tMinZ)) + 1 into zColour
      put (((zLoc - tMinZ) * (0 - 5)) / (tMaxZ - tMinZ)) + 5 into  strokeSizeAlt
      
      put (yColour+zColour)/2 into yColour
      put (xColour+zColour)/2 into xColour
      put round(yColour) into yColour
      put round(xColour) into xColour
      put round(zColour) into zColour
      put zColour && "," && yColour && "," && xColour into xyzColour
      
      put (((xLoc - tMin) * (50 - 320 )) / (tMax - tMin)) + 320 into yLocNew
      put (((yLoc - tMin2) * (320 - 20)) / (tMax2 - tMin2)) + 20 into xLocNew
      
      //NewValue = (((OldValue - OldMin) * (NewMax - NewMin)) / (OldMax - OldMin)) + NewMin
      
      if yLocNew <= 20 and yLocNew > 0 then
         put 10 into yLocNew
      else if yLocNew <= 40 and yLocNew > 20 then
         put 30 into yLocNew
      else if yLocNew <= 60 and yLocNew > 40 then
         put 50 into yLocNew
      else if yLocNew <= 80 and yLocNew > 60 then
         put 70 into yLocNew
      else if yLocNew <= 100 and yLocNew > 80 then
         put 90 into yLocNew
      else if yLocNew <= 120 and yLocNew > 100 then
         put 110 into yLocNew
      else if yLocNew <= 140 and yLocNew > 120 then
         put 130 into yLocNew
      else if yLocNew <= 160 and yLocNew > 140 then
         put 150 into yLocNew
      else if yLocNew <= 180 and yLocNew > 160 then
         put 170 into yLocNew
      else if yLocNew <= 200 and yLocNew > 180 then
         put 190 into yLocNew
      else if yLocNew <= 220 and yLocNew > 200 then
         put 210 into yLocNew
      else if yLocNew <= 240 and yLocNew > 220 then
         put 230 into yLocNew
      else if yLocNew <= 260 and yLocNew > 240 then
         put 250 into yLocNew
      else if yLocNew <= 280 and yLocNew > 260 then
         put 270 into yLocNew
      else if yLocNew <= 300 and yLocNew > 280 then
         put 290 into yLocNew
      else if yLocNew <= 320 and yLocNew > 300 then
         put 310 into yLocNew
      end if
      
      
      if xLocNew <= 20 and xLocNew > 0 then
         put 10 into xLocNew
      else if xLocNew <= 40 and xLocNew > 20 then
         put 30 into xLocNew
      else if xLocNew <= 60 and xLocNew > 40 then
         put 50 into xLocNew
      else if xLocNew <= 80 and xLocNew > 60 then
         put 70 into xLocNew
      else if xLocNew <= 100 and xLocNew > 80 then
         put 90 into xLocNew
      else if xLocNew <= 120 and xLocNew > 100 then
         put 110 into xLocNew
      else if xLocNew <= 140 and xLocNew > 120 then
         put 130 into xLocNew
      else if xLocNew <= 160 and xLocNew > 140 then
         put 150 into xLocNew
      else if xLocNew <= 180 and xLocNew > 160 then
         put 170 into xLocNew
      else if xLocNew <= 200 and xLocNew > 180 then
         put 190 into xLocNew
      else if xLocNew <= 220 and xLocNew > 200 then
         put 210 into xLocNew
      else if xLocNew <= 240 and xLocNew > 220 then
         put 230 into xLocNew
      else if xLocNew <= 260 and xLocNew > 240 then
         put 250 into xLocNew
      else if xLocNew <= 280 and xLocNew > 260 then
         put 270 into xLocNew
      else if xLocNew <= 300 and xLocNew > 280 then
         put 290 into xLocNew
      else if xLocNew <= 320 and xLocNew > 300 then
         put 310 into xLocNew
      end if
      
      put xLocNew & "," & yLocNew into tSquareLoc 
      put the num of grcs into tNumGrcs
      if tNumGrcs = 0 then
               
         create grc itemCount
         set the loc of grc itemCount to tSquareLoc
         set the width of grc itemCount to 20
         set the height of grc itemCount to 20
         set the style of grc itemCount to "rectangle"
         exit to top
      end if
      
      lock screen
         
      repeat with i = 1 to tNumGrcs
         if tSquareLoc = the loc of grc i then
            put the blendlevel of grc i into tBlL
            subtract 1 from tBlL
            set the blendlevel of grc i to tBlL
         end if
      end repeat
      unlock screen
       
   end repeat
end openCard

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

Re: Pseudo code help

Post by calmrr3 » Sun Jan 25, 2015 4:08 pm

Anyone?

I have also tried to change this so that instead of taking the numbers from the fields I have tried to but the contents of the fields into variables:

Code: Select all

on findMinMaxLatManual
   put fld "manualLatFld" into mmLatList
   replace cr with comma in mmLatList
   put max(mmLatList) into maxLat
   put min(mmLatList) into minLat
 end findMinMaxLatManual

Code: Select all

repeat with itemCount = 1 to the number of lines of mmLatList

Livecode doesn't seem to like this though so I think it could be incorrect.
Thanks

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

Re: Pseudo code help

Post by calmrr3 » Sun Jan 25, 2015 5:07 pm

This is the card script at the moment, there is an error in it somewhere but I cant think where:

Code: Select all

on openCard
   lock screen
   repeat with itemCount = 1 to the number of lines of mmLatList
      put line itemCount of mmLatList into xLoc
      put line itemCount of mmLonList into yLoc
      put line itemCount of mmAltList into zLoc
      
      put (((xLoc - minLat) * (50 - 320 )) / (maxLat - minLat)) + 320 into yLocNew
      put (((yLoc - minLon) * (320 - 20)) / (maxLon - minLon)) + 20 into xLocNew
      
      if yLocNew <= 20 and yLocNew > 0 then
         put 10 into yLocNew
      else if yLocNew <= 40 and yLocNew > 20 then
         put 30 into yLocNew
      else if yLocNew <= 60 and yLocNew > 40 then
         put 50 into yLocNew
      else if yLocNew <= 80 and yLocNew > 60 then
         put 70 into yLocNew
      else if yLocNew <= 100 and yLocNew > 80 then
         put 90 into yLocNew
      else if yLocNew <= 120 and yLocNew > 100 then
         put 110 into yLocNew
      else if yLocNew <= 140 and yLocNew > 120 then
         put 130 into yLocNew
      else if yLocNew <= 160 and yLocNew > 140 then
         put 150 into yLocNew
      else if yLocNew <= 180 and yLocNew > 160 then
         put 170 into yLocNew
      else if yLocNew <= 200 and yLocNew > 180 then
         put 190 into yLocNew
      else if yLocNew <= 220 and yLocNew > 200 then
         put 210 into yLocNew
      else if yLocNew <= 240 and yLocNew > 220 then
         put 230 into yLocNew
      else if yLocNew <= 260 and yLocNew > 240 then
         put 250 into yLocNew
      else if yLocNew <= 280 and yLocNew > 260 then
         put 270 into yLocNew
      else if yLocNew <= 300 and yLocNew > 280 then
         put 290 into yLocNew
      else if yLocNew <= 320 and yLocNew > 300 then
         put 310 into yLocNew
      end if
      
      if xLocNew <= 20 and xLocNew > 0 then
         put 10 into xLocNew
      else if xLocNew <= 40 and xLocNew > 20 then
         put 30 into xLocNew
      else if xLocNew <= 60 and xLocNew > 40 then
         put 50 into xLocNew
      else if xLocNew <= 80 and xLocNew > 60 then
         put 70 into xLocNew
      else if xLocNew <= 100 and xLocNew > 80 then
         put 90 into xLocNew
      else if xLocNew <= 120 and xLocNew > 100 then
         put 110 into xLocNew
      else if xLocNew <= 140 and xLocNew > 120 then
         put 130 into xLocNew
      else if xLocNew <= 160 and xLocNew > 140 then
         put 150 into xLocNew
      else if xLocNew <= 180 and xLocNew > 160 then
         put 170 into xLocNew
      else if xLocNew <= 200 and xLocNew > 180 then
         put 190 into xLocNew
      else if xLocNew <= 220 and xLocNew > 200 then
         put 210 into xLocNew
      else if xLocNew <= 240 and xLocNew > 220 then
         put 230 into xLocNew
      else if xLocNew <= 260 and xLocNew > 240 then
         put 250 into xLocNew
      else if xLocNew <= 280 and xLocNew > 260 then
         put 270 into xLocNew
      else if xLocNew <= 300 and xLocNew > 280 then
         put 290 into xLocNew
      else if xLocNew <= 320 and xLocNew > 300 then
         put 310 into xLocNew
      end if
      
      put xLocNew & "," & yLocNew into tSquareLoc 
      put the num of grcs into tNumGrcs
      if tNumGrcs = 0 then
         create grc itemCount
         set the style of grc itemCount to "rectangle"
         set the width of grc itemCount to 20
         set the height of grc itemCount to 20
         set the loc of grc itemCount to tSquareLoc
         set the opaque of grc itemCount to true
         
      else
         repeat with i = 1 to tNumGrcs
            if tSquareLoc = the loc of grc i then
               put the blendlevel of grc i into tBlL
               subtract 1 from tBlL
               set the blendlevel of grc i to tBlL
            end if
         end repeat 
      end if
   end repeat 
   
   unlock screen
end openCard

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

Re: Pseudo code help

Post by SparkOut » Sun Jan 25, 2015 7:53 pm

Hi calmrr3,
the trouble is, it's really difficult to know what you're trying to do. One thing that will help stop the "eye-clutter" is to ditch the long series of if..then.. else to set the range of xNewLoc and yNewLoc to be "snapped" to a rounded value. If you put a function in the card or stack script:

Code: Select all

function fnSnapNumbers pNum,pSnapGap,pSnapOffset
   put pNum mod pSnapGap into tM
   put max(0,round(tM-pSnapOffset - 9,-1)) into tM
   put trunc((pNum-1) / pSnapGap) * pSnapGap into tN
   return tN + tM + pSnapOffset
end fnSnapNumbers
you can replace that whole section of the if..thens... with

Code: Select all

      //snap yLocNew to centres of gaps of 20, with offset of 10
      put fnSnapNumbers(yLocNew,20,10) into yLocNew

      //snap xLocNew to centres of gaps of 20, with offset of 10
      put fnSnapNumbers(xLocNew,20,10) into xLocNew
Having got rid of that distraction to the eye, it looks like there is no declaration for variables tMin, tMin2, tMinZ. Apart from that, not sure where to go. What errors are you coming up against?

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

Re: Pseudo code help

Post by calmrr3 » Sun Jan 25, 2015 9:04 pm

Hi, thanks for your reply,

The variables are created on the previous card and they are the maximum and minimum values of 3 different lists of numbers (latitude, longitude and altitude).

I am trying to plot the gps route but in a way that looks like this:

Image

So rather than plotting each point, it snaps the gps coordinate to the nearest square. I am trying to figure out how to check if there is already a graphic at that location and if there is, minus 1 from that graphic blend level without drawing the new graphic. So the darker the square, the more gps points have been rounded to that location. I managed to get this working by drawing each point as a square with a blend level of 99 and just drawing each square but this method was very slow is it was drawing every single gpx coordinate as a square and placing them on top of each other.

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 » Sun Jan 25, 2015 10:31 pm

Hi calmrr3 - I haven't done exactly what you need (it would involve a lot of setting up of fields and values etc) but I have had a go a what I think might be the bit that was proving troublesome for you.

If you create a button in a fresh card and copy the following code into its script you should be able to see it create graphics as long as they don't share the same loc as any existing graphics. I've set the blendlevel of new graphics to 60 so that you can see them being created, easy enough to change that to 99 later on. I've also left in a commented-out line of code "--put the loc of the last grc into tSquareLoc" which you can uncomment if you want to see the darkening effect in action (I've changed the amount to darken the graphic to -10 so that you can more easily see the effect, but again easy enough for you to change this in your code):

Code: Select all

on mouseUp
   --setup any newly created graphics to have desired attributes
   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 99
   set the backgroundcolor of the templateGraphic to black
   --
   --randomly create a location to test the code
   --replace these lines calling the randomInRange() function
   --with valid xLocNew and yLocNew values in your code
   put randomInRange(200,the width of the current cd) into xLocNew
   put randomInRange(200,the height of the current cd) into yLocNew
   put xLocNew & comma & yLocNew into tSquareLoc
   --put the loc of the last grc into tSquareLoc
   --
   --check if any graphics on the card share the same location
   --and if any do then decrease their blend level by 1
   --also, if any graphics with the same location are found
   --then put a value into a 'flag' variable called "tSameLoc"
   repeat with i = 1 to the number of grcs
      if tSquareLoc = the loc of grc i then
         set the blendlevel of grc i to (the blendlevel of grc i - 1)
         put true into tSameLoc
      end if
   end repeat
   --
   --if tSameLoc does not exist (i.e. if it does not have a value)
   --then create a new graphic (with the attributed set earlier) at that location
   if not tSameLoc then
      create grc
      set the loc of the last grc to tSquareLoc
   end if
end mouseUp

function randomInRange lowerLimit,upperLimit
   return random(upperLimit - lowerLimit + 1) + lowerLimit - 1
end randomInRange
So, between this and sparkout's nice fnSnapNumbers() function do you feel close enough to a solution to try again?

Kind regards

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

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

Re: Pseudo code help

Post by SparkOut » Mon Jan 26, 2015 10:50 am

a much better snap function:

Code: Select all

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
So you can leave out the pSnapOffset parameter in the function call:

Code: Select all

      //snap yLocNew to centres of gaps of 20
      put fnSnapNumbers(yLocNew,20) into yLocNew

      //snap xLocNew to centres of gaps of 20
      put fnSnapNumbers(xLocNew,20) into xLocNew

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

Re: Pseudo code help

Post by calmrr3 » Mon Jan 26, 2015 11:43 am

Hi, thanks for your replies,

The code worked well, however the issue I am having is with getting it to automatically repeat so that the user doesn't have to click a button to draw each square. The repeat also means that the I can use a different xLoc and yLoc each time thus plotting the gps coordinates. I've tried to combine the above with a repeat loop where itemCount is the number of gps coordinates recorded and LatList,LonList, and AltList are lists of numbers in order (for example: LatList is just the latitude value at each point in a list).

For some reason, this code only draws one square and I can't think why:

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
   lock screen
   repeat with itemCount = 1 to the number of lines of LatList
      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
      
      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
      
      repeat with i = 1 to the number of grcs
         if tSquareLoc = the loc of grc i then
            set the blendlevel of grc i to (the blendlevel of grc i - 1)
            put true into tSameLoc
         end if
      end repeat
      
      if not tSameLoc then
         create grc
         set the loc of the last grc to tSquareLoc
      end if
   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

Thanks again

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 12:55 pm

hi calmrr3 - have you tested both the contents of LatList and number of lines it has? something like a 'put' statement in the first line of your openCard handler

BTW, I would place all the code that amends the templateGraphic before the 'repeat' as you only need to set this once...

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 2:01 pm

dave.kilroy wrote:hi calmrr3 - have you tested both the contents of LatList and number of lines it has? something like a 'put' statement in the first line of your openCard handler

BTW, I would place all the code that amends the templateGraphic before the 'repeat' as you only need to set this once...

Dave
Hi Dave,

I included this:

Code: Select all

 put the number of lines of LatList & comma & the number of lines of LonList & comma & the number of lines of AltList into fld "test"
   put LatList into fld "test2"
to the openCard script, and the result was:

Image

So no issues with the contents or the number of lines.

Thanks

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 2:37 pm

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
"...this is not the code you are looking for..."

Post Reply

Return to “Converting to LiveCode”