Index: branches/eam_branches/ohana.20190329/src/libkapa/include/kapa.h
===================================================================
--- branches/eam_branches/ohana.20190329/src/libkapa/include/kapa.h	(revision 40925)
+++ branches/eam_branches/ohana.20190329/src/libkapa/include/kapa.h	(revision 40926)
@@ -45,5 +45,7 @@
   KAPA_PLOT_BARS_OUTLINE =  4,
   KAPA_PLOT_BARS_OUTFILL =  5,
-  KAPA_PLOT_INVALID_MAX  =  6,
+  KAPA_PLOT_POLYGON      =  6,
+  KAPA_PLOT_POLYFILL     =  7,
+  KAPA_PLOT_INVALID_MAX  =  8,
 } KapaPlotStyle;
 
@@ -320,4 +322,5 @@
 void bDrawTriOpen  (bDrawBuffer *buffer, double x1, double y1, double x2, double y2, double x3, double y3);
 void bDrawTriFill  (bDrawBuffer *buffer, double x1, double y1, double dx, double dy);
+void bDrawPolyFill (bDrawBuffer *buffer, double *x, double *y, int Npoints);
 
 void bDrawSmooth (bDrawBuffer *buffer, float sigma);
Index: branches/eam_branches/ohana.20190329/src/libkapa/src/KapaStyles.c
===================================================================
--- branches/eam_branches/ohana.20190329/src/libkapa/src/KapaStyles.c	(revision 40925)
+++ branches/eam_branches/ohana.20190329/src/libkapa/src/KapaStyles.c	(revision 40926)
@@ -54,4 +54,7 @@
   if (!strcasecmp (string,  "outline")) return KAPA_PLOT_BARS_OUTLINE;
   if (!strcasecmp (string,  "outfill")) return KAPA_PLOT_BARS_OUTFILL;
+
+  if (!strcasecmp (string,  "polygon"))   return KAPA_PLOT_POLYGON;
+  if (!strcasecmp (string,  "polyfill"))  return KAPA_PLOT_POLYFILL;
 
   if (strlen(string) > 2) {
Index: branches/eam_branches/ohana.20190329/src/libkapa/src/bDrawFuncs.c
===================================================================
--- branches/eam_branches/ohana.20190329/src/libkapa/src/bDrawFuncs.c	(revision 40925)
+++ branches/eam_branches/ohana.20190329/src/libkapa/src/bDrawFuncs.c	(revision 40926)
@@ -173,37 +173,51 @@
 void bDrawRectOpen (bDrawBuffer *buffer, double x1, double y1, double x2, double y2) {
 
-  int X1, Y1, X2, Y2;
-
   if (x1 > x2) SWAP (x1, x2);
   if (y1 > y2) SWAP (y1, y2);
 
+  int X1 = MIN (MAX (ROUND (x1), 0), buffer[0].Nx - 1);
+  int X2 = MIN (MAX (ROUND (x2), 1), buffer[0].Nx - 1);
+
+  int Y1 = MIN (MAX (ROUND (y1), 0), buffer[0].Ny - 1);
+  int Y2 = MIN (MAX (ROUND (y2), 1), buffer[0].Ny - 1);
+
+  int dNs = -0.5*(buffer->bWeight - 1); 
+  /* 0, 0, 0, -1, -1, -2, -2 */
+
+  int dNe = +0.5*buffer->bWeight + 1; 
+  /* 1, 1, 2, 2, 2, 3, 3 */
+
+  for (int dN = dNs; dN < dNe; dN ++) {
+    // line on the bottom needs to run longer for the negative dN values
+    bDrawLineHorizontal (buffer, X1 + dN, X2 + 1 - dN, Y1 + dN);
+
+    // line on the top needs to run longer for the positive dN values
+    bDrawLineHorizontal (buffer, X1 + dN, X2 + 1 - dN, Y2 - dN);
+
+    // line on the left needs to run longer for the negative dN values
+    bDrawLineVertical   (buffer, X1 + dN, Y1 + dN, Y2 - dN);
+
+    // line on the right needs to run longer for the positive dN values
+    bDrawLineVertical   (buffer, X2 - dN, Y1 + dN, Y2 - dN);
+  }
+  return;
+}
+
+void bDrawRectFill (bDrawBuffer *buffer, double x1, double y1, double x2, double y2) {
+
+  int i;
+  int X1, Y1, X2, Y2;
+
+  if (x1 > x2) SWAP (x1, x2);
+  if (y1 > y2) SWAP (y1, y2);
+
   X1 = MIN (MAX (ROUND (x1), 0), buffer[0].Nx - 1);
   X2 = MIN (MAX (ROUND (x2), 1), buffer[0].Nx - 1);
 
   Y1 = MIN (MAX (ROUND (y1), 0), buffer[0].Ny - 1);
-  Y2 = MIN (MAX (ROUND (y2), 1), buffer[0].Ny - 1);
-
-  bDrawLineHorizontal (buffer, X1, X2 + 1, Y1);
-  bDrawLineHorizontal (buffer, X1, X2 + 1, Y2);
-  bDrawLineVertical   (buffer, X1, Y1, Y2);
-  bDrawLineVertical   (buffer, X2, Y1, Y2);
-  return;
-}
-
-void bDrawRectFill (bDrawBuffer *buffer, double x1, double y1, double x2, double y2) {
-
-  int i;
-  int X1, Y1, X2, Y2;
-
-  if (x1 > x2) SWAP (x1, x2);
-  if (y1 > y2) SWAP (y1, y2);
-
-  X1 = MIN (MAX (ROUND (x1), 0), buffer[0].Nx - 1);
-  X2 = MIN (MAX (ROUND (x2), 1), buffer[0].Nx - 1);
-
-  Y1 = MIN (MAX (ROUND (y1), 0), buffer[0].Ny - 1);
   Y2 = MIN (MAX (ROUND (y2), 0), buffer[0].Ny - 1);
 
   for (i = Y1; i < Y2; i++) {
+    // this should must be a line or width 1 or we duplicate pixels
     bDrawLineHorizontal (buffer, X1, X2, i);
   } 
@@ -258,4 +272,5 @@
 // use the Bresenham line drawing technique
 // integer-only Bresenham line-draw version which is fast
+// bresenham assumes x1 < x2 and (y2 - y1) < (x2 - x1)
 void bDrawLineBresen (bDrawBuffer *buffer, int X1, int Y1, int X2, int Y2, int swapcoords) {
 
@@ -405,4 +420,212 @@
 }
 
+# define BOT_LEFT 0
+# define BOT_RGHT 1
+# define TOP_LEFT 2
+# define TOP_RGHT 3
+
+// I should probably look up an appropriate recipe for this 
+// source code for XDrawPolyFill?
+
+int GetNextSeqNumber (int seq, int Npoint, int Clockwise, int LeftSide);
+int bDrawFillBetweenSegments (bDrawBuffer *buffer, int *x, int *y, int ystart);
+
+void bDrawPolyFill (bDrawBuffer *buffer, double *x, double *y, int Npoints) {
+
+  // The coord list which is passed in (x,y) must be in order around the contour
+  for (int i = 0; i < Npoints; i++) {
+    fprintf (stderr, "%d : %f,%f\n", i, x[i], y[i]);
+  }
+
+  // First, find the lowest point and start at that sequence number
+  int iMin = 0;
+  double yMin = y[iMin];
+  for (int i = 1; i < Npoints; i++) {
+    if (y[i] < yMin) { iMin = i; yMin = y[iMin]; }
+  }
+
+  // we generate two line segments and will fill between them
+  int xval[4];
+  int yval[4];
+
+  // the starting point is a vertex
+  xval[BOT_LEFT] = x[iMin];  yval[BOT_LEFT] = y[iMin];
+  xval[BOT_RGHT] = x[iMin];  yval[BOT_RGHT] = y[iMin];
+
+  // get iNext, iPrev assuming ClockWise, then test
+  int isCW = TRUE;
+  int iNextLeft = GetNextSeqNumber (iMin, Npoints, isCW, TRUE); // TRUE => left-side
+  int iNextRght = GetNextSeqNumber (iMin, Npoints, isCW, FALSE); 
+
+  // if this is true, the points are not clockwise
+  if (x[iNextLeft] > x[iNextRght]) {
+    isCW = FALSE;
+    int tmp = iNextLeft;
+    iNextLeft = iNextRght;
+    iNextRght = tmp;
+  }
+
+  int iTL = iNextLeft; // sequence number of the top,left point
+  int iTR = iNextRght; // sequence number of the top,right point
+  xval[TOP_LEFT] = x[iTL]; yval[TOP_LEFT] = y[iTL];
+  xval[TOP_RGHT] = x[iTR]; yval[TOP_RGHT] = y[iTR];
+
+  // we need to track the starting row, start at the bottom
+  int ystart = yval[BOT_LEFT];
+
+  while (TRUE) {
+    int isLeft = bDrawFillBetweenSegments (buffer, xval, yval, ystart);
+
+    // the last pair of segments in the sequence end at the same top point:
+    if (iTL == iTR) break;
+
+    // after one pass, if isLeft is true, then cycle the points on the left
+    // segment, otherwise cycle the right segment:
+
+    if (isLeft) {
+      iTL = GetNextSeqNumber (iTL, Npoints, isCW, TRUE);
+      xval[BOT_LEFT] = xval[TOP_LEFT]; yval[BOT_LEFT] = yval[TOP_LEFT];
+      xval[TOP_LEFT] = x[iTL];         yval[TOP_LEFT] = y[iTL];
+      ystart = yval[BOT_LEFT];
+    } else {
+      iTR = GetNextSeqNumber (iTR, Npoints, isCW, FALSE);
+      xval[BOT_RGHT] = xval[TOP_RGHT]; yval[BOT_RGHT] = yval[TOP_RGHT];
+      xval[TOP_RGHT] = x[iTR];         yval[TOP_RGHT] = y[iTR];
+      ystart = yval[BOT_RGHT];
+    }
+  }
+  return;
+}
+
+// we have two line segments defined by x[],y[].  
+
+// [0],[1] is the left-side segment
+// [2],[3] is the right-side segment
+
+// Fill the region between the two segments until 
+// the lower of the two y coordinates y[1], y[3]
+
+// return which of y[1] or y[3] was the ending row
+
+int bDrawFillBetweenSegments_BresenBroken (bDrawBuffer *buffer, int *x, int *y, int ystart) {
+
+  fprintf (stderr, "%d, %d - %d, %d : %d, %d - %d, %d @ %d\n",
+	   x[BOT_LEFT], y[BOT_LEFT], x[TOP_LEFT], y[TOP_LEFT],
+	   x[BOT_RGHT], y[BOT_RGHT], x[TOP_RGHT], y[TOP_RGHT], ystart);
+
+  int dY_L = y[TOP_LEFT] - y[BOT_LEFT];
+  int dX_L = x[TOP_LEFT] - x[BOT_LEFT];
+
+  int dY_R = y[TOP_RGHT] - y[BOT_RGHT];
+  int dX_R = x[TOP_RGHT] - x[BOT_RGHT];
+    
+  // I need to calculate x_L & x_R from the two line segements given ystart.
+
+  // I cannot assume X_L,X_R correspond to x[BOT_LEFT],x[BOT_RGHT]?
+
+  int X_L = (dY_L == 0) ? x[BOT_LEFT] : (ystart - y[BOT_LEFT]) * dX_L / dY_L + x[BOT_LEFT];
+  int X_R = (dY_R == 0) ? x[BOT_RGHT] : (ystart - y[BOT_RGHT]) * dX_R / dY_R + x[BOT_RGHT];
+  int e_L = 0;
+  int e_R = 0;
+
+  fprintf (stderr, "start: %d - %d @ %d\n", X_L, X_R, ystart);
+
+  // stop at whichever comes first
+  // XXX not sure if I need to use < or <= here:
+  // with <=, the value of Y after the loop will be one too high.
+  int Y; 
+  for (Y = ystart; (Y < y[TOP_LEFT]) && (Y < y[TOP_RGHT]); Y++) {
+      
+    // draw horizontal line from X_L to X_R at Y
+    bDrawLineHorizontal (buffer, X_L, X_R, Y);
+
+    e_L += dX_L;
+    int e_L2 = 2*e_L;
+    if (e_L2 > dY_L) {
+      X_L ++;
+      e_L -= dY_L;
+    }
+    if (e_L2 < -dY_L) {
+      X_L --;
+      e_L += dY_L;
+    }
+
+    e_R += dX_R;
+    int e_R2 = 2*e_R;
+    if (e_R2 > dY_R) {
+      X_R ++;
+      e_R -= dY_R;
+    }
+    if (e_R2 < -dY_R) {
+      X_R --;
+      e_R += dY_R;
+    }
+  }
+
+  fprintf (stderr, "end: %d - %d @ %d\n", X_L, X_R, Y);
+
+  if (Y == y[TOP_LEFT]) {
+    return TRUE; // TRUE is LEFT SIDE
+  } else {
+    return FALSE;
+  }
+}
+
+int bDrawFillBetweenSegments (bDrawBuffer *buffer, int *x, int *y, int ystart) {
+
+  int dY_L = y[TOP_LEFT] - y[BOT_LEFT];
+  int dX_L = x[TOP_LEFT] - x[BOT_LEFT];
+
+  int dY_R = y[TOP_RGHT] - y[BOT_RGHT];
+  int dX_R = x[TOP_RGHT] - x[BOT_RGHT];
+    
+  double Slope_L = (dY_L == 0) ? NAN : dX_L / (double) dY_L;
+  double Slope_R = (dY_R == 0) ? NAN : dX_R / (double) dY_R;
+
+  // stop at whichever comes first
+  // XXX not sure if I need to use < or <= here:
+  // with <=, the value of Y after the loop will be one too high.
+  int Y; 
+  for (Y = ystart; (Y < y[TOP_LEFT]) && (Y < y[TOP_RGHT]); Y++) {
+
+    // calculating X_L, X_R based on the slope and current y point
+    int X_L = (dY_L == 0) ? x[BOT_LEFT] : (Y - y[BOT_LEFT]) * Slope_L + x[BOT_LEFT];
+    int X_R = (dY_R == 0) ? x[BOT_RGHT] : (Y - y[BOT_RGHT]) * Slope_R + x[BOT_RGHT];
+      
+    // draw horizontal line from X_L to X_R at Y
+    bDrawLineHorizontal (buffer, X_L, X_R, Y);
+  }
+
+  if (Y == y[TOP_LEFT]) {
+    return TRUE; // TRUE is LEFT SIDE
+  } else {
+    return FALSE;
+  }
+}
+
+int GetNextSeqNumber (int seq, int Npoints, int Clockwise, int LeftSide) {
+
+  int iNext;
+
+  if (Clockwise) {
+    if (LeftSide) {
+      // next point is next in sequence
+      iNext = (seq < Npoints - 1) ? seq + 1 : 0;
+    } else {
+      // next point is prev in sequence
+      iNext = (seq > 0) ? seq - 1 : Npoints - 1;
+    }
+  } else {
+    if (LeftSide) {
+      // next point is prev in sequence
+      iNext = (seq > 0) ? seq - 1 : Npoints - 1;
+    } else {
+      // next point is next in sequence
+      iNext = (seq < Npoints - 1) ? seq + 1 : 0;
+    }
+  }
+  return iNext;
+}
+
 void bDrawArc (bDrawBuffer *buffer, double Xc, double Yc, double Xr, double Yr, double Ts, double Te) {
 
@@ -604,4 +827,5 @@
   if (x == 0) {
     if (decrementY) {
+      // this should must be a line or width 1 or we duplicate pixels
       bDrawLineHorizontal (buffer, Xc    , Xc + 1, Yc + y);
       bDrawLineHorizontal (buffer, Xc    , Xc + 1, Yc - y);
@@ -609,4 +833,5 @@
 
     // center line 
+    // this should must be a line or width 1 or we duplicate pixels
     bDrawLineHorizontal (buffer, Xc - y, Xc + y + 1, Yc    );
     return;
@@ -614,4 +839,5 @@
 
   if (x == y) {
+    // this should must be a line or width 1 or we duplicate pixels
     bDrawLineHorizontal (buffer, Xc - x, Xc + x + 1, Yc + y);
     bDrawLineHorizontal (buffer, Xc - x, Xc + x + 1, Yc - y);
@@ -621,4 +847,5 @@
   // only draw these two lines if we decrement y
   if (decrementY) {
+    // this must be a line or width 1 or we duplicate pixels
     bDrawLineHorizontal (buffer, Xc - x, Xc + x + 1, Yc + y);
     bDrawLineHorizontal (buffer, Xc - x, Xc + x + 1, Yc - y);
@@ -626,4 +853,5 @@
 
   // always draw these two lines:
+  // this must be a line or width 1 or we duplicate pixels
   bDrawLineHorizontal (buffer, Xc - y, Xc + y + 1, Yc + x);
   bDrawLineHorizontal (buffer, Xc - y, Xc + y + 1, Yc - x);
