#include "amut.h"
#include "amgr.h"
#include "ammarep.h"

typedef struct gauss_info
{
  dyv *mu;
  dym *cov_inv;
  double cov_determinant;
} gauss_info;

double gauss_eval(dyv *x,dyv *mu,dym *cov_inv,double covdet)
{
  dyv *x_minus_mu = mk_dyv_subtract(x,mu);
  double quad_form = dym_xt_a_x_value(x_minus_mu,cov_inv);
  double multiplicand = 1 / sqrt(2 * PI * covdet);
  double result = multiplicand * exp(-quad_form / 2);
  free_dyv(x_minus_mu);
  return result;
}  
  
double gauss_height_fn(char *data,double x,double z)
{
  gauss_info *gi = (gauss_info *) data;
  dyv *xdyv = mk_dyv_2(x,z);
  double result = gauss_eval(xdyv,gi->mu,gi->cov_inv,gi->cov_determinant);
  free_dyv(xdyv);
  return result;
}

void screen_main(int argc,char *argv[])
{
  int i;
  printf("Will do ag_window_shape(384,384);\n");
  wait_for_key();
  ag_window_shape(384,384);

  printf("Will do ag_on(test.ps);\n");
  wait_for_key();
  ag_on("test.ps");

  printf("Will do ag_line(10.0,10.0,256.0,502.0);\n");
  wait_for_key();
  ag_line(10.0,10.0,256.0,502.0);

  printf("Will do ag_off();\n");
  wait_for_key();
  ag_off();

  printf("Will do ag_on("");\n");
  wait_for_key();
  ag_on("");

  printf("Will do ag_line(10.0,10.0,502.0,256.0);\n");
  wait_for_key();
  ag_line(10.0,10.0,502.0,256.0);

  printf("Will do ag_screen_shape(600.0,600.0);\n");
  wait_for_key();
  ag_window_shape(600.0,600.0);

  printf("Will do ag_line(10.0,10.0,502.0,256.0);\n");
  wait_for_key();
  ag_line(10.0,10.0,502.0,256.0);

  printf("Will do ag_off();\n");
  wait_for_key();
  ag_off();

  printf("Will do ag_on(test2.ps);\n");
  wait_for_key();
  ag_on("test2.ps");

  printf("Will do ag_line(10.0,10.0,256.0,502.0);\n");
  wait_for_key();
  ag_line(10.0,10.0,256.0,502.0);

  for ( i = 2 ; i < 25 ; i++ )
  {
    if ( i == 12 ) ag_set_pen_color(AG_RED);
    ag_pixel((double)i,(double)(i*i));
  }

  printf("Will do ag_off();\n");
  wait_for_key();
  ag_off();

  printf("Done\n");
  wait_for_key();
}

void test_pats(void)
{
  ps_usepats();
  ag_on("pattest.ps");
  ag_set_pen_color(AG_BLACK);
  ag_disc(100,100,50);
  ag_set_pen_color(AG_WHITE);
  ag_disc(100,100,40);
  ag_set_pen_color(AG_RED);
  ag_disc(100,100,30);
  ag_set_pen_color(AG_WHITE);
  ag_disc(100,100,20);
  ag_set_pen_color(AG_BLACK);
  ag_disc(100,100,10);
  ag_rectangle(50,50,150,150);
  printf("This bullseye should show up in pattest.ps, with the red part\n");
  printf("turned into a pattern instead of a color\n");
  wait_for_key();
  ag_off();
}

int main(int argc,char *argv[])
{
  void screen_main(int argc,char *argv[]);
  ag_on_robust("");
  ag_set_pen_color(AG_ORANGE);
  ag_box(100.0,100.0,200.0,200.0);
  wait_for_key();
  test_pats();
  screen_main(argc,argv);
  return 0;
}
