Pathologically Eclectic Rubbish Lister PerlMonks

### Re: 1-D Cellular Automata

by gumby (Scribe)
 on Jun 19, 2002 at 19:33 UTC ( #175820=note: print w/replies, xml ) Need Help??

in reply to 1-D Cellular Automata

I was waiting for another one of these posts ;-) Rule 90 will generate Sierpinski's sieve (but then, you can in turn generate that from Pascal's triangle (mod 2)).

```  #!/usr/bin/perl -w

use strict;

my (\$file, %rules, \$string, \$rewrite, \$angle, \$i);

\$file = "gosper";

# Peano-Gosper curve.
\$rules{"X"} = "X+YF++YF-FX--FXFX-YF+";
\$rules{"Y"} = "-FX+YFYF++YF+FX--FX-Y";
\$string = "FX"; \$angle = 60; \$i = 4;

foreach (1..\$i) {
\$rewrite = "";
for (split //,\$string) {
\$rewrite .= \$rules{\$_} || \$_;
}
\$string = \$rewrite;
}

open PS,">\$file.ps";
print PS "%#\n\n";
print PS "306 396 moveto\n";
for my \$ch (split //,\$string) {
if (\$ch eq 'f') {
print PS "0 1 rlineto\n";
} elsif (\$ch eq 'F') {
print PS "0 1 rmoveto\n";
} elsif (\$ch eq '+') {
print PS "360 \$angle rotate\n";
} elsif (\$ch eq '-') {
print PS "360 \$angle neg rotate\n";
} elsif (\$ch eq '!') {
print PS "-1 1 scale\n";
} elsif (\$ch eq '[') {
print PS "gsave\n";
} elsif (\$ch eq ']') {
print PS "stroke\ngrestore\n";
}
}
print PS "stroke\nshowpage";
close PS;

Note that you need to replace uppercase F with lowercase f to turn on the 'pen'.

Replies are listed 'Best First'.
Re: 1-D Cellular Automata
by ferrency (Deacon) on Jun 20, 2002 at 18:13 UTC
I like L-systems. A few years ago I wrote an L-system which displayed a Koch snowflake in postscript, for one of the early TPJ obfuscated perl contests, but I don't think it qualified due to the postscript. Maybe I'll post it in the obfuscation section if I can dig it up.

Unfortunately, since then, I haven't found any good turtle-graphics style module to support interesting L-system output. There was (is?) a Graphics::Turtle module (or something similar) in CPAN, but according to the author it was an idea, not an implementation.

However, using postscript for turtle graphics output is pretty easy, if you don't mind using a postscript viewer to look at the output. Instead of rotating your "turtle" and calculating a new (X, Y) based on your angle and a fixed radius, translate your current position to (0, 0), rotate the world around the origin, and then draw a line to (say) (0, 1). Using postscript as my output device, the snowflake generator was under 4 80-character lines long, including unnecessary obfuscation and without any external module dependancies.

I hope this provides some inspiration :) If I don't find my old postscript L-system generator maybe I'll rewrite it and post it here somewhere.

Alan

Thanks, great post. I've changed quite a bit of (cut out a lot of) code (and made some refinements) so I've posted a working version.

Create A New User
Node Status?
node history
Node Type: note [id://175820]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (1)
As of 2020-10-25 05:44 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
My favourite web site is:

Results (249 votes). Check out past polls.

Notices?