D
Dan
Hey hey,
I'm trying to code a program for generating cyclic cellular automaton
(http://en.wikipedia.org/wiki/Cyclic_cellular_automaton) and have
gotten it working well enough to generate pretty pictures but have run
into a problem with it wrapping around the array properly when wanting
to check cell values beyond the edge of the screen.
In the picture link it shows that the wrapping works correctly when
cells are checking the value over the bounds from east to west and
west to east (you can tell from the big diamond in the top left across
the left bound to the right bound and from the bottom right diamond
pulsing from the right bound to the left bound) and from north to
south (from the top left diamond pulsing to the top to the bottom).
The wrapping does not work correctly when trying to go from the bottom
bound to the top bound. This can be seen in the big diamond in the
bottom right pulsing into the bottom and having nothing appear on the
top.
http://img48.imageshack.us/my.php?image=screencaptureoe9.png
The diamonds grow over time as each 'step' is run.
I have the all the cells stored in a one dimensional array which I
treat as a two dimensional array but I think my logic is screwed up
when calculating the correct cell when it happens to be checking
across the bounds.
In the example below modelArray is considered a pointer to an array of
integers. It is the same size as previousModelArray.
previousModelArray is the array that is used to calculate the next
step. Once the calculation is done it is discarded.
Also the function name looks funny because it's implemented in
objective-c. Most of the code is C though so I thought I would post it
in this group.
-(void)step {
int *previousModelArray;
previousModelArray = malloc(sizeof(int) * arrayWidth *
arrayHeight);
for(int i = 0; i < arrayWidth * arrayHeight; i++) {
previousModelArray = modelArray;
}
for(int x = 0; x < arrayWidth; x++) {
for (int y = 0; y < arrayHeight; y++) {
int i = x + (y * arrayWidth);
int iRight = ((x + 1) % arrayWidth) + (y *
arrayWidth);
int iDown = x + (((y + 1) % arrayHeight) *
arrayWidth);
int iLeft = ((x - 1) % arrayWidth) + (y *
arrayWidth);
int iUp = x + (((y - 1) % arrayHeight) *
arrayWidth);
if(((modelArray + 1) % n) ==
previousModelArray[iRight]) {
modelArray = previousModelArray
[iRight];
}
else if (((modelArray + 1) % n) ==
previousModelArray[iDown]) {
modelArray = previousModelArray
[iDown];
}
else if (((modelArray + 1) % n) ==
previousModelArray[iLeft]) {
modelArray = previousModelArray
[iLeft];
}
else if (((modelArray + 1) % n) ==
previousModelArray[iUp]) {
modelArray = previousModelArray
[iUp];
}
}
}
free(previousModelArray);
}
Any help would be much appreciated.
Thanks,
Dan
I'm trying to code a program for generating cyclic cellular automaton
(http://en.wikipedia.org/wiki/Cyclic_cellular_automaton) and have
gotten it working well enough to generate pretty pictures but have run
into a problem with it wrapping around the array properly when wanting
to check cell values beyond the edge of the screen.
In the picture link it shows that the wrapping works correctly when
cells are checking the value over the bounds from east to west and
west to east (you can tell from the big diamond in the top left across
the left bound to the right bound and from the bottom right diamond
pulsing from the right bound to the left bound) and from north to
south (from the top left diamond pulsing to the top to the bottom).
The wrapping does not work correctly when trying to go from the bottom
bound to the top bound. This can be seen in the big diamond in the
bottom right pulsing into the bottom and having nothing appear on the
top.
http://img48.imageshack.us/my.php?image=screencaptureoe9.png
The diamonds grow over time as each 'step' is run.
I have the all the cells stored in a one dimensional array which I
treat as a two dimensional array but I think my logic is screwed up
when calculating the correct cell when it happens to be checking
across the bounds.
In the example below modelArray is considered a pointer to an array of
integers. It is the same size as previousModelArray.
previousModelArray is the array that is used to calculate the next
step. Once the calculation is done it is discarded.
Also the function name looks funny because it's implemented in
objective-c. Most of the code is C though so I thought I would post it
in this group.
-(void)step {
int *previousModelArray;
previousModelArray = malloc(sizeof(int) * arrayWidth *
arrayHeight);
for(int i = 0; i < arrayWidth * arrayHeight; i++) {
previousModelArray = modelArray;
}
for(int x = 0; x < arrayWidth; x++) {
for (int y = 0; y < arrayHeight; y++) {
int i = x + (y * arrayWidth);
int iRight = ((x + 1) % arrayWidth) + (y *
arrayWidth);
int iDown = x + (((y + 1) % arrayHeight) *
arrayWidth);
int iLeft = ((x - 1) % arrayWidth) + (y *
arrayWidth);
int iUp = x + (((y - 1) % arrayHeight) *
arrayWidth);
if(((modelArray + 1) % n) ==
previousModelArray[iRight]) {
modelArray = previousModelArray
[iRight];
}
else if (((modelArray + 1) % n) ==
previousModelArray[iDown]) {
modelArray = previousModelArray
[iDown];
}
else if (((modelArray + 1) % n) ==
previousModelArray[iLeft]) {
modelArray = previousModelArray
[iLeft];
}
else if (((modelArray + 1) % n) ==
previousModelArray[iUp]) {
modelArray = previousModelArray
[iUp];
}
}
}
free(previousModelArray);
}
Any help would be much appreciated.
Thanks,
Dan