Rule_90

[email protected] (Alan Fry) Thu, 29 Aug 2002 11:10:21 +0100
Newsgroups perl.macperl.toolbox
Message-ID <p05100300b99391fc18bb@[158.152.146.73]>
I have been dipping into a fat and fascinating book, "The New 
Science" by Stephen Wolfram who took a PhD in Physics at Caltech in 
1979. His theses concern the complex consequences of simple rules.

This is the first example in the book, a fractal-like pattern in 
which each pixel is set if the pixel in the row above has one 
neighbour set (Rule 90); like this:

	111  110  101  100  011 010  001  000
	 0    1    0    1    1   0    1    0

It lends itself so nicely to a MacPerl window I hope it might be of 
some interest.

Alan Fry


#------------------------------------------------------------------------------

#!perl -w

use Mac::Windows;
use Mac::QuickDraw;
use Mac::Events;
use strict;
use vars qw($v $OK);

#----------------------------------- window -----------------------------------

$v->{frame} = OffsetRect(new Rect(0, 0,1024, 512), 6, 62);
$v->{win} = MacColorWindow->new($v->{frame}, "Rule_90", 1, 4, 1);
SetPort $v->{win}->window;
RGBBackColor(new RGBColor(52224, 65535, 65535));
$v->{col} = new RGBColor(65535, 0, 0);
$v->{win}->sethook('drawgrowicon', sub { 1 });
$v->{win}->sethook('goaway', sub {TrackGoAway($_[0]->{port}, $_[1]); $OK = 1});
$v->{win}->sethook('redraw', \&myDraw );

#------------------------------ Wait Next Event -------------------------------

while ($v->{win}->window and !$OK) { WaitNextEvent };

#---------------------------------- cleanup -----------------------------------

$v->{win}->dispose;

#-------------------------------- subroutines ---------------------------------

sub myDraw {
	my (@new, @old);
	my ($i, $j);

	@old = (0) x 1024;
	$old[512] = 1;
	SetCPixel(512, 0, $v->{col});

	@new = (0) x 1024;

	for $i (1..511) {
		for $j (1..1022) {
			$new[$j] = ($old[$j-1] + $old[$j+1]) % 2;
		}
		@old = @new;
		for (0..1024) {
			if ($old[$_]) { SetCPixel($_, $i, $v->{col}) }
		}
	}
}

#------------------------------------------------------------------------------