Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New to the CF scene
    Join Date
    Jul 2004
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Cool independent indices in js?

    I extracted this from some code I wrote for generating l-system strings. I use these to create floral patterns. There is an idea of having "age" information, so only if a node existed for some time (number of rewriting cycles) it will e.g. produce a flower. I just don t arrive how to implement independently increasing indices in JavaScript..
    I explain my problem using a wildcard (*).. if each cycle I would like a character "I" to be rewritten by two productions:
    I(*) --> I(*+1); //the same I, aged by +1
    I(*) --> I (1); //a new I, with age 1

    ..I don t get how to do this with JavaScript...I always end up with kind of global variables like the x in my code..
    thanks a lot for any help..I would love to continue with my design..I just have the feeling I m stuck in a wrong way of facing the problem..


    var x=0;
    var list = new Array();
    var newList = new Array("I("+x+")");

    var cycle;
    var cycleEnd = 5;
    for (cycle =1; cycle<=cycleEnd; cycle++)
    {
    //show the list before rewriting
    alert("Cycle "+ cycle +" : " + newList.join(" "));

    list= newList;
    var i=0;
    for(z=0;z<list.length; z++)
    {
    switch(list[z])
    {
    case "I("+x+")":
    newList[i]= "I("+x+")"; i++;
    x += 1;
    newList[i]= "I("+x+")"; i++;
    }
    }
    }

  • #2
    Senior Coder
    Join Date
    Feb 2004
    Posts
    1,206
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I don't think I understand what your problem is..

    You want independently increasing indices? I think you mean independently increasing variables that you use to index an array..?

    But maybe I'm wrong and it's irrelevant anyway..

    I'm also confused about this:
    I(*) --> I(*+1); //the same I, aged by +1
    I(*) --> I (1); //a new I, with age 1

    I see the following code:
    Code:
     switch(list[z]) {
      case "I("+x+")":
       newList[i]= "I("+x+")"; i++;
       x += 1;
       newList[i]= "I("+x+")"; i++;
     }
    Are you perhaps trying to create another case in this switch to do something with the I(*+1)? I'm just shooting in the dark here, because I'm not familiar with what you're trying to achieve...

    So if you could explain a bit better what you're trying to figure out how to do, we can help you out.

    Sadiq.

  • #3
    New to the CF scene
    Join Date
    Jul 2004
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    hi sadiq,
    many thanks for your answer; this is what I would like to do:
    Im defining an array of characters (var list), then I go through this array, overwrite each character with a specific production rule
    ( eg I(x)-->I(x+1) and parse the result to a new array (var newList); afterwards you can interpret the resulting string with turtle graphics; this is called Lindenmayer-system and you can eg simulate plants with this.
    You could use whatever and several production rules (e. I --> AKS; A-->IA;, K--->K;..); my specific problem is that I would like to have an "age" information with a character to define since how many overwritings (or cycles, loops) it already exists; then you could define different behaviour, lets say for all "I" that exist since 10 recursions or longer..
    is this somewhat more clear? i thought about defining a kind of "I" object with an age property; then everytime it is called the age value increases by 1..hey, this sounds quite easy..why did nt I arrive there yet? I m gonna try it out again
    best, manuel

  • #4
    Senior Coder
    Join Date
    Feb 2004
    Posts
    1,206
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Yes! As I was reading your post, I thought it would make more sense to encapsulate this using an object, rather than parsing a string each time and looking for parentheses and incrementing the number in that fashion..

    Here are some articles to help you with OO-Javascript:
    http://www.chunkysoup.net/advanced/oojavascript1/
    http://javascript.about.com/library/.../aa051501a.htm
    http://www.sitepoint.com/article/470

    You'll find many more if you do a search on Google.

    Good luck,
    Sadiq.

  • #5
    Regular Coder
    Join Date
    Jul 2002
    Posts
    698
    Thanks
    0
    Thanked 0 Times in 0 Posts
    wouldn't the age(cycle) of all the items in each cycle be exactly 1 since they where only just genrated in the previous cycle?. And if you intend to draw something I would have to tell you that JavaScript is not exactly the best language to draw with.

  • #6
    New to the CF scene
    Join Date
    Jul 2004
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I haven t had time to go further yet, so don t know yet about the age..concerning drawing: i m working with Illustrator and you can directly script using javascript..


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •