Many people seem to be having difficulties with the ComplexPoly lab. This is a bit surprizing for me, because the actual idea of the lab is very simple. Here it is:

Take complex numbers that correspond to points on a circle, plug them into the your complex polynomial P() and graph the resulting complex numbers.

What you get is "the image of the circle" under P(). All the code you need for this is:

  double R = YOUR_RADIUS;           // the radius of your circle
  Complex Z, W;
  double x, y;

  for( int d=0; d < 360; d += 1 ){  // d is the algle in degrees   
                                    //    that runs around the circle
    Z.PolarSetDegrees( R, d );      // this makes a new complex number
                                    //    on the circle of radius R
    W = P( Z );                     // W is the "image" of Z  

    W.Get( x, y );                  // x, y are coordiantes of the point W

    // Now graph the point with coords (x, y)
  
  }  // do the next point on the circle.
This is IT. No more code is required. You obtain the coords of 360 points on the circle, you run them through P(), you plot the results. The only thing that remains is to add the line of code that graphs the point. Notice that the coordinates you plug into P() are "real corrds", and the result W = P(Z) also has "real coords". If you plot the pixel at (x, y), two bad things will happen:
  1. (0,0) is the top left corner of your screen, so no points with negative coords will be seen,
  2. the picture may be too large or too small to fit on your screen.
Therefore, you neeb to shift your picture to the center of your screen, i.e. (200, 200), and scale it so that it fits. Hence the code that graphs the point (x,y) is not one line, but three:
 
  double screenX = factorX * x + offsetX;  // offsetX == offsetY == 200
  double screenY = factorY * y + offsetY;  //   for a screen 400x400
                                           // factor is the blow-up factor
 
  SetColorPixel( screenX, screenY, RGB(255, 0, 0) );  // red
Set factorX, factorY to something that fits your picture, and enjoy.
All the rest in the lab is just icing on this cake. First of all, each P() produces a different picture, which may need a different scale. If you store all the points in an array before graphing them, you can find the maximum radius you'd need for this picture and scale accordingly. Hence the array Point2D data[ ] in the lab. Then you might want to do the scaling "nicely" (although for this simple problem the above two lines and four variables factorX, factorY, offsetX, offsetY are quite enough). In the lab, this is what LinearScale2D does: it's a class that remembers the factors and offsets, and applies them to a Point2D from data[] to get a Point, i.e. a Mac graphics point. See the function PlotData in Plot2D.h and Plot2D.cp for how the data[] is plotted. Look in Scale2D.h to see what LinearScale2D is doing. That will refer you to LinearScale (in Scale.h), which simply encapsulates the formula with factor and offset.

Let me say once again, that nothing of this is strictly necessary to get the result -- the essential functionality of this program is in the few lines above.


Sergey Bratus
Last modified: Sat Mar 6 10:04:56 EST 1999