Posted on 1998-05-06
Last Modified: 2010-04-15
This is probably ridiculous...but; I need to know how to implement a complex (a.k.a. imaginary) formula type thing in order to generate the mandelbrot set. In fact I'm making this question worth 600 points (and possibly more) in order to have someone help me with this program I need to write for a grade. I'm not looking for someone to write the program for me because then I wouldn't learn anything but I do need alot of help.
For the mandelbrot set the rule is z->z*z+c.
C is the complex number. You may already know this but I should say it any ways.
So I guess to make a run down of this question is.....I need to know how to program complex numbers and then graph them. Could someone please help?
Thank you,
Question by:crash020297
LVL 84

Expert Comment

ID: 1249644
(a + b*i) + (c + d*i) = (a+c) + (b+d)*i

(a+b*i) * (c+d*i) =
(a*c) + (a*d*i) + (b*i * c) + (b*i * d*i) =
(a*c) + ((a*d)+(b*c))*i + (b*d)*(i*i) =
((a*c)-(b*d)) + ((a*d)+(b*c))*i

LVL 10

Accepted Solution

RONSLOW earned 600 total points
ID: 1249645
To program them, use a struct that contains the real and imaginary part


typedef struct {
  double r;
  double i;
} Complex;

Now write functions for working with complex numbers.


Complex Add (Complex x, Complex y) {
  Complex z;
  z.r = x.r + y.r;
  z.i = x.i + y.i;
  return z;

The rules for complex arithmetic are fairly straighforward (I think that is what ozo was saying.

For graphing, use you graphics library and plot the real part as the x coord and imaginary as the y coord.  The details as to how to do this depends on what graphics library you have.

LVL 10

Expert Comment

ID: 1249646
For info on the Mandelbrot algorithm, look at

You will need to be able to multiply two complex numbers (or at least square one) and to be able to add them, and be able to get the magnitude (||) of it to test for the exit condition.

The 'nice' way to do this is to write functinos for each operation.  More efficient is to expand out the calculations as one operation.  However, most compilers will be able to do optimizations (especially if the coplex functions are made static - in which case they will often be inlined).

Also, for efficiency, the operator may be better written as working on pointers.  eg

void AddIt (Complex* this, const Complex* that) {
  this->r += that->r;
  this->i += that->i;

This is quicker because 1) we are only passing pointers and not the whole complex struct. 2) we aren't constructing any temporary objects.

ScreenConnect 6.0 Free Trial

Discover new time-saving features in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

LVL 10

Expert Comment

ID: 1249647
At the same site is a page with complex operations

Here is (an excerpt) the text of that page .. You should be able to use these to write complex arithmetic functions

[NOTE: If you were able to use C++, then you could write a Complex class that understood operators like + etc, or just use the complex datatype from the standard library]

(note: a,b are reals, x,y are complex, i is the square root of -1)
Powers of i: i^2 = -1
Addition: (a+i*b)+(c+i*d) = (a+c)+i*(b+d)
Multiplication: (a+i*b)*(c+i*d) = a*c-b*d + i*(a*d+b*c)
Division: (a+i*b)/(c+i*d) = (a+i*b)*(c-i*d)/(c^2+d^2)
Exponentiation: exp(a+i*b) = exp(a)(cos(b)+i*sin(b))
Sine: sin(x) = (exp(i*x)-exp(-i*x))/(2*i)
Cosine: cos(x) = (exp(i*x)+exp(-i*x))/2
Magnitude: |a+i*b| = sqrt(a^2+b^2)
Log: log(a+i*b) = log(|a+i*b|)+i*arctan(b/a)
(Note: log is multivalued.)
Log (polar coordinates): log(r*e^(i*theta)) = log(r)+i*theta
Complex powers: x^y = exp(y*log(x))
DeMoivre's theorem: x^a = r^a * [cos(a*theta) + i * sin(a*theta)]

More details can be found in any complex analysis book.
LVL 10

Expert Comment

ID: 1249648
Another good way is to use fractint. Fractint is distributed with the complete source code, so you can look in there and find good and fast algorithms for generating fractals.
LVL 10

Expert Comment

ID: 1249649
That might be cheating a bit .. and that program (along with many others) is referred to in the sites I suggested.


Author Comment

ID: 1249650
Thanks alot every one! This is so very helpful!
I really appreciate it.

Featured Post

Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question