# include "Ximage.h"

# define DrawLine(BUF,X1,Y1,X2,Y2) (bDrawLine (BUF, (X1), (Y1), (X2), (Y2)))
# define DrawRectangle(BUF,X,Y,dX,dY) (bDrawRectOpen (BUF, (X-0.5*dX), (Y-0.5*dY), (X+0.5*dX), (Y+0.5*dY)))
# define FillRectangle(BUF,X,Y,dX,dY) (bDrawRectFill (BUF, (X-0.5*dX), (Y-0.5*dY), (X+0.5*dX+1), (Y+0.5*dY+1)))
# define DrawCircle(BUF,X1,Y1,R) (bDrawCircle (BUF, (X1), (Y1), (R)))
# define FillCircle(BUF,X1,Y1,R) (bDrawCircleFill (BUF, (X1), (Y1), (R)))
# define FillTriangle(BUF,X1,Y1,dX,dY) (bDrawTriFill (BUF, (X1), (Y1), (dX), (dY)))
# define OpenTriangle(BUF,X1,Y1,X2,Y2,X3,Y3) (bDrawTriOpen (BUF, (X1), (Y1), (X2), (Y2), (X3), (Y3)))

# define CONNECT 0
# define HISTOGRAM 1
# define POINTS 2

static Graphic *graphic;

int bDrawObjects (bDrawBuffer *buffer, KapaGraphWidget *graph) {
  
  int i;
  int type;
  int weight;
  bDrawColor color;
  bDrawColor black;
  
  graphic = GetGraphic();

  black = KapaColorByName ("black");
  for (i = 0; i < graph[0].Nobjects; i++) {

    weight = MAX (0, MIN (10, graph[0].objects[i].lweight));
    type = graph[0].objects[i].ltype;    
    color = graph[0].objects[i].color;
    bDrawSetStyle (buffer, color, weight, type);

    switch (graph[0].objects[i].style) {
      case CONNECT: 
	bDrawConnect (buffer, graph, &graph[0].objects[i]);
	break;
      case HISTOGRAM:
	bDrawHistogram (buffer, graph, &graph[0].objects[i]);
	break;
      case POINTS:
	bDrawPoints (buffer, graph, &graph[0].objects[i]);
	break;
    }

    if (graph[0].objects[i].etype & 0x01) {
      bDrawYErrors (buffer, graph, &graph[0].objects[i]);
    }
    if (graph[0].objects[i].etype & 0x02) {
      bDrawXErrors (buffer, graph, &graph[0].objects[i]);
    }
  }
  bDrawSetStyle (buffer, black, 0, 0);
  return (TRUE);
}

void bDrawConnect (bDrawBuffer *buffer, KapaGraphWidget *graph, Gobjects *object) {
  
  int i;
  float *x, *y;
  double mxi, mxj, myi, myj, bxi, bxj, byi, byj, bx, by;
  double sx0, sy0, sx1, sy1;
  double X0, X1, Y0, Y1;

  mxi = graph[0].axis[0].dfx / (object[0].x1 - object[0].x0);
  mxj = graph[0].axis[1].dfx / (object[0].y1 - object[0].y0);
  myi = graph[0].axis[0].dfy / (object[0].x1 - object[0].x0);
  myj = graph[0].axis[1].dfy / (object[0].y1 - object[0].y0); 
  
  bxi  =  graph[0].axis[0].fx - object[0].x0*graph[0].axis[0].dfx/(object[0].x1 - object[0].x0);
  bxj  =  -object[0].y0*graph[0].axis[1].dfx/(object[0].y1 - object[0].y0);
  byi  =  -object[0].x0*graph[0].axis[0].dfy/(object[0].x1 - object[0].x0);
  byj  =  graph[0].axis[1].fy - object[0].y0*graph[0].axis[1].dfy/(object[0].y1 - object[0].y0); 
  
  bx = bxi + bxj;
  by = byi + byj;
  
  X0 = graph[0].axis[0].fx;
  X1 = graph[0].axis[0].fx + graph[0].axis[0].dfx;
  Y0 = graph[0].axis[1].fy;
  Y1 = graph[0].axis[1].fy + graph[0].axis[1].dfy;

  x = object[0].x; y = object[0].y;
  for (i = 0; (i < object[0].Npts) && !(finite(x[i]) && finite(y[i])); i++);
  if (i >= object[0].Npts) return;
  sx0 = x[i]*mxi + y[i]*mxj + bx;
  sy0 = x[i]*myi + y[i]*myj + by;
  
  for (i++; i < object[0].Npts; i++) {
    if (!(finite(x[i]) && finite(y[i]))) continue;
    sx1 = x[i]*mxi + y[i]*mxj + bx;
    sy1 = x[i]*myi + y[i]*myj + by;
    bDrawClipLine (buffer, sx0, sy0, sx1, sy1, X0, Y0, X1, Y1);
    sx0 = sx1; sy0 = sy1;
  }
}

void bDrawClipLine (bDrawBuffer *buffer, double x0, double y0, double x1, double y1, double X0, double Y1, double X1, double Y0) {

  /* skip line segement if both points are beyond box */
  if ((x0 <= X0) && (x1 <= X0)) return;
  if ((x0 >= X1) && (x1 >= X1)) return;
  if ((y0 <= Y0) && (y1 <= Y0)) return;
  if ((y0 >= Y1) && (y1 >= Y1)) return;

  /* replace x0,y0 if outside box */
  if ((x0 < X0) && (x1 >= X0)) {
    y0 = y0 + (X0 - x0)*(y1 - y0)/(x1 - x0);
    x0 = X0;
  }
  if ((x0 > X1) && (x1 <= X1)) {
    y0 = y0 + (X1 - x0)*(y1 - y0)/(x1 - x0);
    x0 = X1;
  }
  if ((y0 < Y0) && (y1 >= Y0)) {
    x0 = x0 + (Y0 - y0)*(x1 - x0)/(y1 - y0);
    y0 = Y0;
  }
  if ((y0 > Y1) && (y1 <= Y1)) {
    x0 = x0 + (Y1 - y0)*(x1 - x0)/(y1 - y0);
    y0 = Y1;
  }

  /* skip line segement if both points are beyond box */
  if ((x0 <= X0) && (x1 <= X0)) return;
  if ((x0 >= X1) && (x1 >= X1)) return;
  if ((y0 <= Y0) && (y1 <= Y0)) return;
  if ((y0 >= Y1) && (y1 >= Y1)) return;

  /* replace x1,y1 if outside box */
  if ((x1 < X0) && (x0 >= X0)) {
    y1 = y0 + (X0 - x0)*(y1 - y0)/(x1 - x0);
    x1 = X0;
  }
  if ((x1 > X1) && (x0 <= X1)) {
    y1 = y0 + (X1 - x0)*(y1 - y0)/(x1 - x0);
    x1 = X1;
  }
  if ((y1 < Y0) && (y0 >= Y0)) {
    x1 = x0 + (Y0 - y0)*(x1 - x0)/(y1 - y0);
    y1 = Y0;
  }
  if ((y1 > Y1) && (y0 <= Y1)) {
    x1 = x0 + (Y1 - y0)*(x1 - x0)/(y1 - y0);
    y1 = Y1;
  }
  DrawLine (buffer, x0, y0, x1, y1);
}

/*******/
void bDrawHistogram (bDrawBuffer *buffer, KapaGraphWidget *graph, Gobjects *object) {

  int i;
  float *x, *y;
  double mxi, mxj, myi, myj, bxi, bxj, byi, byj, bx, by;
  double sx0, sy0, sx1, sy1, sxa, sya, sxo, syo;
  double X0, X1, Y0, Y1;

  mxi = graph[0].axis[0].dfx / (object[0].x1 - object[0].x0);
  mxj = graph[0].axis[1].dfx / (object[0].y1 - object[0].y0);
  myi = graph[0].axis[0].dfy / (object[0].x1 - object[0].x0);
  myj = graph[0].axis[1].dfy / (object[0].y1 - object[0].y0); 
  
  bxi  =  graph[0].axis[0].fx - object[0].x0*graph[0].axis[0].dfx/(object[0].x1 - object[0].x0);
  bxj  =  -object[0].y0*graph[0].axis[1].dfx/(object[0].y1 - object[0].y0);
  byi  =  -object[0].x0*graph[0].axis[0].dfy/(object[0].x1 - object[0].x0);
  byj  =  graph[0].axis[1].fy - object[0].y0*graph[0].axis[1].dfy/(object[0].y1 - object[0].y0); 
  
  bx = bxi + bxj;
  by = byi + byj;
  
  X0 = graph[0].axis[0].fx;
  X1 = graph[0].axis[0].fx + graph[0].axis[0].dfx;
  Y0 = graph[0].axis[1].fy;
  Y1 = graph[0].axis[1].fy + graph[0].axis[1].dfy;

  /* find the first valid datapoint */
  x = object[0].x; y = object[0].y;
  for (i = 0; (i < object[0].Npts) && !(finite(x[i]) && finite(y[i])); i++);
  if (i >= object[0].Npts) return;

  /* first valid data point */
  sx0 = x[i]*mxi + y[i]*mxj + bx;
  sy0 = x[i]*myi + y[i]*myj + by;
  sx0 = MIN (MAX (sx0, X0), X1);
  sy0 = MAX (MIN (sy0, Y0), Y1);
  
  /* find the second valid datapoint */
  for (i++; (i < object[0].Npts) && !(finite(x[i]) && finite(y[i])); i++);
  if (i >= object[0].Npts) return;

  /* second valid data point */
  sx1 = x[i]*mxi + y[i]*mxj + bx;
  sy1 = x[i]*myi + y[i]*myj + by;
  sx1 = MIN (MAX (sx1, X0), X1);
  sy1 = MAX (MIN (sy1, Y0), Y1);
  
  /* connect first point to second point */
  sxa = MIN (MAX (sx0 - 0.5*(sx1 - sx0), X0), X1);
  sya = MAX (sy0, Y0);
  DrawLine (buffer, sx0, sy0, sxa, sy0);
  DrawLine (buffer, sxa, sy0, sxa, sya);
  
  /* draw segment equal distance behind first point and down to x-axis */
  sxa = MIN (MAX (0.5*(sx0 + sx1), X0), X1);
  DrawLine (buffer, sx0, sy0, sxa, sy0);
  DrawLine (buffer, sxa, sy0, sxa, sy1);
  DrawLine (buffer, sxa, sy1, sx1, sy1);
  sx0 = sx1;
  sy0 = sy1;
  
  /* continue with rest of points */
  sxo = syo = 0;
  for (i++; i < object[0].Npts; i++) {
    if (!(finite(x[i]) && finite(y[i]))) continue;
    sx1 = x[i]*mxi + y[i]*mxj + bx;
    sy1 = x[i]*myi + y[i]*myj + by;
    sx1 = MIN (MAX (sx1, X0), X1);
    sy1 = MAX (MIN (sy1, Y0), Y1);
    sxa = MIN (MAX (0.5*(sx0 + sx1), X0), X1);
    DrawLine (buffer, sx0, sy0, sxa, sy0);
    DrawLine (buffer, sxa, sy0, sxa, sy1);
    DrawLine (buffer, sxa, sy1, sx1, sy1);
    sxo = sx0; syo = sy0;
    sx0 = sx1; sy0 = sy1;
  }
  
  /* draw segment equal distance after last point and down to x-axis */
  sxa = MIN (MAX (sx1 + 0.5*(sx1 - sxo), X0), X1);
  sya = MAX (sy1, Y0);
  DrawLine (buffer, sx1, sy1, sxa, sy1);
  DrawLine (buffer, sxa, sy1, sxa, sya);

# if (0)
  sx0 = x[i]*mxi + y[i]*mxj + bx;
  sy0 = x[i]*myi + y[i]*myj + by;
  sx0 = MIN (MAX (sx0, X0), X1);
  sy0 = MAX (MIN (sy0, Y0), Y1);
  
  /* continue with rest of points */
  for (i++; i < object[0].Npts; i++) {
    if (!(finite(x[i]) && finite(y[i]))) continue;
    sx1 = x[i]*mxi + y[i]*mxj + bx;
    sy1 = x[i]*myi + y[i]*myj + by;
    sx1 = MIN (MAX (sx1, X0), X1);
    sy1 = MAX (MIN (sy1, Y0), Y1);
    sxa = 0.5*(sx0 + sx1);
    DrawLine (buffer, sx0, sy0, sxa, sy0);
    DrawLine (buffer, sxa, sy0, sxa, sy1);
    DrawLine (buffer, sxa, sy1, sx1, sy1);
    sx0 = sx1; sy0 = sy1;
  }
# endif
}

/*******/
void bDrawPoints (bDrawBuffer *buffer, KapaGraphWidget *graph, Gobjects *object) {
 
  int i;
  float *x, *y, *z;
  double mxi, mxj, myi, myj, bxi, bxj, byi, byj, bx, by;
  int sx, sy, sx1, sy1, sx2, sy2, ds, dz, D;
  unsigned char *pixel1, *pixel2, *pixel3;

  mxi = graph[0].axis[0].dfx / (object[0].x1 - object[0].x0);
  mxj = graph[0].axis[1].dfx / (object[0].y1 - object[0].y0);
  myi = graph[0].axis[0].dfy / (object[0].x1 - object[0].x0);
  myj = graph[0].axis[1].dfy / (object[0].y1 - object[0].y0);  
 
  bxi  =  graph[0].axis[0].fx - object[0].x0*graph[0].axis[0].dfx/(object[0].x1 - object[0].x0);
  bxj  =  -object[0].y0*graph[0].axis[1].dfx/(object[0].y1 - object[0].y0);
  byi  =  -object[0].x0*graph[0].axis[0].dfy/(object[0].x1 - object[0].x0);
  byj  =  graph[0].axis[1].fy - object[0].y0*graph[0].axis[1].dfy/(object[0].y1 - object[0].y0);  

  bx = bxi + bxj;
  by = byi + byj;
  
  Graphic *graphic = GetGraphic();

  ALLOCATE (pixel1, unsigned char, graphic[0].Npixels);
  ALLOCATE (pixel2, unsigned char, graphic[0].Npixels);
  ALLOCATE (pixel3, unsigned char, graphic[0].Npixels);

  /** cmap[i].pixel must be defined even if X is not used **/
  for (i = 0; i < graphic[0].Npixels; i++) { /* set up pixel array */
    pixel1[i] = graphic[0].cmap[i].red >> 8;
    pixel2[i] = graphic[0].cmap[i].green >> 8;
    pixel3[i] = graphic[0].cmap[i].blue >> 8;
  }

  /**** points are scaled by object.z ***/
  int scaleSize = (object[0].size < 0);
  int scaleColor = (object[0].color < 0);

  ds = 0.5 * (graphic->dx + graphic->dy) * 0.003 * object[0].size;
  dz = 0.5 * (graphic->dx + graphic->dy) * 0.010;
  x = object[0].x; y = object[0].y; z = object[0].z;

  if (object[0].ptype == 0) {	/* filled box */
    for (i = 0; i < object[0].Npts; i++) {
      if (!(finite(x[i]) && finite(y[i]))) continue;
      sx = x[i]*mxi + y[i]*mxj + bx;
      sy = x[i]*myi + y[i]*myj + by;
      if ((sx > graph[0].axis[0].fx) && (sx < graph[0].axis[0].fx + graph[0].axis[0].dfx) &&
	  (sy < graph[0].axis[1].fy) && (sy > graph[0].axis[1].fy + graph[0].axis[1].dfy))
      {
	if (scaleColor) {
	  if (!finite(z[i])) continue;
	  int pixel = MIN (graphic->Npixels - 2, MAX (0, z[i]*(graphic->Npixels - 1)));
	  buffer->bColor_R = pixel1[pixel];
	  buffer->bColor_G = pixel2[pixel];
	  buffer->bColor_B = pixel3[pixel];
	}
	D = scaleSize ? dz*z[i] : ds;
	FillRectangle (buffer, sx, sy, 2*D, 2*D);
	// plot range saturated by bDrawRectFill
      }
    }
  }
  if (object[0].ptype == 1) {	/* open box */
    for (i = 0; i < object[0].Npts; i++) {
      if (!(finite(x[i]) && finite(y[i]))) continue;
      sx = x[i]*mxi + y[i]*mxj + bx;
      sy = x[i]*myi + y[i]*myj + by;
      if ((sx > graph[0].axis[0].fx) && (sx < graph[0].axis[0].fx + graph[0].axis[0].dfx) &&
	  (sy < graph[0].axis[1].fy) && (sy > graph[0].axis[1].fy + graph[0].axis[1].dfy))
      {
	if (scaleColor) {
	  if (!finite(z[i])) continue;
	  int pixel = MIN (graphic->Npixels - 2, MAX (0, z[i]*(graphic->Npixels - 1)));
	  buffer->bColor_R = pixel1[pixel];
	  buffer->bColor_G = pixel2[pixel];
	  buffer->bColor_B = pixel3[pixel];
	}
	D = scaleSize ? dz*z[i] : ds;
	DrawRectangle (buffer, sx, sy, 2*D, 2*D);
	// plot range saturated by bDrawRectOpen
      }
    }
  }
  if (object[0].ptype == 2) { /* cross */
    for (i = 0; i < object[0].Npts; i++) {
      if (!(finite(x[i]) && finite(y[i]))) continue;
      sx = x[i]*mxi + y[i]*mxj + bx;
      sy = x[i]*myi + y[i]*myj + by;
      if ((sx > graph[0].axis[0].fx) && (sx < graph[0].axis[0].fx + graph[0].axis[0].dfx) &&
	  (sy < graph[0].axis[1].fy) && (sy > graph[0].axis[1].fy + graph[0].axis[1].dfy))
      {
	if (scaleColor) {
	  if (!finite(z[i])) continue;
	  int pixel = MIN (graphic->Npixels - 2, MAX (0, z[i]*(graphic->Npixels - 1)));
	  buffer->bColor_R = pixel1[pixel];
	  buffer->bColor_G = pixel2[pixel];
	  buffer->bColor_B = pixel3[pixel];
	}
	D = scaleSize ? dz*z[i] : ds;
	DrawLine (buffer, sx - D, sy, sx + D, sy);
	DrawLine (buffer, sx, sy - D, sx, sy + D);
	// out-of-range points skipped by bDrawPoint
      }
    }
  }
  if (object[0].ptype == 3) {	/* x */
    for (i = 0; i < object[0].Npts; i++) {
      if (!(finite(x[i]) && finite(y[i]))) continue;
      sx = x[i]*mxi + y[i]*mxj + bx;
      sy = x[i]*myi + y[i]*myj + by;
      if ((sx > graph[0].axis[0].fx) && (sx < graph[0].axis[0].fx + graph[0].axis[0].dfx) &&
	  (sy < graph[0].axis[1].fy) && (sy > graph[0].axis[1].fy + graph[0].axis[1].dfy))
      {
	if (scaleColor) {
	  if (!finite(z[i])) continue;
	  int pixel = MIN (graphic->Npixels - 2, MAX (0, z[i]*(graphic->Npixels - 1)));
	  buffer->bColor_R = pixel1[pixel];
	  buffer->bColor_G = pixel2[pixel];
	  buffer->bColor_B = pixel3[pixel];
	}
	D = scaleSize ? dz*z[i] : ds;
	DrawLine (buffer, sx + D, sy - D, sx - D, sy + D);
	DrawLine (buffer, sx - D, sy - D, sx + D, sy + D);
	// out-of-range points skipped by bDrawPoint
      }
    }
  }
  if (object[0].ptype == 4) {	/* filled triangle */
    for (i = 0; i < object[0].Npts; i++) {
      if (!(finite(x[i]) && finite(y[i]))) continue;
      sx = x[i]*mxi + y[i]*mxj + bx;
      sy = x[i]*myi + y[i]*myj + by;
      if ((sx > graph[0].axis[0].fx) && (sx < graph[0].axis[0].fx + graph[0].axis[0].dfx) &&
	  (sy < graph[0].axis[1].fy) && (sy > graph[0].axis[1].fy + graph[0].axis[1].dfy))
      {
	if (scaleColor) {
	  if (!finite(z[i])) continue;
	  int pixel = MIN (graphic->Npixels - 2, MAX (0, z[i]*(graphic->Npixels - 1)));
	  buffer->bColor_R = pixel1[pixel];
	  buffer->bColor_G = pixel2[pixel];
	  buffer->bColor_B = pixel3[pixel];
	}
	D = scaleSize ? dz*z[i] : ds;
	// FillTriangle (buffer, sx - D, sy - 0.58*D, sx + D, sy - 0.58*D, sx, sy + 1.15*D);
	FillTriangle (buffer, sx, sy + 0.58*D, D, -1.73*D);
	// out-of-range points skipped by bDrawPoint
      }
    }
  }
  if (object[0].ptype == 14) {	/* filled triangle */
    for (i = 0; i < object[0].Npts; i++) {
      if (!(finite(x[i]) && finite(y[i]))) continue;
      sx = x[i]*mxi + y[i]*mxj + bx;
      sy = x[i]*myi + y[i]*myj + by;
      if ((sx > graph[0].axis[0].fx) && (sx < graph[0].axis[0].fx + graph[0].axis[0].dfx) &&
	  (sy < graph[0].axis[1].fy) && (sy > graph[0].axis[1].fy + graph[0].axis[1].dfy))
      {
	if (scaleColor) {
	  if (!finite(z[i])) continue;
	  int pixel = MIN (graphic->Npixels - 2, MAX (0, z[i]*(graphic->Npixels - 1)));
	  buffer->bColor_R = pixel1[pixel];
	  buffer->bColor_G = pixel2[pixel];
	  buffer->bColor_B = pixel3[pixel];
	}
	D = scaleSize ? dz*z[i] : ds;
	// FillTriangle (buffer, sx - D, sy - 0.58*D, sx + D, sy - 0.58*D, sx, sy + 1.15*D);
	FillTriangle (buffer, sx, sy - 0.58*D, D, +1.73*D);
	// out-of-range points skipped by bDrawPoint
      }
    }
  }
  if (object[0].ptype == 5) {	/* open triangle */
    for (i = 0; i < object[0].Npts; i++) {
      if (!(finite(x[i]) && finite(y[i]))) continue;
      sx = x[i]*mxi + y[i]*mxj + bx;
      sy = x[i]*myi + y[i]*myj + by;
      if ((sx > graph[0].axis[0].fx) && (sx < graph[0].axis[0].fx + graph[0].axis[0].dfx) &&
	  (sy < graph[0].axis[1].fy) && (sy > graph[0].axis[1].fy + graph[0].axis[1].dfy))
      {
	if (scaleColor) {
	  if (!finite(z[i])) continue;
	  int pixel = MIN (graphic->Npixels - 2, MAX (0, z[i]*(graphic->Npixels - 1)));
	  buffer->bColor_R = pixel1[pixel];
	  buffer->bColor_G = pixel2[pixel];
	  buffer->bColor_B = pixel3[pixel];
	}
	D = scaleSize ? dz*z[i] : ds;
	OpenTriangle (buffer, sx - D, sy + 0.58*D, sx + D, sy + 0.58*D, sx, sy - 1.15*D);
	// out-of-range points skipped by bDrawPoint
      }
    }
  }
  if (object[0].ptype == 15) {	/* open triangle */
    for (i = 0; i < object[0].Npts; i++) {
      if (!(finite(x[i]) && finite(y[i]))) continue;
      sx = x[i]*mxi + y[i]*mxj + bx;
      sy = x[i]*myi + y[i]*myj + by;
      if ((sx > graph[0].axis[0].fx) && (sx < graph[0].axis[0].fx + graph[0].axis[0].dfx) &&
	  (sy < graph[0].axis[1].fy) && (sy > graph[0].axis[1].fy + graph[0].axis[1].dfy))
      {
	if (scaleColor) {
	  if (!finite(z[i])) continue;
	  int pixel = MIN (graphic->Npixels - 2, MAX (0, z[i]*(graphic->Npixels - 1)));
	  buffer->bColor_R = pixel1[pixel];
	  buffer->bColor_G = pixel2[pixel];
	  buffer->bColor_B = pixel3[pixel];
	}
	D = scaleSize ? dz*z[i] : ds;
	OpenTriangle (buffer, sx - D, sy - 0.58*D, sx + D, sy - 0.58*D, sx, sy + 1.15*D);
	// out-of-range points skipped by bDrawPoint
      }
    }
  }
  if (object[0].ptype == 6) {	/* Y */
    for (i = 0; i < object[0].Npts; i++) {
      if (!(finite(x[i]) && finite(y[i]))) continue;
      sx = x[i]*mxi + y[i]*mxj + bx;
      sy = x[i]*myi + y[i]*myj + by;
      if ((sx > graph[0].axis[0].fx) && (sx < graph[0].axis[0].fx + graph[0].axis[0].dfx) &&
	  (sy < graph[0].axis[1].fy) && (sy > graph[0].axis[1].fy + graph[0].axis[1].dfy))
      {
	if (scaleColor) {
	  if (!finite(z[i])) continue;
	  int pixel = MIN (graphic->Npixels - 2, MAX (0, z[i]*(graphic->Npixels - 1)));
	  buffer->bColor_R = pixel1[pixel];
	  buffer->bColor_G = pixel2[pixel];
	  buffer->bColor_B = pixel3[pixel];
	}
	D = scaleSize ? dz*z[i] : ds;
	DrawLine (buffer, sx, sy, sx - D, sy - 0.58*D);
	DrawLine (buffer, sx, sy, sx + D, sy - 0.58*D);
	DrawLine (buffer, sx, sy, sx,     sy + 1.15*D);
	// out-of-range points skipped by bDrawPoint
      }
    }
  }
  if (object[0].ptype == 16) {	/* Y */
    for (i = 0; i < object[0].Npts; i++) {
      if (!(finite(x[i]) && finite(y[i]))) continue;
      sx = x[i]*mxi + y[i]*mxj + bx;
      sy = x[i]*myi + y[i]*myj + by;
      if ((sx > graph[0].axis[0].fx) && (sx < graph[0].axis[0].fx + graph[0].axis[0].dfx) &&
	  (sy < graph[0].axis[1].fy) && (sy > graph[0].axis[1].fy + graph[0].axis[1].dfy))
      {
	if (scaleColor) {
	  if (!finite(z[i])) continue;
	  int pixel = MIN (graphic->Npixels - 2, MAX (0, z[i]*(graphic->Npixels - 1)));
	  buffer->bColor_R = pixel1[pixel];
	  buffer->bColor_G = pixel2[pixel];
	  buffer->bColor_B = pixel3[pixel];
	}
	D = scaleSize ? dz*z[i] : ds;
	DrawLine (buffer, sx, sy, sx - D, sy + 0.58*D);
	DrawLine (buffer, sx, sy, sx + D, sy + 0.58*D);
	DrawLine (buffer, sx, sy, sx,     sy - 1.15*D);
	// out-of-range points skipped by bDrawPoint
      }
    }
  }
  if (object[0].ptype == 7) {	/* 0 */
    for (i = 0; i < object[0].Npts; i++) {
      if (!(finite(x[i]) && finite(y[i]))) continue;
      sx = x[i]*mxi + y[i]*mxj + bx;
      sy = x[i]*myi + y[i]*myj + by;
      if ((sx > graph[0].axis[0].fx) && (sx < graph[0].axis[0].fx + graph[0].axis[0].dfx) &&
	  (sy < graph[0].axis[1].fy) && (sy > graph[0].axis[1].fy + graph[0].axis[1].dfy))
      {
	if (scaleColor) {
	  if (!finite(z[i])) continue;
	  int pixel = MIN (graphic->Npixels - 2, MAX (0, z[i]*(graphic->Npixels - 1)));
	  buffer->bColor_R = pixel1[pixel];
	  buffer->bColor_G = pixel2[pixel];
	  buffer->bColor_B = pixel3[pixel];
	}
	D = scaleSize ? dz*z[i] : ds;
	DrawCircle (buffer, sx, sy, D);
	// out-of-range points skipped by bDrawPoint
      }
    }
  }
  if (object[0].ptype == 8) {	/* pentagon */
    for (i = 0; i < object[0].Npts; i++) {
      if (!(finite(x[i]) && finite(y[i]))) continue;
      sx = x[i]*mxi + y[i]*mxj + bx;
      sy = x[i]*myi + y[i]*myj + by;
      if ((sx > graph[0].axis[0].fx) && (sx < graph[0].axis[0].fx + graph[0].axis[0].dfx) &&
	  (sy < graph[0].axis[1].fy) && (sy > graph[0].axis[1].fy + graph[0].axis[1].dfy))
      {
	if (scaleColor) {
	  if (!finite(z[i])) continue;
	  int pixel = MIN (graphic->Npixels - 2, MAX (0, z[i]*(graphic->Npixels - 1)));
	  buffer->bColor_R = pixel1[pixel];
	  buffer->bColor_G = pixel2[pixel];
	  buffer->bColor_B = pixel3[pixel];
	}
	D = scaleSize ? dz*z[i] : ds;
	DrawLine (buffer, sx + 0.00*D, sy - 1.00*D, sx + 0.95*D, sy - 0.31*D);
	DrawLine (buffer, sx + 0.95*D, sy - 0.31*D, sx + 0.58*D, sy + 0.81*D);
	DrawLine (buffer, sx + 0.58*D, sy + 0.81*D, sx - 0.58*D, sy + 0.81*D);
	DrawLine (buffer, sx - 0.58*D, sy + 0.81*D, sx - 0.95*D, sy - 0.31*D);
	DrawLine (buffer, sx - 0.95*D, sy - 0.31*D, sx + 0.00*D, sy - 1.00*D);
	// out-of-range points skipped by bDrawPoint
      }
    }
  }
  if (object[0].ptype == 9) {	/* hexagon */
    for (i = 0; i < object[0].Npts; i++) {
      if (!(finite(x[i]) && finite(y[i]))) continue;
      sx = x[i]*mxi + y[i]*mxj + bx;
      sy = x[i]*myi + y[i]*myj + by;
      if ((sx > graph[0].axis[0].fx) && (sx < graph[0].axis[0].fx + graph[0].axis[0].dfx) &&
	  (sy < graph[0].axis[1].fy) && (sy > graph[0].axis[1].fy + graph[0].axis[1].dfy))
      {
	if (scaleColor) {
	  if (!finite(z[i])) continue;
	  int pixel = MIN (graphic->Npixels - 2, MAX (0, z[i]*(graphic->Npixels - 1)));
	  buffer->bColor_R = pixel1[pixel];
	  buffer->bColor_G = pixel2[pixel];
	  buffer->bColor_B = pixel3[pixel];
	}
	D = scaleSize ? dz*z[i] : ds;
	DrawLine (buffer, sx -      D, sy,          sx - 0.50*D, sy + 0.87*D);
	DrawLine (buffer, sx - 0.50*D, sy + 0.87*D, sx + 0.50*D, sy + 0.87*D);
	DrawLine (buffer, sx + 0.50*D, sy + 0.87*D, sx +      D, sy);
	DrawLine (buffer, sx +      D, sy,          sx + 0.50*D, sy - 0.87*D);
	DrawLine (buffer, sx + 0.50*D, sy - 0.87*D, sx - 0.50*D, sy - 0.87*D);
	DrawLine (buffer, sx - 0.50*D, sy - 0.87*D, sx -      D, sy);
	// out-of-range points skipped by bDrawPoint
      }
    }
  }
  if (object[0].ptype == 10) {	/* filled circle */
    for (i = 0; i < object[0].Npts; i++) {
      if (!(finite(x[i]) && finite(y[i]))) continue;
      sx = x[i]*mxi + y[i]*mxj + bx;
      sy = x[i]*myi + y[i]*myj + by;
      if ((sx > graph[0].axis[0].fx) && (sx < graph[0].axis[0].fx + graph[0].axis[0].dfx) &&
	  (sy < graph[0].axis[1].fy) && (sy > graph[0].axis[1].fy + graph[0].axis[1].dfy))
      {
	if (scaleColor) {
	  if (!finite(z[i])) continue;
	  int pixel = MIN (graphic->Npixels - 2, MAX (0, z[i]*(graphic->Npixels - 1)));
	  buffer->bColor_R = pixel1[pixel];
	  buffer->bColor_G = pixel2[pixel];
	  buffer->bColor_B = pixel3[pixel];
	}
	D = scaleSize ? dz*z[i] : ds;
	FillCircle (buffer, sx, sy, D);
	// out-of-range points skipped by bDrawLineHorizontal
      }
    }
  }
  if (object[0].ptype == 12) {	/* filled triangle (down) */
    for (i = 0; i < object[0].Npts; i++) {
      if (!(finite(x[i]) && finite(y[i]))) continue;
      sx = x[i]*mxi + y[i]*mxj + bx;
      sy = x[i]*myi + y[i]*myj + by;
      if ((sx > graph[0].axis[0].fx) && (sx < graph[0].axis[0].fx + graph[0].axis[0].dfx) &&
	  (sy < graph[0].axis[1].fy) && (sy > graph[0].axis[1].fy + graph[0].axis[1].dfy))
      {
	if (scaleColor) {
	  if (!finite(z[i])) continue;
	  int pixel = MIN (graphic->Npixels - 2, MAX (0, z[i]*(graphic->Npixels - 1)));
	  buffer->bColor_R = pixel1[pixel];
	  buffer->bColor_G = pixel2[pixel];
	  buffer->bColor_B = pixel3[pixel];
	}
	D = scaleSize ? dz*z[i] : ds;
	// FillTriangle (buffer, sx - D, sy - 0.58*D, sx + D, sy - 0.58*D, sx, sy + 1.15*D);
	FillTriangle (buffer, sx, sy + 0.58*D, D, 1.73*D);
	// out-of-range points skipped by bDrawPoint
      }
    }
  }
  if (object[0].ptype == 100) {	/* connect a pair of points */

    double X0 = graph[0].axis[0].fx;
    double X1 = graph[0].axis[0].fx + graph[0].axis[0].dfx;
    double Y0 = graph[0].axis[1].fy;
    double Y1 = graph[0].axis[1].fy + graph[0].axis[1].dfy;

    for (i = 0; i + 1 < object[0].Npts; i+=2) {
      if (!(finite(x[i]) && finite(y[i]))) continue;
      sx1 = x[i]*mxi + y[i]*mxj + bx;
      sy1 = x[i]*myi + y[i]*myj + by;
      sx2 = x[i+1]*mxi + y[i+1]*mxj + bx;
      sy2 = x[i+1]*myi + y[i+1]*myj + by;
      bDrawClipLine (buffer, sx1, sy1, sx2, sy2, X0, Y0, X1, Y1);
    }
  }

  free (pixel1);
  free (pixel2);
  free (pixel3);
}
    
/*******/
void bDrawXErrors (bDrawBuffer *buffer, KapaGraphWidget *graph, Gobjects *object) {
  
  int i, bar, dz, ds, D;
  float *x, *y, *z, *dxm, *dxp;
  double mxi, mxj, myi, myj, bxi, bxj, byi, byj, bx, by;
  double sx0, sy0, sx1, sy1, sz, sx10, sx11;

  int scaleSize = (object[0].size < 0);

  mxi = graph[0].axis[0].dfx / (object[0].x1 - object[0].x0);
  mxj = graph[0].axis[1].dfx / (object[0].y1 - object[0].y0);
  myi = graph[0].axis[0].dfy / (object[0].x1 - object[0].x0);
  myj = graph[0].axis[1].dfy / (object[0].y1 - object[0].y0);
  
  bxi  =  graph[0].axis[0].fx - object[0].x0*graph[0].axis[0].dfx/(object[0].x1 - object[0].x0);
  bxj  =  -object[0].y0*graph[0].axis[1].dfx/(object[0].y1 - object[0].y0);
  byi  =  -object[0].x0*graph[0].axis[0].dfy/(object[0].x1 - object[0].x0);
  byj  =  graph[0].axis[1].fy - object[0].y0*graph[0].axis[1].dfy/(object[0].y1 - object[0].y0);
  
  bx = bxi + bxj;
  by = byi + byj;
  
  ds = 0.5 * (graphic->dx + graphic->dy) * 0.003 * object[0].size;
  dz = 0.5 * (graphic->dx + graphic->dy) * 0.010;

  x = object[0].x; y = object[0].y; dxp = object[0].dxp; dxm = object[0].dxm; z = object[0].z;
  bar = object[0].ebar; sz = object[0].size*graph[0].axis[1].dfy*0.03;
   
  double X0 = graph[0].axis[0].fx;
  double X1 = graph[0].axis[0].fx + graph[0].axis[0].dfx;
  double Y0 = graph[0].axis[1].fy;
  double Y1 = graph[0].axis[1].fy + graph[0].axis[1].dfy;

  /// XXX NOTE : D should be modified by (mxi,myi) for tilted axes dx = D*(mxi/mx), dy = D*(myi/mx)

  for (i = 0; i < object[0].Npts; i++) {
    // for open circles, only go to the outer radius
    D = 0;
    if (object[0].ptype ==  7) { D = scaleSize ? dz*z[i] : ds; }
    if (object[0].ptype ==  1) { D = scaleSize ? dz*z[i] : ds; }
    if (object[0].ptype ==  5) { D = scaleSize ? 0.66*dz*z[i] : 0.66*ds; }
    if (object[0].ptype == 15) { D = scaleSize ? 0.66*dz*z[i] : 0.66*ds; }
    if (!(finite(x[i]) && finite(y[i]) && finite(dxp[i]))) goto skip_dxp;
    if (D > fabs(dxp[i]*mxi)) goto skip_dxp;
    sx0 = x[i]*mxi + y[i]*mxj + bx + D;
    sy0 = x[i]*myi + y[i]*myj + by;
    sx1 = sx0 + dxp[i]*mxi - D;
    sy1 = sy0 + dxp[i]*myi;
    if (((sx0 > graph[0].axis[0].fx) && (sx0 < graph[0].axis[0].fx + graph[0].axis[0].dfx) &&
	 (sy0 < graph[0].axis[1].fy) && (sy0 > graph[0].axis[1].fy + graph[0].axis[1].dfy)) ||
	((sx1 > graph[0].axis[0].fx) && (sx1 < graph[0].axis[0].fx + graph[0].axis[0].dfx) &&
	 (sy1 < graph[0].axis[1].fy) && (sy1 > graph[0].axis[1].fy + graph[0].axis[1].dfy)))
    {
      bDrawClipLine (buffer, sx0, sy0, sx1, sy1, X0, Y0, X1, Y1);
      if (bar) {
	sx10 = sy1 - sz;
	sx11 = sy1 + sz;
	bDrawClipLine (buffer, sx1, sx10, sx1, sx11, X0, Y0, X1, Y1);
      }
    }
  skip_dxp:
    if (!(finite(x[i]) && finite(y[i]) && finite(dxm[i]))) continue;
    if (D > fabs(dxm[i]*mxi)) continue;
    sx0 = x[i]*mxi + y[i]*mxj + bx - D;
    sy0 = x[i]*myi + y[i]*myj + by;
    sx1 = sx0 - dxm[i]*mxi + D;
    sy1 = sy0 - dxm[i]*myi;
    if (((sx0 > graph[0].axis[0].fx) && (sx0 < graph[0].axis[0].fx + graph[0].axis[0].dfx) &&
	 (sy0 < graph[0].axis[1].fy) && (sy0 > graph[0].axis[1].fy + graph[0].axis[1].dfy)) ||
	((sx1 > graph[0].axis[0].fx) && (sx1 < graph[0].axis[0].fx + graph[0].axis[0].dfx) &&
	 (sy1 < graph[0].axis[1].fy) && (sy1 > graph[0].axis[1].fy + graph[0].axis[1].dfy)))
    {
      bDrawClipLine (buffer, sx0, sy0, sx1, sy1, X0, Y0, X1, Y1);
      if (bar) {
	sx10 = sy1 - sz;
	sx11 = sy1 + sz;
	bDrawClipLine (buffer, sx1, sx10, sx1, sx11, X0, Y0, X1, Y1);
      }
    }
  }
}
    
/*******/
void bDrawYErrors (bDrawBuffer *buffer, KapaGraphWidget *graph, Gobjects *object) {
  
  int i, bar, dz, ds, D;
  float *x, *y, *z, *dym, *dyp;
  double mxi, mxj, myi, myj, bxi, bxj, byi, byj, bx, by;
  double sx0, sy0, sx1, sy1, sz, sx10, sx11;
  
  int scaleSize = (object[0].size < 0);

  mxi = graph[0].axis[0].dfx / (object[0].x1 - object[0].x0);
  mxj = graph[0].axis[1].dfx / (object[0].y1 - object[0].y0);
  myi = graph[0].axis[0].dfy / (object[0].x1 - object[0].x0);
  myj = graph[0].axis[1].dfy / (object[0].y1 - object[0].y0);
  
  bxi  =  graph[0].axis[0].fx - object[0].x0*graph[0].axis[0].dfx/(object[0].x1 - object[0].x0);
  bxj  =  -object[0].y0*graph[0].axis[1].dfx/(object[0].y1 - object[0].y0);
  byi  =  -object[0].x0*graph[0].axis[0].dfy/(object[0].x1 - object[0].x0);
  byj  =  graph[0].axis[1].fy - object[0].y0*graph[0].axis[1].dfy/(object[0].y1 - object[0].y0);
  
  bx = bxi + bxj;
  by = byi + byj;
  
  ds = 0.5 * (graphic->dx + graphic->dy) * 0.003 * object[0].size;
  dz = 0.5 * (graphic->dx + graphic->dy) * 0.010;

  x = object[0].x; y = object[0].y; dyp = object[0].dyp; dym = object[0].dym; z = object[0].z;
  bar = object[0].ebar; sz = object[0].size*graph[0].axis[0].dfx*0.03;
  
  double X0 = graph[0].axis[0].fx;
  double X1 = graph[0].axis[0].fx + graph[0].axis[0].dfx;
  double Y0 = graph[0].axis[1].fy;
  double Y1 = graph[0].axis[1].fy + graph[0].axis[1].dfy;

  /// XXX NOTE : D should be modified by (mxi,myi) for tilted axes dx = D*(mxi/mx), dy = D*(myi/mx)

  for (i = 0; i < object[0].Npts; i++) {
    // for open circles, only go to the outer radius
    D = 0;
    if (object[0].ptype ==  7) { D = scaleSize ? dz*z[i] : ds; }
    if (object[0].ptype ==  1) { D = scaleSize ? dz*z[i] : ds; }
    if (object[0].ptype ==  5) { D = scaleSize ? 1.15*dz*z[i] : 1.15*ds; }
    if (object[0].ptype == 15) { D = scaleSize ? 0.58*dz*z[i] : 0.58*ds; }
    if (!(finite(x[i]) && finite(y[i]) && finite(dyp[i]))) goto skip_dyp;
    if (D > fabs(dyp[i]*myj)) goto skip_dyp;
    sx0 = x[i]*mxi + y[i]*mxj + bx;
    sy0 = x[i]*myi + y[i]*myj + by - D;
    sx1 = sx0 + dyp[i]*mxj;
    sy1 = sy0 + dyp[i]*myj + D;
    if (((sx0 > graph[0].axis[0].fx) && (sx0 < graph[0].axis[0].fx + graph[0].axis[0].dfx) &&
	 (sy0 < graph[0].axis[1].fy) && (sy0 > graph[0].axis[1].fy + graph[0].axis[1].dfy)) ||
	((sx1 > graph[0].axis[0].fx) && (sx1 < graph[0].axis[0].fx + graph[0].axis[0].dfx) &&
	 (sy1 < graph[0].axis[1].fy) && (sy1 > graph[0].axis[1].fy + graph[0].axis[1].dfy)))
    {
      bDrawClipLine (buffer, sx0, sy0, sx1, sy1, X0, Y0, X1, Y1);
      if (bar) {
	sx10 = sx1 - sz;
	sx11 = sx1 + sz;
	bDrawClipLine (buffer, sx10, sy1, sx11, sy1, X0, Y0, X1, Y1);
      }
    }
  skip_dyp:
    if (!(finite(x[i]) && finite(y[i]) && finite(dym[i]))) continue;
    if (object[0].ptype ==  5) { D = scaleSize ? 0.58*dz*z[i] : 0.58*ds; }
    if (object[0].ptype == 15) { D = scaleSize ? 1.15*dz*z[i] : 1.15*ds; }
    if (D > fabs(dym[i]*myj)) continue;
    sx0 = x[i]*mxi + y[i]*mxj + bx;
    sy0 = x[i]*myi + y[i]*myj + by + D;
    sx1 = sx0 - dym[i]*mxj;
    sy1 = sy0 - dym[i]*myj - D;
    if (((sx0 > graph[0].axis[0].fx) && (sx0 < graph[0].axis[0].fx + graph[0].axis[0].dfx) &&
	 (sy0 < graph[0].axis[1].fy) && (sy0 > graph[0].axis[1].fy + graph[0].axis[1].dfy)) ||
	((sx1 > graph[0].axis[0].fx) && (sx1 < graph[0].axis[0].fx + graph[0].axis[0].dfx) &&
	 (sy1 < graph[0].axis[1].fy) && (sy1 > graph[0].axis[1].fy + graph[0].axis[1].dfy)))
    {
      bDrawClipLine (buffer, sx0, sy0, sx1, sy1, X0, Y0, X1, Y1);
      if (bar) {
	sx10 = sx1 - sz;
	sx11 = sx1 + sz;
	bDrawClipLine (buffer, sx10, sy1, sx11, sy1, X0, Y0, X1, Y1);
      }
    }
  }
}
