The cat map is a classic example of a 2D chaotic map, taught in introductory dynamical systems courses. Mathematically it is a toral automorphism, described by the following iterated function:
One way to visualize the map is by applying it to an image, treating the image as a grid of pixels and mapping each pixel to a new location on the grid:
The mapping is periodic, meaning each pixel will eventually map back to its original location.
import processing.video.*;
PImage img, imgOrig;
MovieMaker mm;
boolean recordMovie = true;
int iteration = 0;
void setup()
{
imgOrig = loadImage("puffball.png");
img = imgOrig;
size(img.width, img.height, P2D);
if(recordMovie)
{
mm = new MovieMaker(this, img.width, img.height, "catmap.mov");
}
}
boolean equal(PImage p1, PImage p2)
{
for(int i = 0; i < p1.pixels.length; i++)
{
if(p1.pixels[i] != p2.pixels[i]) return false;
}
return true;
}
void draw()
{
image(img, 0, 0);
if(recordMovie) mm.addFrame();
if(equal(img, imgOrig) && iteration > 0)
{
noLoop();
if(recordMovie) mm.finish();
}
PImage imgNext = createImage(img.width, img.height, RGB);
for(int y = 0; y < img.height; y++)
{
for(int x = 0; x < img.width; x++)
{
int pixel = img.pixels[y*img.width + x];
int xNew = (2*x + y) % img.width;
int yNew = (x + y) % img.height;
imgNext.pixels[yNew*img.width + xNew] = pixel;
}
}
img = imgNext;
iteration++;
}