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

Programma ...... Pyth.c

Versione ....... 1.1, 7/1989

Autore ......... Andreas Krebs, Mittelweg 9, 7110 Oehringen 2

Funzione ....... Dimostrazione costruzione albero di Pitagora

Software ....... Aztec C V3.6a || Lattice C V5.04

Hardware ....... Amiga 500/2500, Kick & Workbench V1.1/1.2/1.3

Compilazione ... (*Aztec*)      cc Pyth ;; ln pyht -lc -lm
             ... (*Lattice*)    lc -vbr -O -ff -Lntmv -w -mt Pyth

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

#include <intuition/intuition.h>
#include <graphics/gfx.h>
#include <graphics/gfxbase.h>
#include <graphics/gfxmacros.h>
#include <stdio.h>
#include <libraries/mathffp.h>

#ifdef LATTICE
#    include <stdlib.h>
#    include <proto/exec.h>
#    include <proto/graphics.h>
#    include <proto/intuition.h>
#    include <proto/dos.h>
#else
#    include <functions.h>
#endif

struct IntuitionBase *IntuitionBase;
struct GfxBase       *GfxBase;
struct MathBase      *MathBase;
struct MathTransBase *MathTransBase;

struct Screen    *PythScreen;
struct Window    *PythWindow;
struct RastPort  *rp;

USHORT Kords[21][3][2];
USHORT Box[4][2];

struct NewScreen NewPythScreen =
{
     0,0,640,400,3,1,0,HIRES|LACE,CUSTOMSCREEN,NULL,NULL,NULL,NULL
};

struct NewWindow NewPythWindow =
{
     0,10,640,390,1,0,CLOSEWINDOW,WINDOWCLOSE|ACTIVATE|BORDERLESS,
     NULL,NULL,(UBYTE *)"Pyth v1.1 ©7/89 by Andreas Krebs",
     NULL,NULL,0,0,0,0,CUSTOMSCREEN
};

void CloseALL()
{
     if(PythWindow)      CloseWindow(PythWindow);
     if(PythScreen)      CloseScreen(PythScreen);
     if(MathBase)        CloseLibrary(MathBase);
     if(MathTransBase)   CloseLibrary(MathTransBase);
     if(GfxBase)         CloseLibrary((struct Library *)GfxBase);
     if(IntuitionBase)   CloseLibrary(IntuitionBase);
}

void OpenALL()
{

  if(!(IntuitionBase=(struct IntuitionBase *)
       OpenLibrary("intuition.library",0L)))
  {
    puts("Non si apre Intuition!");
    exit(FALSE);
  }

  if(!(GfxBase=(struct GfxBase *)
       OpenLibrary("graphics.library",0L)))
  {
    puts("Non si apre Graphics!");
    CloseALL();
    exit(FALSE);
  }

  if(!(MathBase=(struct MathBase *)
       OpenLibrary("mathffp.library",0L)))
  {
    puts("Non si apre Mathffp!");
    CloseALL();
    exit(FALSE);
  }

  if(!(MathTransBase=(struct MathTransBase *)
       OpenLibrary("mathtrans.library",0L)))
  {
    puts("Non si apre MathTrans !");
    CloseALL();
    exit(FALSE);
  }

  if(GfxBase->DisplayFlags & (UWORD)PAL)
  {
    NewPythScreen.Height=512;
    NewPythWindow.Height=502;
  }

  if(!(PythScreen=(struct Screen *)
       OpenScreen(&NewPythScreen)))
  {
    puts("Non si apre lo schermo !");
    CloseALL();
    exit(FALSE);
  }

  NewPythWindow.Screen=PythScreen;

  if(!(PythWindow=(struct Window *)
       OpenWindow(&NewPythWindow)))
  {
    puts("Non si apre la finestra !");
    CloseALL();
    exit(FALSE);
  }

  rp = PythWindow -> RPort;
}

void DrawBox()
{
     Move(rp,(LONG)Box[3][0],(LONG)Box[3][1]);
  
     PolyDraw(rp,4L,&Box[0][0]);
}

void DrawDreieck(Nr)
USHORT Nr;
{
     SetAPen(rp,4L);

     Move(rp,(LONG)Kords[Nr][2][0],(LONG)Kords[Nr][2][1]);

     PolyDraw(rp,3L,&Kords[Nr][0][0]);
}

void DrawPyth()
{
  USHORT Seite[21],t;
  SHORT x=0,y=0,Nr=0,ax,ay;
  int sqr,sx,sy;
  FLOAT a,b,c,h,e,f,wia1,wia2,wib,abx,aby,abx2,aby2;

  for( t = 0 ; t < 21 ; t++ )
  {
    Seite[ t ]=0;
  }

  SetAPen( rp, 5L );

  if( GfxBase -> DisplayFlags & (UWORD)PAL ) /* Verifica PAL */
  {
    Move(rp,320L,500L); Draw(rp,320L,400L);
    Draw(rp,220L,400L); Draw(rp,220L,500L);
    Draw(rp,320L,500L);

    Kords[0][0][0]=320; Kords[0][0][1]=400;
    Kords[0][1][0]=256; Kords[0][1][1]=352;
    Kords[0][2][0]=220; Kords[0][2][1]=400;
  }
  else
  {
    Move(rp,310L,388L); Draw(rp,310L,308L);
    Draw(rp,230L,308L); Draw(rp,230L,388L);
    Draw(rp,310L,388L);

    Kords[0][0][0]=310; Kords[0][0][1]=308;
    Kords[0][1][0]=259; Kords[0][1][1]=270;
    Kords[0][2][0]=230; Kords[0][2][1]=308;
  }

  DrawDreieck(0);

  do
  {
    Nr++;

    x=Kords[Nr-1][0][0] - Kords[Nr-1][1][0];
    y=Kords[Nr-1][0][1] - Kords[Nr-1][1][1];
    sqr=x*x+y*y;
    a=SPSqrt((FLOAT)(sqr));

    if(x==0.0 || x==-0.0)
      wia1=PI/2;
    else
      wia1=SPAtan((FLOAT)y/(FLOAT)x);

    if(x<=0.0 || x<=-0.0)
      wia1+=PI;

    x = Kords[Nr-1][1][0] - Kords[Nr-1][2][0];
    y = Kords[Nr-1][1][1] - Kords[Nr-1][2][1];
    sqr = x * x + y * y;
    b = SPSqrt((FLOAT)(sqr));

    if( x==0.0 || x==-0.0 )
      wia2=PI/2.0;
    else
      wia2=-SPAtan((FLOAT)y/(FLOAT)x);

    if( x<=0.0 || x<=-0.0 )
      wia2 += PI;

    x=Kords[Nr-1][2][0]-Kords[Nr-1][0][0];
    y=Kords[Nr-1][2][1]-Kords[Nr-1][0][1];
    sqr=x*x+y*y;
    c=SPSqrt((FLOAT)(sqr));

    h=a*0.6;   /*   a*sin(36.9°)   */
    e=a*0.8;   /*   a*sin(53.1°)   */
    f=c-e;

    if(Seite[Nr]==1)
    {
      abx=-b*SPSin(wia2);
      aby=b*SPCos(wia2);
    }
    else
    {
      abx=a*SPSin(wia1);
      aby=a*SPCos(wia1);
    }

    Box[0][0]=Kords[Nr-1][Seite[Nr]][0];
    Box[0][1]=Kords[Nr-1][Seite[Nr]][1];
    Box[1][0]=Kords[Nr-1][Seite[Nr]][0]+(SHORT)abx;
    Box[1][1]=Kords[Nr-1][Seite[Nr]][1]-(SHORT)aby;
    Box[2][0]=Kords[Nr-1][Seite[Nr]+1][0]+(SHORT)abx;
    Box[2][1]=Kords[Nr-1][Seite[Nr]+1][1]-(SHORT)aby;
    Box[3][0]=Kords[Nr-1][Seite[Nr]+1][0];
    Box[3][1]=Kords[Nr-1][Seite[Nr]+1][1];

    if(Seite[Nr]==1)
      SetAPen(rp,3L);
    else
      SetAPen(rp,5L);

    DrawBox();

    Kords[Nr][0][0]=Box[1][0];
    Kords[Nr][0][1]=Box[1][1];
    Kords[Nr][2][0]=Box[2][0];
    Kords[Nr][2][1]=Box[2][1];

    sx=(int)Box[Seite[Nr]+1][0];
    sy=(int)Box[Seite[Nr]+1][1];

    if(Seite[Nr]==1)
    {
      wib=wia2+0.927295;
      wib=PI-wib;

      aby2=f*SPSin(wib);
      abx2=f*SPCos(wib);
    }
    else
    {
      wib=wia1+0.6435011;

      aby2=e*SPSin(wib);
      abx2=e*SPCos(wib);
    }

    ax=(SHORT)abx2;
    ay=(SHORT)aby2;

    sx-=ax;
    sy-=ay;

    Kords[Nr][1][0]=(USHORT)sx;
    Kords[Nr][1][1]=(USHORT)sy;

    DrawDreieck(Nr);

    if((b<=3.0 && a<=4.0 && c<=5.0) || Nr>=20)
    {
      for(t=(Nr+1);t<21;t++)
      {
        Seite[t]=0;
      }

      while(Seite[Nr]==1)
      {
        Seite[Nr]=0;
        Nr--;
        if(Nr<1)
          break;
      }
      Seite[Nr]=1;
      Nr--;
    }

    if(GetMsg(PythWindow->UserPort))
    {
      CloseALL();
      exit(TRUE);
    }
  }while(Nr>-1);
}

void main()
{
     OpenALL();

     SetRGB4( &PythScreen->ViewPort, 3L, 15L,  0L,  0L);
     SetRGB4( &PythScreen->ViewPort, 4L,  0L, 15L, 15L);
     SetRGB4( &PythScreen->ViewPort, 5L,  0L, 15L,  0L);

     DrawPyth();

     Wait( 1L << PythWindow -> UserPort -> mp_SigBit );

     CloseALL();
}