/*
 * simple-drawing.c - demonstrate drawing of pixels, lines, arcs, etc.
 *                    on a window. All drawings are done in black color
 *                    over a white background.
 */
/* RUNS UNDER LINUX */
/* compile with:
   gcc mandtext.c -o mandtext
*/

#include <stdio.h>
#include <stdlib.h>		/* getenv(), etc. */
#include <unistd.h>		/* sleep(), etc.  */
#include <math.h>               /* fabs() */

typedef int bool;

#define false 0
#define true 1


/*
 * function: create_simple_window. Creates a window with a white background
 *           in the given size.
 * input:    display, size of the window (in pixels), and location of the window
 *           (in pixels).
 * output:   the window's ID.
 * notes:    window is created with a black border, 2 pixels wide.
 *           the window is automatically mapped after its creation.
 */

#define MAXINTS 10

#define IN_MANDELBROT_SET(x, y) (inMandelbrot(x, y) == MAXINTS)


unsigned int
inMandelbrot(double origReal, double origImaginaryCoefficient)
{
    unsigned int j;
    double imagSquared, rSquared;
    double newReal, newImaginaryCoefficient;
    double oldReal, oldImag;
    double twoRI;

    for (j = 0, newReal = origReal, newImaginaryCoefficient = origImaginaryCoefficient;
         j < MAXINTS;
         j++)
    {
        rSquared = newReal * newReal;
        imagSquared = newImaginaryCoefficient * newImaginaryCoefficient;

        if (rSquared + imagSquared > 10)
	{
            printf("%d: real %g squared is %g; imaginary %g squared is %g, sum %g, too big!\n",
                   j + 1, newReal, rSquared, newImaginaryCoefficient,
                   imagSquared, rSquared + imagSquared);
            break;
	}

        oldReal = newReal;
        oldImag = newImaginaryCoefficient;

        /* (Ii + R)(Ii + R) = (I**2)(i**2) + 2RIi + R**2 =
         * -(I**2) + 2RIi + R**2 =
         * 2RIi + R**2 - I**2
         * so the new imaginary coefficient is 2RI
         * and the new real is R**2 - I**2.
         * Then, don't forget, we add the original numbers back in.
         */

        twoRI = 2.0 * newImaginaryCoefficient * newReal;
        newImaginaryCoefficient = twoRI + origImaginaryCoefficient;
        newReal = (rSquared - imagSquared) + origReal;
        printf("%d: (%g %c %g i) squared = %g %c %g i + %g i**2 = %g %c %g i; add in the original %g %c %g i, and we get %g %c %g i\n",
               j + 1, oldReal, (oldImag < 0.0 ? '-' : '+'), fabs(oldImag),
               rSquared, (twoRI < 0.0 ? '-' : '+'), fabs(twoRI), imagSquared,
               rSquared - imagSquared, (twoRI < 0.0 ? '-' : '+'), fabs(twoRI),
               origReal, (origImaginaryCoefficient < 0.0 ? '-' : '+'), fabs(origImaginaryCoefficient),
               newReal, (newImaginaryCoefficient  < 0.0 ? '-' : '+'), fabs(newImaginaryCoefficient));
    } /* for loop */

    if (j >= MAXINTS)
    {
        printf("After %d spins, we're still pretty small. So far, so good.\n", j);
    }

    return j;
}

void
main(int argc, char* argv[])
{
    double real, imaginary;

    printf("Test various points in the complex plane for membership in the Mandelbrot set\n");

    do
    {
        printf("Enter real component: ");
        scanf("%lg", &real);
        printf("Enter imaginary component: ");
        scanf("%lg", &imaginary);
        if (real == 0.0 && imaginary == 0.0)
	{
            printf("you entered 0.0, 0.0. All done. Bye!\n");
            break;
	}

        (void) inMandelbrot(real, imaginary);
    } while (true);

    exit(0);
}
