/********************************************************************

	Program:.............Your Own Biomorph
	Done by:.............TitBit
	Begin on:............13-Jul-90
	End on:..............13-Jul-90
	
	Compiler:............Lattice V5.02 or >>
	Options:.............lc -ff -Lf Your_Bio
	
	Target:..............Try your own functions and generate 
			     biomorph.  If you find something of
			     very interesting write us to:
			     
			     Stefano Luciani
			     via Maggiore Leggero, 46
			     44029 Porto Garibaldi
			     (Ferrara) ITALY
			     
			     Alessandro Scardovi
			     via Ravegana, 162
			     48100 Ravenna
			     ITALY
			     
			     Federico Spinola
			     via Saffi, 1
			     Bologna
			     ITALY
			     
*******************************************************************/

/********************************************************************/

                           /* DEFINITIONS */

#define INFX       -10
#define SUPX        10
#define INFY       -10
#define SUPY        10
#define CREAL      0.5
#define CIMAGINARY 0

/********************************************************************/

#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <exec/types.h>
#include <intuition/intuition.h>
#include <time.h>

struct IntuitionBase *IntuitionBase;
struct GfxBase *GfxBase;
struct Screen *CustScr;
struct Window *Wdw;
struct ViewPort *WVP;

#define Rp Wdw->RPort

#define PUR1   0x101
#define PUR2   0x302
#define PUR3   0x403
#define PUR4   0x503
#define PUR5   0x504
#define PUR6   0x604
#define PUR7   0x704
#define PUR8   0x805   /* Scelte Colori */
#define PUR9   0x905
#define PURA   0xA05
#define PURB   0xB05
#define PURC   0xC06
#define PURD   0xD06
#define PURE   0xE07
#define PURF   0xF07
#define WHITE  0xFFF

static USHORT colormap[16] =
{
	WHITE,
#define BGRP 0
	PUR1,
#define PP1P 1
	PUR2,
#define PP2P 2
	PUR3,
#define PP3P 3
	PUR4,
#define PP4P 4
	PUR5,
#define PP5P 5
	PUR6,
#define PP6P 6
	PUR7,
#define PP7P 7
	PUR8,
#define PP8P 8
	PUR9,
#define PP9P 9
	PURA,
#define PPAP 10
	PURB,
#define PPBP 11
	PURC,
#define PPCP 12
	PURD,
#define PPDP 13
	PURE,
#define PPEP 14
	PURF,
#define PPFP 15
};

struct TextAttr StdFont =
{
   "topaz.font",
   TOPAZ_EIGHTY,
   FS_NORMAL,
   FPF_ROMFONT,
};

struct NewScreen NewCustScr =
{
   0,0,
   320,256,4,
   0,PP3P,  /* colori */
   NULL,
   CUSTOMSCREEN,
   &StdFont,
   "Tit-Bit presents:",
   NULL,
   NULL,
};

struct NewWindow NewWdw=
{
   0,12,
   320,244,
   0,31,  /* colori */
   CLOSEWINDOW,
   NOCAREREFRESH | ACTIVATE | WINDOWCLOSE,
   NULL,
   NULL,
   "Your Own Biomorph",
   NULL,
   NULL,
   0,0,
   0,0,
   CUSTOMSCREEN
};




void main()
{
	void demo();
	
   IntuitionBase = (struct IntuitionBase*)OpenLibrary("intuition.library",0);
   if (IntuitionBase==NULL) exit(FALSE);
   GfxBase = (struct GfxBase*)OpenLibrary("graphics.library",0);
   if (GfxBase==NULL) exit(FALSE);
   if ((NewWdw.Screen =CustScr =(struct Screen*)OpenScreen(&NewCustScr))==NULL)exit(FALSE);
   if((Wdw=(struct Window*)OpenWindow(&NewWdw))==NULL)exit(FALSE);
   WVP=(struct ViewPort*)ViewPortAddress(Wdw);
   LoadRGB4(WVP,&colormap,32);
   
   demo();
   
   Wait(1<<Wdw->UserPort->mp_SigBit);
   CloseWindow(Wdw);
   CloseScreen(CustScr);
   CloseLibrary(GfxBase);
   CloseLibrary(IntuitionBase);
   return;
}


void demo()
{
	double u=2,v,h=10,g;
	double nx,ny,latox,latoy,incremx,incremy;
	register double zre,zim,temp;
	int colore,j,m,nn,n,k;
	
	v=NewWdw.Width-3;
	g=NewWdw.Height-2;
	nx=v-u;
	ny=g-h;
	n=0;
	m=0;
	latox=(SUPX-INFX);
	latoy=(SUPY-INFY);
	incremx=latox/nx;
	incremy=latoy/ny;
	
	if ((SUPX>INFX)&&(SUPY>INFY))
	for (j=h;j<=g;j++)
	{
		
		for (k=u;k<=v;k++)
	
		{	
			zim=INFY+(m * incremy);
			zre=INFX+(n * incremx);
			
			SetAPen(Rp,5);
			WritePixel(Rp,k,j);
			
			for(nn=1;nn<=10;nn++)
			{
			
	/* This formula computes Z^2 + C. That is Z*Z + C.
	   By complex analisys is:
	   
	   Z = Zreal + i * Zimag
	   Z*Z = Zreal*Zreal - Zimag*Zimag + 2*i*Zreal*Zimag
	   
	   To try new formulas change from here............*/
	   
			temp=zre*zre-zim*zim+CREAL;
			zim=2*zre*zim+CIMAGINARY;
		
	/* .....................................to here!!!*/	
		
			zre=temp;
			
			if (fabs(zre) > 10) break;
			if (fabs(zim) > 10) break;			
			if ((zre*zre+zim*zim)>100.0) break;	
			}
			
			if (((fabs(zre))<10)||((fabs(zim))<10))
			{
		if (fabs(zre)<10)
		colore=(int) floor(fabs(zre)*10) % 15 +1;
		else
		colore=(int) floor(fabs(zim)*10) % 15 +1;
			}
				else colore=BGRP;
		
			SetAPen(Rp,colore);
			WritePixel(Rp,k,j);
			
			n++;
		}
		m++;
		n=0;
	}
	return;
}

