                                                     Morrison.ls3



/* Listing 3. Points2Coefficients.c

   Input six points to determine the coefficients of an Affine

   transformation taking three points into three points.

   Solves two sets of three equations in three unknowns.  */


#include "stdio.h"

#include "exec/types.h"

#include "exec/tasks.h"

#include "exec/execbase.h"

#include "exec/memory.h"

#include "libraries/dos.h"

#include "libraries/dosextens.h"

#include "graphics/gfx.h"

#include "graphics/gfxmacros.h"

#include "graphics/gfxbase.h"

#include "graphics/view.h"

#include "intuition/intuition.h"

#include "intuition/intuitionbase.h"

#include "exec/exec.h"

#include "math.h"

static char header[]={"***  SIMULTANEOUS EQUATIONS 4/4/91  ***"};

struct FileHandle *Open();

struct IntuitionBase *IntuitionBase;

struct GfxBase *GfxBase;

double pt1x[4],pt1y[4],pt2x[4],pt2y[4];

double a, b, c, d, e, f ;

double h1,h2,h3,x1;

double a11,a12,a13,a21,a22,a23,a31,a32,a33;

double deta,deta11,deta12,deta13,deta21,deta22,deta23;

static int p11,p12, p21, p22;


main(argc, argv)

int argc;

char *argv[];

{

      LONG ofh, bfh;

      struct IntuiMessage *mess;

      int  i, j, nsides;

      ULONG class;

      USHORT code;

      SHORT  x, y;

      LONG   nb = 0;

      float dm = 999.9, xrat=1.0, yrat=-1.0;

      int dum = 999;

      int xoff = 0, yoff = 400;

      FILE *fopen(), *pp, *sp, *cp, *op;


      a=0;b=0;c=0;d=0;e=0;f=0;


 IntuitionBase=(struct IntuitionBase *)OpenLibrary("intuition.library",0);

      if(IntuitionBase == NULL)

      {  printf("Couldn't open Intuition library\n");

         goto finish; }


  GfxBase = (struct GfxBase *)OpenLibrary("graphics.library",0);

      if(GfxBase == NULL)

      {  printf("Couldn't open Graphics library\n");

         goto finish; }

   if ( argc < 3 )

   {  printf("CLI Usage: Need names of input points file and coefficient\n");

      printf("           output file on the command line. \n");

      goto finish; }


     op = fopen(argv[1],"r");

     if(op==0)

     {   printf("Couldn't open points input file \n");

         goto finish; }

     cp = fopen(argv[2],"a");


transformation:;

      for (i=0;  i < 3; i++)

      {

/* Read integer value points and convert to double for calculation.*/

          fscanf(op," %d %d  %d %d\n",&p11,&p12,&p21,&p22);

          pt1x[i]=(double)p11;

          pt1y[i]=(double)p12;

          pt2x[i]=(double)p21;

          pt2y[i]=(double)p22;

      }


      if(pt1x[0] > 998)

      {

/* Read in the scaling and offset values for output file. */

          fscanf(cp," %f  %d  %f  %d \n",&xrat,&xoff,&yrat,&yoff);

          goto finish;
      }

/* Use Cramer's rule to solve the equations:

               x2 = ax1 + by1 + e  and  y2 = cx1 + dy1 + f

   for the coefficients  a, b, c, d, e, and  f    */

          a11 = pt1x[0];

         a21 = pt1x[1];

         a31 = pt1x[2];

         a12 = pt1y[0];

         a22 = pt1y[1];

         a32 = pt1y[2];

         a13 = 1;

         a23 = 1;

         a33 = 1;

         h1 = pt2x[0];

         h2 = pt2x[1];

         h3 = pt2x[2];



   deta=a11*(a22*a33-a23*a32)-a12*(a21*a33-a23*a31)+a13*(a21*a32-a22*a31);

   if(deta==0)

   {  printf("determinent = 0\n");

      goto transformation;  }


   deta11=h1*(a22*a33-a23*a32)-h2*(a12*a33-a13*a32)+ h3*(a12*a23-a13*a22);

   deta12=a11*(h2*a33-a23*h3)-a21*(h1*a33-a13*h3) + a31*(h1*a23-a13*h2);

   deta13=a11*(a22*h3-h2*a32)-a21*(a12*h3-h1*a32) + a31*(a12*h2-h1*a22);

   a=deta11/deta;

   b=deta12/deta;

   e=deta13/deta;


         h1 = pt2y[0];

         h2 = pt2y[1];

         h3 = pt2y[2];

   deta21=h1*(a22*a33-a23*a32)-h2*(a12*a33-a13*a32)+ h3*(a12*a23-a13*a22);

   deta22=a11*(h2*a33-a23*h3)-a21*(h1*a33-a13*h3) + a31*(h1*a23-a13*h2);

   deta23=a11*(a22*h3-h2*a32)-a21*(a12*h3-h1*a32) + a31*(a12*h2-h1*a22);

   c=deta21/deta;

   d=deta22/deta;

   f=deta23/deta;


   fprintf(cp,"%.5f  %.5f  %.5f   %.5f   %.0f   %.0f\n\n",a,b,c,d,e,f);

   goto transformation;


finish:;

       if(op)  fclose(op);

       if(cp)

       {

  fprintf(cp,"%.5f  %.5f  %.5f   %.5f  %.0f  %.0f\n\n",dm,dm,dm,dm,dm,dm);

           fprintf(cp," %f  %d  %f  %d \n",xrat,xoff,yrat,yoff);

           fclose(cp);

       }

       if (IntuitionBase)   CloseLibrary(IntuitionBase);


}  /* end of main */

