updated
[chise/kage.git] / kagecgi / kagepoly.c
index 571ca15..1e2f895 100755 (executable)
 \r
 #include "kage.h"\r
 #include "kagecgi.h"\r
-#include "sysdep.h"\r
 \r
-void fillPolygon(struct kPoint *p, int number, int col, unsigned char **image){\r
-       int i, ix, iy;\r
-       Region rgn;\r
-       XRectangle rect;\r
-       \r
-       for(i = 0; i < number; i++){\r
-                       xpoly[i].x = p[i].X;\r
-                       xpoly[i].y = p[i].Y;\r
+void fillPolygon(kPoint *p, int number, int col, unsigned char **image){\r
+  int miny, maxy;\r
+  int cross[16], num;\r
+  int i, j, k, l, m;\r
+  kPoint tpoly[kResolution + 2];\r
+  int tnum;\r
+  double a, b;\r
+\r
+  //copy to temp buffer and detect max/min y-point\r
+  tnum = 0;\r
+  tpoly[tnum].X = p[0].X;\r
+  tpoly[tnum].Y = p[0].Y;\r
+  miny = maxy = tpoly[tnum].Y;\r
+  tnum++;\r
+  for(i = 1; i < number; i++){\r
+    if(p[i].X != tpoly[tnum - 1].X || p[i].Y != tpoly[tnum - 1].Y){\r
+      tpoly[tnum].X = p[i].X;\r
+      tpoly[tnum].Y = p[i].Y;\r
+      if(tpoly[tnum].Y < miny) miny = tpoly[tnum].Y;\r
+      if(tpoly[tnum].Y > maxy) maxy = tpoly[tnum].Y;\r
+      tnum++;\r
+    }\r
+  }\r
+  tpoly[tnum].X = tpoly[0].X;\r
+  tpoly[tnum].Y = tpoly[0].Y;\r
+  tpoly[tnum + 1].X = tpoly[1].X;\r
+  tpoly[tnum + 1].Y = tpoly[1].Y;\r
+\r
+  //detect crossing point of each y-point and draw\r
+  for(j = miny; j <= maxy; j++){\r
+    num = 0;\r
+    for(i = 0; i < tnum; i++){\r
+      //if be parallel to x-axis\r
+      if(tpoly[i].Y == tpoly[i + 1].Y){\r
+       if(tpoly[i].Y == j){\r
+         cross[num] = tpoly[i + 1].X;\r
+         num++;\r
+       }\r
+      }\r
+      //if be parallel to y-axis\r
+      else if(tpoly[i].X == tpoly[i + 1].X){\r
+       if((tpoly[i].Y < j && j <= tpoly[i + 1].Y) ||\r
+        (tpoly[i + 1].Y <= j && j < tpoly[i].Y)){\r
+         cross[num] = tpoly[i].X;\r
+         num++;\r
+         //spearhead: add one more\r
+         if(tpoly[i + 1].Y == j &&\r
+          ((tpoly[i].Y > tpoly[i + 1].Y &&\r
+          tpoly[i + 1].Y < tpoly[i + 2].Y) ||\r
+          (tpoly[i].Y < tpoly[i + 1].Y &&\r
+          tpoly[i + 1].Y > tpoly[i + 2].Y))){\r
+           cross[num] = tpoly[i + 1].X;\r
+           num++;\r
+         }\r
        }\r
-       rgn = XPolygonRegion(xpoly, number, EvenOddRule);\r
-       XClipBox(rgn, &rect);\r
-       \r
-       for(ix = rect.x ; ix <= rect.x + rect.width; ix++){\r
-               for(iy = rect.y ; iy <= rect.y + rect.height; iy++){\r
-                       if(XPointInRegion(rgn, ix, iy) && ix >= 0 && iy >= 0 && ix < canvasWidth && iy < canvasHeight){\r
-                               image[iy][ix] = col;\r
-                       }\r
-               }\r
+      }\r
+      //detect crossing point of each vector\r
+      else if((tpoly[i].Y < j && j <= tpoly[i + 1].Y) ||\r
+       (tpoly[i + 1].Y <= j && j < tpoly[i].Y)){\r
+       a = (tpoly[i + 1].Y - tpoly[i].Y) \r
+        / (tpoly[i + 1].X - tpoly[i].X);\r
+       b = tpoly[i].Y - a * tpoly[i].X;\r
+       cross[num] = (j - b) / a;\r
+       num++;\r
+       //spearhead: add one more\r
+       if(tpoly[i + 1].Y == j &&\r
+        ((tpoly[i].Y > tpoly[i + 1].Y &&\r
+        tpoly[i + 1].Y < tpoly[i + 2].Y) ||\r
+        (tpoly[i].Y < tpoly[i + 1].Y &&\r
+        tpoly[i + 1].Y > tpoly[i + 2].Y))){\r
+         cross[num] = tpoly[i + 1].X;\r
+         num++;\r
        }\r
+      }\r
+    }\r
+    if(num != 0){\r
+      //(for debug)\r
+      //if(num % 2 != 0){\r
+       //fprintf(stderr,"y:%d(%d)\n",j,num);\r
+       //for(k=0;k<num;k++) fprintf(stderr,"%d+",cross[k]);\r
+      //}\r
+      //sort crossing point\r
+      for(k = 0; k < num - 1; k++){\r
+       for(l = num - 1; l > k; l--){\r
+         if(cross[l] < cross[l - 1]){\r
+           m = cross[l];\r
+           cross[l] = cross[l - 1];\r
+           cross[l - 1] = m;\r
+         }\r
+       }\r
+      }\r
+      //draw to vram\r
+      for(k = 0 ; k < num; k = k + 2){\r
+       for(l = cross[k]; l <= cross[k + 1]; l++){\r
+         if(0 <= j && j < canvasHeight && 0 <= l && l < canvasWidth)\r
+          image[j][l] = col;\r
+       }\r
+      }\r
+    }\r
+  }\r
 }\r
-\r