/* 
 * MDist - find out if a point is in the Mandelbrot set, and estimate
 *         the distance to the border.  That is, compute the radius of
 *         a disk that is entirely inside (or outside) the set. If the
 *         test point C is inside M, the potential Gm(C) is also
 *         computed.
 *
 * $VER: MDist.h 1.2
 */

#ifndef MDIST_H_INCLUDED
#define MDIST_H_INCLUDED

#include <math.h>

/* Complex numbers - a great GNU C extension!
 *
 * But it seems unreliable to take the address of a complex variable,
 * as the real and imaginary part are stored in memory in an undefined order,
 * that seem to change from one compilation to the other :-( . */
 
typedef __complex__ double complex;

#define RE(x) (__real__ (x))
#define IM(x) (__imag__ (x))

struct MDistParameters
{
  int MaxIter, MaxIterOutside, MaxPeriod;
/* These variables should hold the *SQUARE* of intended value. */
  double MaxZAbs, ERel, EAbs;
};

/* Return value */
typedef enum { Inside, Outside, Unknown, InsideUnknown, OutsideUnknown }
        MDist_t;

/* Macros */
#define SQR(x) ((x)*(x))

inline static double
  ABS2(complex z)
{
  return ( SQR(RE(z)) + SQR(IM(z)));
}

/* Function prototype */
MDist_t MDist(double point[2], double *radius, double *potential,
	      struct MDistParameters *parameters);

#endif
