...

View Full Version : independent indices in js?



manko
07-25-2004, 05:50 PM
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++;
}
}
}

sad69
07-26-2004, 05:48 PM
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:


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.

manko
07-26-2004, 08:17 PM
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

sad69
07-26-2004, 08:24 PM
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/weekly/aa051501a.htm
http://www.sitepoint.com/article/470

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

Good luck,
Sadiq.

Garadon
07-26-2004, 10:15 PM
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.

manko
07-30-2004, 03:59 PM
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..



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum