/**************************************************************************/
/* graf.c                                                      20.01.88   */
/* Version 1.0                                                            */
/*                                                                        */
/*                                                                        */
/* x=y-sign(x)*sqrt(abs(b*x-c))                                           */
/* y=a-x                                                                  */
/*                                                                        */
/* fuer Markt+Technik geschrieben von Heiko Knappe                        */
/**************************************************************************/


#include <exec/types.h>
#include <math.h>
#include <intuition/intuition.h>

#define WIDTH  320         /* Screen Modus                     */
#define HEIGHT 250
#define DEPTH  5
#define FLAGS  0

#define XOFF   160
#define YOFF   125

extern save();


struct IntuitionBase *IntuitionBase;
struct GfxBase *GfxBase;
struct Screen *MyScreen;
struct RastPort *rp;
struct NewScreen MyNewScreen=
 {0,0,WIDTH,HEIGHT,DEPTH,1,0,FLAGS,CUSTOMSCREEN,0,0,0,0};

float i=0,r=0,ii,rr;     /* relative Punktkoordinaten */
float a=.4,b=1,c=0;      /* Parameter */
float str,sti,rrange=2.5,irange=2.5;
short x,y;
int   n;


/***********************/
/* Hauptprogramm       */
/***********************/

main(argc,argv)
int argc;
char *argv[];
{
 open_things();
 zeichnen();
 while(MyScreen->TopEdge<100);
 /* falls das Bild gespeichert werden soll */
 save("pic",1,MyScreen);
 close_things();
}


/***********************/
/* Initialisierungs-   */
/* routinen            */
/***********************/

open_things()
{
 if((IntuitionBase=OpenLibrary("intuition.library",0))==0)
    close_things();
 if((GfxBase=OpenLibrary("graphics.library",0))==0)
    close_things();
 if((MyScreen=OpenScreen(&MyNewScreen))==0)
    close_things();
 rp=&(MyScreen->RastPort);
 init_colors();
} 

close_things()
{
 if(IntuitionBase!=0) CloseLibrary(IntuitionBase);
 if(GfxBase!=0) CloseLibrary(GfxBase);
 if(MyScreen!=0) CloseScreen(MyScreen);
 exit(0);
} 

init_colors()
{
 int i;

 for(i=0;i<16;++i)
    SetRGB4(&(MyScreen->ViewPort),i,i,0,0);
 for(i=16;i<32;++i)
    SetRGB4(&(MyScreen->ViewPort),i,31-i,0,31-i);
}


/***********************/
/* Grafik zeichnen     */
/***********************/

zeichnen()
{
 str=(float)(WIDTH-XOFF)/rrange,sti=(float)(HEIGHT-YOFF)/irange;
 while(MyScreen->TopEdge<100)
 {
    x=XOFF+(short)(r*str),y=YOFF-(short)(i*sti);
    while((x<0)||(x>WIDTH)||(y<0)||(y>HEIGHT))
    {
       if(x<0) x=-x;
       if(x>WIDTH) x=2*WIDTH-x;
       if(y<0) y=-y;
       if(y>HEIGHT) y=2*HEIGHT-y;
    }
    SetAPen(rp,(ReadPixel(rp,x,y)+1)%32);
    WritePixel(rp,x,y);
    rr=(b*r-c); if(rr<0) rr=-rr;
    rr=sqrt(rr); if(r<0) rr=-rr;
    rr=i-rr;
    ii=a-r;
    r=rr,i=ii;
 } 
}
