Difference between revisions of "Zigzag Sequence"

From Second Life Wiki
Jump to navigation Jump to search
Line 8: Line 8:


<lsl>
<lsl>
Loop(integer max) {
ZigZag(integer max)  
{
     list sequence;
     list sequence;
     integer x;
     integer x;
     for(x = 0;x <= max; ++x) {
     for(; x <= max; ++x)  
    {
         integer y = llCeil(llSin((x+1) * PI));
         integer y = llCeil(llSin((x+1) * PI));
         sequence += llList2CSV([x,y]);
         sequence += llList2CSV([x, y]);
         sequence += "\n";
         sequence += "\n";
     }
     }
     llSetText((string) sequence, <1,1,1>, 1);
     llSetText((string) sequence, <1, 1, 1>, 1);
}
}
default
default
{
{
     state_entry() {
     state_entry() {
         Loop(8);
         ZigZag(8);
     }
     }
}
}
</lsl>
</lsl>

Revision as of 12:12, 21 July 2013

Origin

So, while I was scripting my Checkers board I had to incorporate an algorithm for arranging the pieces in a zig-zag pattern. As many of you already (but for those that don't, here you are), a checkers board is much like Quadrant-1 in a coordinate plane system. Which means in order to create this zig-zag effect, one has to construct as sequence like so: (0, 0), (1, 1), (2, 0), (3, 1), etc. And instead of using static, or hardcoded, values, I created a script that creates this sequence for me. Brace yourself, there is Trigonometry involved.

The Script

This is just a simple for loop with text so you get the idea.

<lsl> ZigZag(integer max) {

   list sequence;
   integer x;
   for(; x <= max; ++x) 
   {
       integer y = llCeil(llSin((x+1) * PI));
       sequence += llList2CSV([x, y]);
       sequence += "\n";
   }
   llSetText((string) sequence, <1, 1, 1>, 1);

} default {

   state_entry() {
       ZigZag(8);
   }

} </lsl>