# include "data.h"

void SetLimits (Vector *xvec, Vector *yvec, Graphdata *graphmode) {

  double maxX, minX, maxY, minY, range;
  int i;

  if (xvec != NULL) {
    maxX = minX = xvec[0].elements[0];
    for (i = 1; i < xvec[0].Nelements; i++) {
      if (!finite(xvec[0].elements[i])) continue;
      maxX = MAX (maxX, xvec[0].elements[i]);
      minX = MIN (minX, xvec[0].elements[i]);
    }
    range = maxX - minX;
    if (range == 0) range = 0.001 * maxX;
    if (range == 0) range = 0.001;
    graphmode[0].xmin = minX - 0.05*range;
    graphmode[0].xmax = maxX + 0.05*range;
  }

  if (yvec != NULL) {
    maxY = minY = yvec[0].elements[0];
    for (i = 1; i < yvec[0].Nelements; i++) {
      if (!finite(yvec[0].elements[i])) continue;
      maxY = MAX (maxY, yvec[0].elements[i]);
      minY = MIN (minY, yvec[0].elements[i]);
    }
    range = maxY - minY;
    if (range == 0) range = 0.0011 * maxY;
    if (range == 0) range = 0.0011;
    graphmode[0].ymin = minY - 0.05*range;
    graphmode[0].ymax = maxY + 0.05*range;
  }
  SetGraph (graphmode);

  set_variable ("XMIN", graphmode[0].xmin);
  set_variable ("XMAX", graphmode[0].xmax);
  set_variable ("YMIN", graphmode[0].ymin);
  set_variable ("YMAX", graphmode[0].ymax);
}

void SetLimitsRaw (float *xvec, float *yvec, int Nelements, Graphdata *graphmode) {

  double maxX, minX, maxY, minY, range;
  int i;

  if (xvec != NULL) {
    maxX = minX = xvec[0];
    for (i = 1; i < Nelements; i++) {
      if (!finite(xvec[i])) continue;
      maxX = MAX (maxX, xvec[i]);
      minX = MIN (minX, xvec[i]);
    }
    range = maxX - minX;
    if (range == 0) range = 0.001 * maxX;
    if (range == 0) range = 0.001;
    graphmode[0].xmin = minX - 0.05*range;
    graphmode[0].xmax = maxX + 0.05*range;
  }

  if (yvec != NULL) {
    maxY = minY = yvec[0];
    for (i = 1; i < Nelements; i++) {
      if (!finite(yvec[i])) continue;
      maxY = MAX (maxY, yvec[i]);
      minY = MIN (minY, yvec[i]);
    }
    range = maxY - minY;
    if (range == 0) range = 0.0011 * maxY;
    if (range == 0) range = 0.0011;
    graphmode[0].ymin = minY - 0.05*range;
    graphmode[0].ymax = maxY + 0.05*range;
  }
  SetGraph (graphmode);

  set_variable ("XMIN", graphmode[0].xmin);
  set_variable ("XMAX", graphmode[0].xmax);
  set_variable ("YMIN", graphmode[0].ymin);
  set_variable ("YMAX", graphmode[0].ymax);
}
