I made a program that renders all the rules of cellular automata (Rules 0 - 255) and it's extremely slow but it works. Perhaps people with ideas for optimization can post below but here's the code:


Code:
real(0,1,1
real(8,1,0
real(7,9,0,0,160,120,255,0
"CELLULAR AUTOMATA^RENDERER
real(6,0,10,10,0,94,0
"(ENTER)
real(6,0,10,50,0,0,0
Pause
real(0,1,0,1
ClrHome
While R>0-1 and R<256
Input "RULE? (0-255)",R
End
8->dim(|LBRULE
Fill(0,|LBRULE
ClrHome

If R/2!=int(R/2
1->|LBRULE(8)
While R>1
ClrHome
1->B
0->A
While A<=R
2^B->A
B+1->B
Output(2,1,A/2
Output(3,1,B-1
Output(4,1,R
End
1->|LBRULE(8-(B-2
R-(A/2->R
End
real(0,1,1
real(8,1,0
real(7,9,0,0,160,120,255,0
160->dim(|LLINE
160->dim(|LLINE2
Fill(0,|LLINE
Fill(0,|LLINE2
1->|LLINE(80
real(7,9,80,0,1,1,0,0
1->X
1->Y
For(I,1,118
For(J,1,158
X+1->X
(4*|LLINE(X-1))+(2*|LLINE(X))+|LLINE(X+1)->R
If |LBRULE(8-R)=1:Then
1->|LLINE2(X)
real(7,9,X,Y,1,1,0,0
End
End
1->X
Y+1->Y
|LLINE2->|LLINE
Fill(0,|LLINE2
End
Pause
DelVar |LLINE:DelVar |LLINE2:DelVar |LBRULE




Rule 126:


Rule 110 (My favorite Razz)


There are no spritesheets for this program so if you want to test it then you can use that code. Any optimizations are welcomed and Thanks in advance for those who reply! Smile[/img]
Does this code display one pixel per cell? If so, could you use Pxl-On() instead of real(? (Which is faster?)

I don't have a CSE, so about how long does this code take to render an entire screen?

EDIT: Here's my completed code (coordinates are for monochrome calcs). It works now.

Code:
Repeat not(int(R/256
Input "RULE? (0-255)",R
End
.5<=fPart(R2^~cumSum(binomcdf(7,0->L1   //binary digits in reverse order, so digit 0 first
ClrDraw
Pxl-On(0,47
For(Y,1,46
0
For(n,47-Y,47+Y
remainder(2Ans,8)+pxl-Test(Y-1,n+1
If L1(1+Ans
Pxl-On(Y,n
End
End
Botboy3000 wrote:
I made a program that renders all the rules of cellular automata (Rules 0 - 255) and it's extremely slow but it works. Perhaps people with ideas for optimization can post below but here's the code:


Code:
real(0,1,1
real(8,1,0
real(7,9,0,0,160,120,255,0
"CELLULAR AUTOMATA^RENDERER
real(6,0,10,10,0,94,0
"(ENTER)
real(6,0,10,50,0,0,0
Pause
real(0,1,0,1
ClrHome
While R>0-1 and R<256
Input "RULE? (0-255)",R
End
8->dim(|LBRULE
Fill(0,|LBRULE
ClrHome

If R/2!=int(R/2
1->|LBRULE(8)
While R>1
ClrHome
1->B
0->A
While A<=R
2^B->A
B+1->B
Output(2,1,A/2
Output(3,1,B-1
Output(4,1,R
End
1->|LBRULE(8-(B-2
R-(A/2->R
End
real(0,1,1
real(8,1,0
real(7,9,0,0,160,120,255,0
160->dim(|LLINE
160->dim(|LLINE2
Fill(0,|LLINE
Fill(0,|LLINE2
1->|LLINE(80
real(7,9,80,0,1,1,0,0
1->X
1->Y
For(I,1,118
For(J,1,158
X+1->X
(4*|LLINE(X-1))+(2*|LLINE(X))+|LLINE(X+1)->R
If |LBRULE(8-R)=1:Then
1->|LLINE2(X)
real(7,9,X,Y,1,1,0,0
End
End
1->X
Y+1->Y
|LLINE2->|LLINE
Fill(0,|LLINE2
End
Pause
DelVar |LLINE:DelVar |LLINE2:DelVar |LBRULE


There are no spritesheets for this program so if you want to test it then you can use that code. Any optimizations are welcomed and Thanks in advance for those who reply! Smile[/img]

Nothing really massive but just noticed you used the rectangle command in xlibc to set the background to white, but there is already a command for that:

Code:
real(0,3,4,255

Which is shorter so you might wanna use that instead, besides that, you could use the newline char to display the text (You used it once for the word "RENDERER", but you could use it again for the Enter like this

Code:
"CELLULAR AUTOMATA^RENDERER^^^^(ENTER)
real(6,0,10,10,0,94

You usually don't need to put the update LCD if its 0
Maybe a little Delvar where you're storing 0 to A
And you don't need the multiplication symbols, or the parenthesis because of the order of operations (the multiplications will come before the additions without the use of the parenthesis) so these

Code:
(4*|LLINE(X-1))+(2*|LLINE(X))+|LLINE(X+1)->R
R-(A/2->R

could be

Code:
4|LLINE(X-1)+2|LLINE(X)+|LLINE(X+1->R
R-A/2->R
I forgot to submit this post when I wrote it two days ago, but I figure it's still useful. Maybe.


Code:
While R>0-1 and R<256
Input "RULE? (0-255)",R
End

The While loop here will terminate only if R is less than 0 or greater than 255, which I assume isn't what you want. Also, if the user previously set R to a value outside that range, the interpreter will skip right past the loop.
And what's up with writing 0-1 instead of ~1?

Code:

Repeat not(int(R/256 //equivalent to checking if R>=0 and R<=255
Input "RULE (0-255):",R
End

The colon in the Input string is just there because I think it looks weird without a separator of some sort. It doesn't actually contribute to the program :p


Code:
If R/2!=int(R/2
1->|LBRULE(8)

Piecewise expressions are your friend:

Code:
R/2!=int(R/2->|LBRULE(8
but R/2!=int(R/2 is equivalent to 0 or fPart(R/2 [0 or converts it to a boolean] remainder(R,2, so
Code:
remainder(R,2->|LBRULE(8



Code:
1->B
0->A
While A<=R
2^B->A
B+1->B
Output(2,1,A/2
Output(3,1,B-1
Output(4,1,R
End

I don't think the Output( calls are necessary... anyway, isn't this just converting the rule to binary? If so, you can use int(2fPart(R2^seq(X,X,~8,~1 (adapted from something by Weregoose).


Code:
real(7,9,X,Y,1,1,0,0

The InvertPixel command does the same thing -

Code:
real(7,4,X,Y,1,0


Lastly, it looks like you're storing the pixel values to |LLINE and |LLINE2? It would be easier to test the values of the pixels with real(7,1,X,Y.

Here's my attempt at writing one (obsolete now that lirtosiast's posted his, I guess) -

Code:
Repeat not(int(R/256
Input "RULE:",R
End
int(2fPart(R2^seq(X,X,~8,~1->L6
real(0,1,1
real(8,1,not(real(8,0->X //set X to 0
real(0,3,4,~1->dim(L5 //-1 wraps around to 255, and this sets dim(L5 to 0
Repeat K=54
Repeat K=54 or K=48
real(7,4,X,0,1,0
real(2,0->K
real(7,4,X,0,1,0
X+(K=3)-(K=2->X
End
real(7,3,X,0,0,1
X->L5(1+dim(L5
End
{min(L5),max(L5->L5
For(B,0,119
For(A,max(0,(L5(1)-BL6(7))not(L6(8))),max(L5(2)+BL6(4),160L6(8 //All the checks with L6 and L5 are to make sure it doesn't render cells that won't ever be on
real(7,3,A,B+1,~not(L6(1+remainder(sum((0 or {real(7,1,A-1,B),real(7,1,A,B),real(7,1,A+1,B)}){100,10,1}),8))),1,0 //this turns the three pixels' values to a number (so {1,0,1} would become 101) and then, because 111, 110, 101, 100, 11, 10, 1 and 0 are all different mod 8, checks that index of L6. There's probably a better way of doing it, but this works.
End
End
Thanks for all the optimizations everybody. I don't quite understand ALL of them Razz but I'll look into them when I have time. It seems that you have reduced my program to almost half its size which I find both impressive and kind of sad that I am such an inefficient programmer... well, still learning. Very Happy I'll probably be uploading an update to the archives if my program ever got accepted in the first place.
Botboy3000 wrote:
Thanks for all the optimizations everybody. I don't quite understand ALL of them Razz but I'll look into them when I have time.
Please ask about the ones you don't understand. If we provide optimizations that you don't understand, it won't help you make those same optimizations in your future programs. Smile
Botboy3000 wrote:
It seems that you have reduced my program to almost half its size which I find both impressive and kind of sad that I am such an inefficient programmer... well, still learning. Very Happy
Yep! It has taken years for most of us to get to our respective levels of proficiency.
Botboy3000 wrote:
I'll probably be uploading an update to the archives if my program ever got accepted in the first place.
Please be patient; your program was uploaded only 28 hours ago, and we claim programs can take 1 minute to 1 week to be moderated. In the future, you're more than welcome to upload an update for a program before the first version is accepted; we'll simply reject the older version(s). Smile
  
Register to Join the Conversation
Have your own thoughts to add to this or any other topic? Want to ask a question, offer a suggestion, share your own programs and projects, upload a file to the file archives, get help with calculator and computer programming, or simply chat with like-minded coders and tech and calculator enthusiasts via the site-wide AJAX SAX widget? Registration for a free Cemetech account only takes a minute.

» Go to Registration page
Page 1 of 1
» All times are UTC - 5 Hours
 
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

 

Advertisement