Path: utzoo!attcan!utgpu!jarvis.csri.toronto.edu!rutgers!att!cbnewsl!mpl From: mpl@cbnewsl.ATT.COM (michael.p.lindner) Newsgroups: comp.lang.c Subject: Re: Mandlebrot set Summary: here's one, but be careful Message-ID: <889@cbnewsl.ATT.COM> Date: 22 Jun 89 19:25:37 GMT References: <2492@blake.acs.washington.edu> Distribution: na Organization: AT&T Bell Laboratories Lines: 52 In article <2492@blake.acs.washington.edu>, themod@blake.acs.washington.edu (Chris Hinton) writes: > Hello to all you netters out there, maybe one of you can help me. I am > in search of a function that, given a point in the complex plane, will return > the number of iterations it takes to determine if the given point is in the > Mandlebrot (sp?) set. > Put more simply, I would call the function in this way : > > s = function(real,imag); . . > + Chris Hinton (aka The Mod) | The veiws presented in the above posting | Well, the number of iterations may be infinite, so if you wrote a function like the one you describe, it might never terminate. In general, one specifies a maximum number of iterations to try, after which one assumes the point is in the Mandelbrot (sp!) set. In addition, your function as defined does not say whether or not the point is actually in the set. So, I give you the following, which returns the number of iterations after which it is known the point is NOT in the set (if the value ever goes outside a circle of radius 2 it is not in the set). If the function returns MAX_ITER, the maximum number of iterations to try, assume the point is in the set. double func(real, imag) double real, imag; { int n; double newreal, newimag, re2, im2; for (n = 0; n < MAX_ITER; n++) { if ((re2 = real * real) + (im2 = imag * imag) > 4.0) break; /* point is not in Mandelbrot set */ /* Xn+1 = Xn^2 + Xn */ newreal = re2 - im2 + real; newimag = 2.0 * real * imag + imag; if (newreal == real && newimag == imag) { n = MAX_ITER; break; } } return n; } For a good discussion of the Mandelbrot set, see the January 1989 Scientific American, or read "Chaos: Making a New Science" by James Gleick, published by Penguin Books. Mike Lindner attunix!mpl AT&T Bell Laboratories 190 River Rd. Summit, NJ 07901