A world in dk(decay/denmark) » cimg http://rotand.dk Just another pointless weblog Sat, 30 Nov 2013 21:03:48 +0000 en-US hourly 1 https://wordpress.org/?v=4.3.18 Java life simulator http://rotand.dk/2007/06/01/java-life-simulator/ http://rotand.dk/2007/06/01/java-life-simulator/#comments Fri, 01 Jun 2007 14:31:54 +0000 http://rotand.dk/blog/2007/06/01/java-life-simulator/ Having played a bit with cellular automata’s – and looking at various other rulesets. I started to wonder what the result would be if every cell had i different ruleset, or just different types of cells interacting.

And what if there were mutations and sexes involved ?

Well getting ideas like that are dangerous, they usually result in some kind of coding project. And well this time I felt the urge to implement some features. Instead of continuing to try to write in c++ with CImg or SDL, which is kinda of new to me. I went back to java. Mainly because I’m more experienced with java and I have some code that I can reuse from earlier projects ( a flamefractal render and a small framework for rendering strange attractors ).

So I started hacking away making nice features and building a framework for experiments. Where there are generic cells and loose coupling using interfaces.

A cell just needs to implement :

  • a constructor taking its coordinates, and the map it belongs to as arguments
  • a isAlive()
  • boolean iterate () which gets it neighbors from the map and decides wheter it should be dead or alive in the next round
  • nextState() instead of using buffers I decided to have a method in a cell that changes it state, and as a ugly temporary hack at the same time returns value to determine its color

Of course in order to apply more interesting rules I need them to have a type, and they should be able to decide their fate based upon their neighbors types. And I still need to figure out a way to implement some kind of generic ruleset, if/when I need a way to mutate.

All the cells are placed in a double array (cells[][] ) and there’s a CellMap – class keeping track of them. I tried optimizing by only looking a cells that are alive, and their neighbors – instead of looking at every cell in each iteration. I used a MVC pattern so the map runs in its own thread and after each iteration it calls the gui to be painted. Its quite fast at iterating, but the painting process takes forever. Alas, the next step is to use a buffered image to speed op the rendering process, or …

After all these fancy designpatterns, threading all it does at the moment is a simple “life-like” cellular automata, but Its made for extending. And It has nice coloring., in earlier experiments i have made a generic gradient class, which came in very handy.

By the way please take a look at my online collection of random strange attractors

]]>
http://rotand.dk/2007/06/01/java-life-simulator/feed/ 0
Cellular Automata II http://rotand.dk/2007/05/30/cellular-automata-ii/ http://rotand.dk/2007/05/30/cellular-automata-ii/#comments Wed, 30 May 2007 20:46:28 +0000 http://rotand.dk/blog/2007/05/30/cellular-automata-ii/ As mentioned before I’ve made a one dimensional Cellular Automata(CA). And although it was intriguing they quickly became very predictive. Actually it was in two dimensions. As the a y-coordinate represented time, but since the states were only depending an the adjacent neighbor’s to the right or left its considered to be one-dimensional.

But they are not as fascinating as CA’s where you expand the neighborhood to include all the eight adjacent cells. That’s called the Moore Neighborhood.

Representing the time dimension. As the intersting part is the evolution of the system, representing time is essential. With a two-dimensional CA it has to be done using animations, and who doesn’t like blinking pixels. This makes CA’s makes even more fun.

In order to decide the state of a cell I look at the Moore neighborhood for every cell on the grid, and count how many neighboring cells are alive. If a cell is alive, then we have to determine whether it survives or dies. If a cell is dead / empty there is the posibility of birth. By defining how many neighbors a cell has to have to survive, and hov many it takes to “give birth” it is possible to run the automata. And this is enough to define a highly complex behavior.

The ubiquitous Conway’s game of life survives if there are 2 or 3 neighbor’s alive and a cell is born if 2 cells are alive, this can be written as : S/B = 23/2. Using a notation like this makes it easy to try out new rules, just adjusting the S/B sets.

I made a small c++ program again using CImg to render these automatons. It’s a rather quick hack, and as such there are plenty off room for optimizing.

But for now it does its job and I don’t intend to develop it further, at least for now. And I think that CIimg might not be the best api for this, I need to look into SDL.

I spent way too much time hacking around to get a nice coloring scheme, and well CImgs intended users are making image manipulation, and not generating images from scratch. SDL is’t intended to generate images from scratch either, but as far as i can tell its easier to manipulate images as int arrays.

Well it does work and i were able to try out some of the rules listed at Wikipedia article about Life like cellular automatons, there online java simulators somewhere on the Internets so I won’t bother to upload any videos – as its much more fun to watch in action an play about with the settings as you go along. If you want to play with the code, well here it is : Cellular Automaton code it’s ugly and you need CImg.h

And now I’ve just downloaded EvoCell which has all the features and a lot more.

Online simulator : MJcell this is a very good implementation. Its fast and has lots of rules and patterns

]]>
http://rotand.dk/2007/05/30/cellular-automata-ii/feed/ 0
Cellular automata http://rotand.dk/2007/05/29/cellular-automata/ http://rotand.dk/2007/05/29/cellular-automata/#comments Tue, 29 May 2007 07:38:57 +0000 http://rotand.dk/blog/2007/05/29/cellular-automata/ rule 54
rule 30

Are quite fun and very intriguing.

What and how.

Generally you represent cells on a grid, and you “evolve” them using simple rules, based on how many neighbors they have.

In the simple version I so far have toyed around with. A cell is either dead or alive.

In one dimension, there are a ruleset and notation called the wolfram rules. Where you use a integer to represent a rule. Each cells faith is based on its own state and the state to its two adjacent neighbors. Here is a table from wikipedia

Rule 30 cellular automaton

current pattern 111 110 101 100 011 010 001 000
new state for center cell 0 0 0 1 1 1 1 0

Its rule 30 because 11110 is the binary representation of 30. This notation makes implementation quite easy. I implemented it this way :

  • Create an image, fill with black
  • Start with initializing the bottom row, put in cells of a different color at random
  • for the row above calculate for each position :
  • int shift =0;
    if (img( (i>0) ? i-1 : img.dimx()-1 , j, 0,0)==c[0])
    shift +=4;
    if (img(i, j, 0)==c[0])
    shift +=2;
    if (img((i+1)%img.dimx(), j, 0)==c[0])
    shift +=1;

  • basically its if left neighbor is alive shift = 4, itself is 2 and right neighbor 1. I wrap neighborhoods so its topological like a cylinder.
  • Heres the smart thing about wolfram rules, we now have the state of the neighborhood represented as an integer. And if we shift the rule with this integer, the lowest bit will represent the state of the current cell. (rule<<shift)&1.
  • continue through all rows from top to bottom.

This will generate some interesting patterns, rule 30 is a lot a triangles. And they have some interesting properties. I make them because I find them visually appealing, but the do have other merits. Look at the wikipedia entry for more

Technicalities : I made a small program in c++ using CImg. CImg is easy to use, and its quick to setup up a disply and handle events. (eg lefttclik =use same rule, initializ again, rightclik = next rule)

And though one dimensional cellular automata’s are intriguing and “fun”. They quickly get boring to look at, so I went for two dimensions. More about that in a later post.

]]>
http://rotand.dk/2007/05/29/cellular-automata/feed/ 0