Sunday, December 11, 2011

Interference Patterns

Edmund Harris recently posted some beautiful pictures created with modular arithmetic. I was surprised and intrigued to see this one, because it so closely resembled an image I made some years ago without thinking about modular arithmetic at all. I dug through my disorganized backups, and finally found it from circa 2004.

These images don't resize well. The scaling can both remove structure and produce artifacts that are not in the original, so here is an example detail portion of the full image.

You can click here for the full version (4.5 MB).

These images are fractal looking, with repeating patterns at smaller scales. You can see the integers and fractions living in there. Perhaps cooler, the smaller repetitions seem to exist on a "higher level" than the basic repeating unit. They float along the patterns below, having a sort of ethereal existence.

If Edmund used modular arithmetic, how was my image made? Consider the function sin(x2). Here is a plot of it in the range of 0 to 15. You can see the frequency of the oscillations is increasing quickly with larger x.

Now consider sampling this function at discrete values of x in the larger range of 0 to 1000. When you get to higher values of x, this continuous function is oscillating back and forth a huge number of times between each successive value of x. Is there a pattern to be seen in the discretely sampled output values?

Finding a pattern on a 1D line is not so easy, but you can plot discrete values of sin(d2) on a 2D grid, where d is the distance of an image pixel to the origin. When the function value for each discrete pixel is calculated and plotted as a color, voila! The image appears, and the patterns undeniably jump out.

At the time, I attributed the effect to a Moire-like phenomenon, with an interference between the discrete grid of pixels and the continuous function. It was neat to read Edmund's explanation that modular arithmetic is behind the scenes. Maybe modular arithmetic is part of the magic behind Moire patterns?

I experimented with other functions as well, and here is a favorite. I love that it has both elliptical and hyperbolic patterns in it, and the transitioning between the two.

You can click here for the full version (2 MB).


  1. Whoa, when scrolling the page, the last image gets some sort of 'awesome' ghosting/aliasing effect =O

  2. If you download the full version and move it around on your screen, the aliasing looks like electric field lines :)

    Moving vertically leads to field lines in one direction, while moving horizontally leads to a different set of field lines (perpendicular to the first set).

  3. Man, I love this. I did some work with this earlier this year, but I used Scratch, a drag and drop programming language that I use with my 5th graders.

    Are these graphics simply done with mathematica? Any chance I could see the code or you could email me the notebook?

  4. Hi Paul. Thanks, these were made with a C++ program I wrote, which used the Microsoft CImage class to set pixels. It wasn't a user-friendly app, and I was changing things at compile time to do my experimenting. Converting into Mathematica code seems like it would be straightforward, but I don't have access to that.

    However, I'd be happy to dig it up and clean up the code to share if you thought it'd be useful to you. Let me know!