barryvanveen/php-cca

Create two-dimensional Cyclic Cellular Automaton with PHP. Export results as (animated) images. Includes presets of working rules.

2.0.1 2018-07-17 20:05 UTC

This package is auto-updated.

Last update: 2024-03-06 11:47:33 UTC


README

Latest Version on Packagist Software License Build Status Coverage Status Quality Score Total Downloads

This project can be used to run two-dimensional Cyclic Cellular Automaton (CCA). Results can be saved as static images of animated gifs. The configuration (of the CCA or images) can be set using various presets or customized to your own liking.

Install

Via Composer

$ composer require barryvanveen/php-cca

Please note that you need PHP 7.0 or higher and the GD extension to install this package.

Usage

Creating a configuration

// using the ConfigBuilder
$builder = Builders\ConfigBuilder::createFromPreset(Config\Presets::PRESET_CCA);
$builder->rows(50);
$builder->columns(50);
$config = $builder->get();
 
// or build it from scratch
$config = new Config([
    Config\Options::NEIGHBORHOOD_SIZE => 1,
    Config\Options::NEIGHBORHOOD_TYPE => Config\NeighborhoodOptions::NEIGHBORHOOD_TYPE_NEUMANN,
    Config\Options::STATES => 14,
    Config\Options::THRESHOLD => 1,
    Config\Options::ROWS => 50,
    Config\Options::COLUMNS => 50,
]);

In \Barryvanveen\CCA\Config\Presets.php you can find all available presets.

Running the CCA

// get a single state
$runner = new Runner($config, Factories\CCAFactory::create($config));
$state = $runner->getLastState(234);
 
// get a set of states
$runner = new Runner($config, Factories\CCAFactory::create($config));
$states = $runner->getFirstStates(123);
 
// get a set of states that loops (if possible)
$runner = new Runner($config, Factories\CCAFactory::create($config));
$states = $runner->getFirstLoop(500);  

The Runner is probably sufficient for most scenarios but if you want more control you can control the CCA yourself. Just look at the Runner implementation to get an idea of how this works.

Generating images

// create a static Gif from a single stae
$image = Generators\Gif::createFromState($config, $state);
$image->save('/path/to/output.gif');
 
// create a static Png from a single state
$image = Generators\Png::createFromState($config, $state);
$image->save('/path/to/output.png');
 
// create an animated Gif
$image = Generators\AnimatedGif::createFromStates($config, $states);
$image->save('/path/to/output.gif');

Examples

The /examples folder contains some scripts to generate different kinds of images. Here are some example images:

static gif from amoeba preset static gif from cca preset static gif from lavalamp preset static gif from cyclic spirals preset

animated gif from squarish spirals preset animated looping gif from cyclic spirals preset animated looping gif from cca preset animated looping gif from 313 preset

Changelog

Please see the releases for more information on what has changed recently.

Testing

$ composer test

Contributing

Please see CONTRIBUTING and CODE_OF_CONDUCT for details.

Security

If you discover any security related issues, please email barryvanveen@gmail.com instead of using the issue tracker.

Acknowledgments

All preset configurations are taken from http://psoup.math.wisc.edu/mcell/rullex_cycl.html which is the work of Mirek Wójtowicz.

The colors for the images are generated using talesoft/phim which is a project by Torben Köhn.

Animated gifs are created using lunakid/anim-gif.

Credits

License

The MIT License (MIT). Please see License File for more information.