#include "tree.h"
#include "dist.h"
#include "f_est.h"
#include <stdlib.h>

#define min(a,b) (((a)<(b))?(a):(b))

double f_noedge(               /* Calculates G(r) without edge correction */
		coord *points, /* An array of positions */
		coord *events, /* An array of events (same length as points) */
		int n,               /* Number of points */
		double r)            /* Parameter of g */
{
  int i,j;
  double estimate=0;      /* Current estimate of k */
  double dist;       /* Euclidean distance between two points */
  double extra=0;    /* For common sub-expression elimination */

  if(points == NULL)
    return 0.0;

  extra=1.0/n;

  for(i=n; i>0; --i){
    for(j=n; j>0; --j){
      dist=edist(points+i-1,events+j-1);
      if(dist<=r){
	estimate+=extra;
	break;
      }
    }
  }

  return estimate;
}

double f_tor(              /* Calculates G(r) on a torus */
	     coord *points,  /* An array of positions */
	     coord *events,  /* An array of events (same length as points) */
	     int n,               /* Number of points */
	     double a,            /* Length of box in x-direction */
	     double b,            /* Length of box in y-direction */
	     double r)            /* Parameter of g */
{
  int i,j;
  double estimate=0;      /* Current estimate of k */
  double dist;       /* Euclidean distance between two points */
  double extra=0;    /* For common sub-expression elimination */

  if(points == NULL)
    return 0.0;

  extra=1.0/n;

  for(i=n; i>0; --i){
    for(j=n; j>0; --j){
      dist=tdist(points+i-1,events+j-1,a,b);
      if(dist<=r){
	estimate+=extra;
	break;
      }
    }
  }

  return estimate;
}
