#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <stdio.h>

#define MAG 200.0
#define abs(x) (x < 0.0? -x : x)
main() {
  int i;
  double x,y,d,ar,ai,x2,y2;
  gopen();

  for (ar = 0.0; ar < 5.0 ; ar += 1.0/MAG) {
	for (ai = 0.0; ai <5.0; ai += 1.0/MAG) {
	  x = 0.1 ; y = 0.1;
	  for (i=0; i<200; i++) {
		d = x*x*x*x+2*y*y*x*x+y*y*y*y;
		x2 = (2.0/3.0*x*x*x*x*x+4.0/3.0*y*y*x*x*x-1.0/3.0*ar*x*x+(2.0/3.0*y*y*y*y-2.0/3.0*ai*y)*x+1.0/3.0*ar*y*y)/d;
		y2 = (2.0/3.0*y*x*x*x*x+(4.0/3.0*y*y*y-1.0/3.0*ai)*x*x+2.0/3.0*ar*y*x+2.0/3.0*y*y*y*y*y+1.0/3.0*ai*y*y)/d;
		if (abs(x2) > 30.0 || abs(y2) > 30.0) {
		  break;
		}
		if (i == 199) {
		  setpixel((int) (ar*MAG),(int) (ai*MAG));
		}
		x = x2; y = y2;
	  }
	}
	gFlush();
  }
  getchar();
  gclose();
}


/* -------------------- main code of glib.c ---------------- */
#define XSIZE 640
#define YSIZE 400

/* global variables */
Display *glib_d;
Window glib_w;
GC glib_gc;
XSetWindowAttributes glib_a;

static int glib_x = 0;
static int glib_y = 0;

/* functions exported */
int gopen() 
{
	glib_d = XOpenDisplay(NULL);
	glib_w = XCreateSimpleWindow(glib_d,RootWindow(glib_d,0),100,100,
								 XSIZE,YSIZE,2,0,BlackPixel(glib_d,0));
	glib_a.override_redirect = 1;
	XChangeWindowAttributes(glib_d,glib_w,CWOverrideRedirect, &glib_a);
	XMapWindow(glib_d,glib_w);
	glib_gc = XCreateGC(glib_d,glib_w,0,0);

	XSetBackground(glib_d, glib_gc, BlackPixel(glib_d, 0));
	XSetForeground(glib_d, glib_gc, WhitePixel(glib_d, 0));

	/* intialize line attributes */
	XSetLineAttributes(glib_d,glib_gc,1,LineSolid,CapButt,JoinMiter);
}

int gclose() 
{
	XFlush(glib_d);
	XCloseDisplay(glib_d);
}

int gFlush() 
{
	XFlush(glib_d);
}



int setpixel(int x, int y)
{
	XDrawPoint(glib_d,glib_w,glib_gc,x,y);
}

/*
main() 
{
	int k;
	gopen();
	for (k=10; k<=500; k++) {
		setpixel(k,k);
	}
	moveto(100,50);
	lineto(400,50);
	for (k=10; k<=300; k += 20) {
		lineto(k,2*k);
	}
	gFlush();
	getchar();
	gclose();
}
  
*/
