/* example use of pmap_poly to infer projective mapping from a quadrilateral */
#define VECTREX 1
#include <stdio.h>
#include <stdlib.h>

#include "poly.h"
#include "mx3.h"

#define SIZE 50

int main(int argc, char **argv)
{
    static poly p = {
        4,
        {
        /* u  v  sx  sy */
#ifndef VECTREX
          {0, 0,  0,  0}, /* vert[0] */ /* From bottom left going anti-clockwise */
          {1, 0, 40,  0}, /* vert[1] */
          {1, 1, 30, 30}, /* vert[2] */
          {0, 1, 10, 20}  /* vert[3] */
#else
          {-18000,-24000,  -17050,-24560},
          { 18000,-24000,   19000,-23750},
          { 18000, 24000,   18800, 24007},
          {-18000, 24000,  -18101, 23976}
#endif
        }
    };
    double ST[3][3];
    double scr[3], tex[3];
    int x, y;

    /* compute screen to texture transform ST */
    pmap_poly(&p, ST);

    /* scan a square area of screen space, transforming to texture space */
#ifndef VECTREX
    for (y=0; y<SIZE; y++)
      for (x=0; x<SIZE; x++) {
        scr[0] = x;
        scr[1] = y;
        scr[2] = 1.;
        mx3d_transform(scr, ST, tex);
        tex[0] /= tex[2];
        tex[1] /= tex[2];
        printf("scr(%d,%d) transforms to tex(%g,%g)\n",
               x, y, tex[0], tex[1]);
      }
#else
    for (y=-24000; y<=24000; y+=24000/2)
        for (x=-18000; x<=18000; x+=18000/2) {
            scr[0] = x;
            scr[1] = y;
            scr[2] = 1.;
            mx3d_transform(scr, ST, tex);
            tex[0] /= tex[2];
            tex[1] /= tex[2];
            printf("scr(%d,%d) transforms to tex(%g,%g)\n",
                x, y, tex[0], tex[1]);
        }
#endif
    exit(0);
    return 0;
}
