\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
+ int miny, maxy;\r
+ int cross[16], num;\r
+ int i, j, k, l, m;\r
+ double a, b;\r
+\r
+ miny = 10000;\r
+ maxy = -10000;\r
+ //detect max/min y-point\r
+ for(i = 0; i < number; i++){\r
+ if(p[i].Y < miny) miny = p[i].Y;\r
+ if(p[i].Y > maxy) maxy = p[i].Y;\r
+ //fprintf(stderr,"%.1f:%.1f\n",p[i].X,p[i].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 < number; i++){\r
+ //if two points are tha same position, do nothing\r
+ if(p[i].X == p[(i + 1) % number].X &&\r
+ p[i].Y == p[(i + 1) % number].Y) continue;\r
+ //if be parallel to x-axis\r
+ if(p[i].Y == p[(i + 1) % number].Y){\r
+ if(p[i].Y == j){\r
+ cross[num] = p[(i + 1) % number].X;\r
+ //fprintf(stderr,"[%d]",cross[num]);\r
+ num++;\r
+ }\r
+ }\r
+ //if be parallel to y-axis\r
+ else if(p[i].X == p[(i + 1) % number].X){\r
+ if((p[i].Y < j && j <= p[(i + 1) % number].Y) ||\r
+ (p[(i + 1) % number].Y <= j && j < p[i].Y)){\r
+ cross[num] = p[i].X;\r
+ //fprintf(stderr,"[%d]",cross[num]);\r
+ num++;\r
+ //spearhead: add one more\r
+ if(p[(i + 1) % number].Y == j &&\r
+ ((p[i].Y > p[(i + 1) % number].Y &&\r
+ p[(i + 1) % number].Y < p[(i + 2) % number].Y) ||\r
+ (p[i].Y < p[(i + 1) % number].Y &&\r
+ p[(i + 1) % number].Y > p[(i + 2) % number].Y))){\r
+ cross[num] = p[(i + 1) % number].X;\r
+ //fprintf(stderr,"[%d]",cross[num]);\r
+ num++;\r
+ //fprintf(stderr,"(add:%d)",j);\r
+ }\r
+ }\r
+ }\r
+ //detect crossing point of each vector\r
+ else if((p[i].Y < j && j <= p[(i + 1) % number].Y) ||\r
+ (p[(i + 1) % number].Y <= j && j < p[i].Y)){\r
+ a = (p[(i + 1) % number].Y - p[i].Y) \r
+ / (p[(i + 1) % number].X - p[i].X);\r
+ b = p[i].Y - a * p[i].X;\r
+ cross[num] = (j - b) / a;\r
+ //fprintf(stderr,"[%d]",cross[num]);\r
+ num++;\r
+ //spearhead: add one more\r
+ if(p[(i + 1) % number].Y == j &&\r
+ ((p[i].Y > p[(i + 1) % number].Y &&\r
+ p[(i + 1) % number].Y < p[(i + 2) % number].Y) ||\r
+ (p[i].Y < p[(i + 1) % number].Y &&\r
+ p[(i + 1) % number].Y > p[(i + 2) % number].Y))){\r
+ cross[num] = p[(i + 1) % number].X;\r
+ //fprintf(stderr,"[%d]",cross[num]);\r
+ num++;\r
+ //fprintf(stderr,"(add:%d)",j);\r
+ }\r
+ }\r
+ }\r
+ if(num != 0){\r
+ if(num % 2 != 0)fprintf(stderr,"y:%d(%d)\n",j,num);\r
+ //if(num != 0 && num % 2 == 0){\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
- 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
+ //for(k=0;k<num;k++)fprintf(stderr,"%d:",cross[k]);\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
+ //image[j][cross[k]] = col;\r
+ //image[j][cross[k+1]] = col;\r
+ }\r
+ }\r
+ }\r
}\r
\r