Arranging squares

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

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

Arranging squares

Post by calmrr3 » Wed Nov 20, 2013 10:51 pm

Hello,

I have 100 squares and I would like them to be arranged in rows of 10 to create a
large square from the 100 small squares. I have a button that randomized the order of them however at the moment I can only achieve a single row of 100 images rather than the ten rows of ten.

Also - as an extra bit, If each square is named "square1" up to "square100" is there a way to have button that will arrange the squares in order counting up to 100?

Here is the script I am using at the moment:

Code: Select all

on mouseUp
   lock screen --for visual effect
      repeat with y = 1 to 100
            put y into line y of temp
      end repeat
      sort temp by random(10000)
   put the width of image "nebula3_1" into tW
   put the width of image "nebula3_1" into tH
        repeat with y = 1 to 100
      put (line y of temp) into s
      --put 0,0 into tLoc
      put  (s-1)*tW into item 1 of tLoc
      put (the height of this card/3) into item 2 of tLoc
      set the loc of image ("nebula3_" & y) to tLoc
      end repeat
   unlock screen --with visual effect dissolve very fast
 end mouseUp

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10333
Joined: Wed May 06, 2009 2:28 pm

Re: Arranging squares

Post by dunbarx » Thu Nov 21, 2013 1:08 am

Oh boy.

If you need the squares (are they graphics?) to be arranged both 1-100 and 10x10, then maybe your naming convention is OK.

How to arrange them 1-100? Can you envision a repeat loop of the "repeat with" form, that might (pseudocode):

put grc "square" & y into location y -- where "y" is an index counter that also manages the loc of successive squares. "square & y" would resolve to "square1, square2, etc.

You can do that. Now what about this, which is a two level "if" construct (pseudocode, but closer to the real thing):

repeat with y = 1 to 10
repeat with z = 1 to 10
put grc "square" & y into location z & "," & y --
end repeat
end repeat

Try this for practice. In a button somewhere

Code: Select all

on mouseUp
   repeat with y = 1 to 4
      repeat with z = 1 to 4
         put y & "," & z & return after temp
      end repeat
   end repeat
   answer temp
end mouseUp
Can you see what is happening? Step through the handler. Can you see how to use this sort of thing to make your location array?

Craig

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

Re: Arranging squares

Post by calmrr3 » Thu Nov 21, 2013 1:37 am

Thanks, that example made sense, though I am still having difficulty getting the squares to form the larger square, at the moment they are just going across the screen in horizontal line side by side. (they are small 25x25 image files, when placed in sequence they reveal the image).

Thanks again

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10333
Joined: Wed May 06, 2009 2:28 pm

Re: Arranging squares

Post by dunbarx » Thu Nov 21, 2013 4:11 am

How do you set them going across? Since you know their size, do you set up a series of locs or do you set, say, the topleft of square2 to the topright of square1? I hope the locs...

If you ran that sample handler, you see that the output (set up as a 4x4) was of the form:

1,1
1,2
1,3
1,4
2,1
2,2
...

Knowing the rect of the squares, can you set up an array of locs that are the centers of those squares? The sample handler idea would take each of the 100 squares and lay them out in the pattern above, with the rows and columns scaled to the size of each square. Do you see? The fifth line above would set the fifth square in row 2, column 1. This would be scaled to a loc on the card based on the dimensions of the square (25 pixels x 25), or 38 pixels down and 13 across.

Experiment, please, and write back...

Craig

Post Reply