Thursday, February 4, 2010

Mandelbrot Fractal - Part 2


This is the follow-up to part 1. I'm going to continue the explanation from part 1 and describe some initial pseudocode. Beware: this pseudocode is slow. I'll probably handle optimization in a later post.

In my previous post I gave you two formulas that describe the Mandelbrot set :

|z| > 2
z = z2 + c


I also told you what z and c were (complex numbers), and how c relates to the screen point.

First of all, I'm going to describe 3 basic operations with complex numbers. You'll use these to calculate the formulas.

a and b are complex numbers, where .real is the real part, and .imag is the imaginary part.

(a + b).real = a.real + b.real
(a + b).imag = a.imag + b.imag

(a * b).real = (a.real * b.real) - (b.imag * b.imag)
(a * b).imag = (a.imag * b.real) + (a.real * b.imag)

|a| = sqrt( (a.real * a.real) + (a.imag * a.imag) )

Be careful with multiplication: the imaginary part depends on the original real part. This means, save the real part in a temporary variable! It drove me crazy when I first programmed the fractal.

In my previous post, I told you the Mandelbrot set is evaluated using a iterative process: if the condition (|z| > 2) is false, try again, until the counter reaches the limit, or the condition evaluates to true. That counter is going to be important in the process, since it'll determine the final color for the point. I'll refer to it as i.

Knowing this, we can write down some pseudocode, like this:

i = 0
limit = 20

z.real = 0
z.imag = 0

c.real = x
c.imag = y

while i < limit then
- if sqrt( z.real2 + z.imag2 ) > 2 then
-- plot as white
-- break
- z = z2 + c
- i = i + 1

If you plot that code, using C, you'll notice two things:
  • It's ugly.
  • It's slow.

It should look like the photo, if everything ia correct. The photo fractal uses limit=50. It's ugly, but it changes dramatically when you add color. I'll address that in the next part.

Tuesday, February 2, 2010

Mandelbrot Fractal - Part 1

I'm going to describe the Mandelbrot fractal, the most beautiful fractal that I know. I assume you have experience with complex numbers (addition, multiplication...).


First of all, what is the Mandelbrot fractal? It's a set of points. To draw the fractal, you must plot every point that is in the Mandelbrot set. The Mandelbrot set is defined using this formula :
z = z2 + c
|z| > 2

where z and c are complex numbers and c is the point you are testing, such as : c.real = x, c.imag = y.

If the condition is true, the point is in the set. If it isn't true, you must iterate using the first formula. If, after a maximium number of iterations, the condition is still false, then the point isn't in the set.

Tuesday, January 26, 2010

Plasma effect


I'm going to describe my version of the popular plasma demoscene effect. There may be another way of doing it, but my version works.

The basic effect uses an index into a pallete, which is defined using a sine based equation. The index depends on two variables (x and y). These are the coordinates of the screen point being evaluated. Example:

idx = sin((PI * x) / 45.0f) + sin((PI * y) / 90.0f);

45 and 90 are just values I've chosen randomly. Change them to alter the plasma. To plot this in screen, do this: for each point in the screen, apply the code. Use idx as an index into a greyscale pallete. There is a problem : it's static. The effect doesn't move. But don't worry, because it's very easy to animate! Change the first formula to:

idx = sin((PI * x) / 45.0f) + sin((PI * y) / 90.0f) + sin((PI * i) / 180.0f);

There's a new element in the formula (sin((PI * i) / 180.0f)). There's a new variable in it (i). If you increment that variable, the effect will animate. You could increment it after a screen draw.

Tuesday, January 19, 2010

M4GTracker


Smiker and I are working in a tracker for GBA, known as M4GTracker. It's popular in the chiptune community, because it's one of the first trackers for GBA. We are currently redoing the UI system, using artwork from iLKke. You can find the link here: http://8bitcollective.com/forums/viewtopic.php?id=13641&p=1.