X-Git-Url: http://git.chise.org/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=kagecgi%2Fkagepoly.c;h=69e29fba6916484bf51caaec44802f818ed47b4f;hb=e70b10bbb8b683ef92123b9ee068efe2f4757b2b;hp=358148ed727730be9b4bf1736679b6d77b8de0a9;hpb=b755cadf536858496296e43b30baec2cd9ec8c13;p=chise%2Fkage.git diff --git a/kagecgi/kagepoly.c b/kagecgi/kagepoly.c index 358148e..69e29fb 100755 --- a/kagecgi/kagepoly.c +++ b/kagecgi/kagepoly.c @@ -1,29 +1,107 @@ // kagepoly.c // -#include -#include -#include "kagecgi.h" #include "kage.h" +#include "kagecgi.h" void fillPolygon(struct kPoint *p, int number, int col, unsigned char **image){ - int i, ix, iy; - Region rgn; - XRectangle rect; - - for(i = 0; i < number; i++){ - xpoly[i].x = p[i].X; - xpoly[i].y = p[i].Y; + int miny, maxy; + int cross[16], num; + int i, j, k, l, m; + struct kPoint tpoly[kResolution + 2]; + int tnum; + double a, b; + + //copy to temp buffer and detect max/min y-point + tnum = 0; + tpoly[tnum].X = p[0].X; + tpoly[tnum].Y = p[0].Y; + miny = maxy = tpoly[tnum].Y; + tnum++; + for(i = 1; i < number; i++){ + if(p[i].X != tpoly[tnum - 1].X || p[i].Y != tpoly[tnum - 1].Y){ + tpoly[tnum].X = p[i].X; + tpoly[tnum].Y = p[i].Y; + if(tpoly[tnum].Y < miny) miny = tpoly[tnum].Y; + if(tpoly[tnum].Y > maxy) maxy = tpoly[tnum].Y; + tnum++; + } + } + tpoly[tnum].X = tpoly[0].X; + tpoly[tnum].Y = tpoly[0].Y; + tpoly[tnum + 1].X = tpoly[1].X; + tpoly[tnum + 1].Y = tpoly[1].Y; + + //detect crossing point of each y-point and draw + for(j = miny; j <= maxy; j++){ + num = 0; + for(i = 0; i < tnum; i++){ + //if be parallel to x-axis + if(tpoly[i].Y == tpoly[i + 1].Y){ + if(tpoly[i].Y == j){ + cross[num] = tpoly[i + 1].X; + num++; } - rgn = XPolygonRegion(xpoly, number, EvenOddRule); - XClipBox(rgn, &rect); - - for(ix = rect.x ; ix <= rect.x + rect.width; ix++){ - for(iy = rect.y ; iy <= rect.y + rect.height; iy++){ - if(XPointInRegion(rgn, ix, iy) && ix >= 0 && iy >= 0 && ix < canvasWidth && iy < canvasHeight){ - image[iy][ix] = col; - } - } + } + //if be parallel to y-axis + else if(tpoly[i].X == tpoly[i + 1].X){ + if((tpoly[i].Y < j && j <= tpoly[i + 1].Y) || + (tpoly[i + 1].Y <= j && j < tpoly[i].Y)){ + cross[num] = tpoly[i].X; + num++; + //spearhead: add one more + if(tpoly[i + 1].Y == j && + ((tpoly[i].Y > tpoly[i + 1].Y && + tpoly[i + 1].Y < tpoly[i + 2].Y) || + (tpoly[i].Y < tpoly[i + 1].Y && + tpoly[i + 1].Y > tpoly[i + 2].Y))){ + cross[num] = tpoly[i + 1].X; + num++; + } } + } + //detect crossing point of each vector + else if((tpoly[i].Y < j && j <= tpoly[i + 1].Y) || + (tpoly[i + 1].Y <= j && j < tpoly[i].Y)){ + a = (tpoly[i + 1].Y - tpoly[i].Y) + / (tpoly[i + 1].X - tpoly[i].X); + b = tpoly[i].Y - a * tpoly[i].X; + cross[num] = (j - b) / a; + num++; + //spearhead: add one more + if(tpoly[i + 1].Y == j && + ((tpoly[i].Y > tpoly[i + 1].Y && + tpoly[i + 1].Y < tpoly[i + 2].Y) || + (tpoly[i].Y < tpoly[i + 1].Y && + tpoly[i + 1].Y > tpoly[i + 2].Y))){ + cross[num] = tpoly[i + 1].X; + num++; + } + } + } + if(num != 0){ + //(for debug) + if(num % 2 != 0){ + fprintf(stderr,"y:%d(%d)\n",j,num); + for(k=0;k k; l--){ + if(cross[l] < cross[l - 1]){ + m = cross[l]; + cross[l] = cross[l - 1]; + cross[l - 1] = m; + } + } + } + //draw to vram + for(k = 0 ; k < num; k = k + 2){ + for(l = cross[k]; l <= cross[k + 1]; l++){ + if(0 <= j && j < canvasHeight && 0 <= l && l < canvasWidth) + image[j][l] = col; + } + } + } + } } -