Annotation of researchv10no/libplot/libpen/fill.c, revision 1.1

1.1     ! root        1: #include <stdio.h>
        !             2: #include <math.h>
        !             3: #include "pen.h"
        !             4: #define SEGS 256
        !             5: #define ROUND(a) a>0?a+.5:a-.5
        !             6: double sinp, cosp;
        !             7: int place();
        !             8: fill(num,ff)
        !             9: int num[];
        !            10: double *ff[];
        !            11: {
        !            12:        register struct seg *sp;
        !            13:        register double x, y;
        !            14:        int sgap;
        !            15:        double slant;
        !            16:        struct seg *in, *spend;
        !            17:        struct seg ss[4];
        !            18:        int     so;
        !            19:        double   X, Y, xyt, **fp;
        !            20:        int ymin;
        !            21:        int     M, *n;
        !            22:        double  *xpp, *ypp;
        !            23:        int fx, fy, flag;
        !            24: 
        !            25:        if(!internal){
        !            26:                in = (struct seg *)malloc((unsigned)(SEGS*sizeof(struct seg)));
        !            27:                if(in == 0){
        !            28:                        fprintf(stderr,"not enough space for fill segments\n");
        !            29:                        return(1);
        !            30:                }
        !            31:                spend = in + (SEGS-2);
        !            32:        }
        !            33:        else {
        !            34:                in = ss;
        !            35:                spend = &ss[3];
        !            36:        }
        !            37:        sinp = cos(e1->pslant);
        !            38:        cosp = sin(e1->pslant);
        !            39:        n = num;
        !            40:        fp = ff;
        !            41:        sp = in;
        !            42:        ymin = 4000;
        !            43:        while((M = *n++)){
        !            44:                xpp = *fp++;
        !            45:                ypp = xpp + 1;
        !            46:                x = SCX(*xpp);
        !            47:                y = SCY(*ypp);
        !            48:                if (cosp)  {
        !            49:                        xyt = sinp * x - cosp * y;
        !            50:                        y = cosp * x + sinp * y;
        !            51:                        y = ROUND(y);
        !            52:                        x = ROUND(xyt);
        !            53:                }
        !            54:                fx = x;
        !            55:                fy = y;
        !            56:                flag = 0;
        !            57:                while(--M){
        !            58:                        xpp += 2;
        !            59:                        ypp += 2;
        !            60:                        X = SCX(*xpp);
        !            61:                        Y = SCY(*ypp);
        !            62:                        if (cosp)  {
        !            63:                                xyt = sinp * X - cosp * Y;
        !            64:                                Y = cosp * X + sinp * Y;
        !            65:                                Y = ROUND(Y);
        !            66:                                X = ROUND(xyt);
        !            67:                        }
        !            68:        check:
        !            69:                        if ( (Y > y) || (Y==y && X > x)){
        !            70:                                sp->x = X;
        !            71:                                sp->y = Y;
        !            72:                                sp->X = x;
        !            73:                                sp->Y = y;
        !            74:                        }
        !            75:                        else {
        !            76:                                sp->x = x;
        !            77:                                sp->y = y;
        !            78:                                sp->X = X;
        !            79:                                sp->Y = Y;
        !            80:                        }
        !            81:                        x=X;
        !            82:                        y=Y;
        !            83:                        sp->stat = 1;
        !            84:                        if(Y < ymin)
        !            85:                                ymin = Y;
        !            86:                        if ( sp->y == sp->Y) {
        !            87:                                if(sp > in){
        !            88:                                if ((sp-1)->y == (sp-1)->Y && (sp-1)->y == sp->y) {
        !            89:                                        if (sp->x == (sp - 1)->X) {
        !            90:                                                (sp - 1)->X = sp->X;
        !            91:                                        sp--;
        !            92:                                        } else if (sp->X == (sp - 1)->x) {
        !            93:                                                (sp - 1)->x = sp->x;
        !            94:                                        sp--;
        !            95:                                        }
        !            96:                                }
        !            97:                                }
        !            98:                        }
        !            99:                        if(sp++ > spend){
        !           100:                                fprintf(stderr, "Too many sides\n");
        !           101:                                return(0);
        !           102:                        }
        !           103:                        if((M==1) && (flag == 0)){
        !           104:                                X = fx;
        !           105:                                Y = fy;
        !           106:                                flag = 1;
        !           107:                                goto check;
        !           108:                        }
        !           109:                }
        !           110:        }
        !           111:        sgap = e0->pgap;
        !           112:        slant = e0->pslant;
        !           113:        e0->pgap = e1->pgap;
        !           114:        e0->pslant = e1->pgap;
        !           115:        esave = e1;
        !           116:        penUp();
        !           117:        fprintf(pltout,"SP %d LT;  ",(e1->brush>0)?e1->brush:1);
        !           118:        e1 = e0;
        !           119:        so = sp - in;
        !           120:        realfill(so, in, ymin);
        !           121:        e0->pgap = sgap;
        !           122:        e0->pslant = slant;
        !           123:        e1 = esave;
        !           124:        if(e1->brush > 0 && e1->brush != e1->pen)
        !           125:                fprintf(pltout,"SP %d ",e1->pen);
        !           126:        if(e1->pentype > 0)
        !           127:                fprintf(pltout,"LT%d; ",e1->pentype);
        !           128:        if(!internal)
        !           129:                free(in);
        !           130:        return(1);
        !           131: }
        !           132: double xcut();
        !           133: realfill(so, s, yymin)
        !           134: int    so, yymin;
        !           135: struct seg *s;
        !           136: {
        !           137:        register struct seg *sp, *s1, *s2, *st;
        !           138:        register int    yb;
        !           139:        struct seg *sfin;
        !           140: 
        !           141:        sfin = s1 = s + so - 1;
        !           142:        for (sp = s; sp <= sfin;  sp++) {
        !           143:                if (sp->stat){
        !           144:                        if (sp->y == sp->Y){
        !           145:                                if (sp < sfin) {
        !           146:                                        s2 = sp+1;
        !           147:                                        while(s2<=sfin && s2->y==s2->Y){
        !           148:                                                if (sp->X == s2->x)
        !           149:                                                        sp->X = s2->X;
        !           150:                                                else if (sp->x == s2->X)
        !           151:                                                        sp->x = s2->x;
        !           152:                                                s2++->stat = 0;
        !           153:                                        }
        !           154:                                }
        !           155:                                else s2 = sp;
        !           156:                                if (s1->y == sp->y &&s2->y == sp->y)
        !           157:                                        sp->stat = 0;
        !           158:                                else if (s1->Y == sp->y && s2->Y == sp->y)
        !           159:                                        sp->stat = 0;
        !           160:                        }
        !           161:                }
        !           162:                s1 = sp;
        !           163:        }
        !           164: 
        !           165:        qsort(s, so, sizeof(struct seg), place);
        !           166:        sfin++;
        !           167:        s1 = s;
        !           168:        yb = s1->y;
        !           169:        for(sp=s; yb <= sp->y;sp++);
        !           170:        s2 = sp - 1;
        !           171:        for (; yb >= yymin; yb--) {
        !           172:                if (!(yb % e1->pgap))
        !           173:                        draw(yb, s1, s2);
        !           174:                for (st = s1; st <= s2; st++) {
        !           175:                        if (st->stat)
        !           176:                                if (yb <= st->Y)
        !           177:                                        st->stat = 0;
        !           178:                }
        !           179:                if (sp >= sfin)
        !           180:                        continue;
        !           181:                for (; yb <= (sp->y + 1) && sp < sfin; sp++) {
        !           182:                        for (st = s1; st <= s2; st++) {
        !           183:                                if (sp->y == st->Y) {
        !           184:                                        if (sp->x == st->X)
        !           185:                                                st->stat = 0;
        !           186:                                        else if (sp->Y == sp->y && sp->X == st->X)
        !           187:                                                st->stat = 0;
        !           188:                                }
        !           189:                        }
        !           190:                }
        !           191: 
        !           192:                s2 = sp - 1;
        !           193:                while (!(s1->stat) && s1 < sfin)
        !           194:                        s1++;
        !           195:        }
        !           196: }
        !           197: draw(y, pr1, pr2)
        !           198: int    y;
        !           199: struct seg *pr1, *pr2;
        !           200: {
        !           201:        register struct seg *pr;
        !           202:        register double *xp;
        !           203:        double  x[32], yy, yi, yii, xi, xii, xj;
        !           204:        double *xpe;
        !           205:        for(pr=pr1, xp=x; pr <= pr2;pr++){
        !           206:                if (pr->stat)
        !           207:                        if (pr->y != pr->Y){
        !           208:                                *xp++ = xcut(y, pr->x, pr->y, pr->X, pr->Y);
        !           209:                                if(xp > &x[31])
        !           210:                                        fprintf(stderr, "Too many intersections\n");
        !           211:                        }
        !           212:        }
        !           213:        xpe = xp;
        !           214:        if(xpe < &x[1])
        !           215:                return;
        !           216:        do {
        !           217:                xj = 0;
        !           218:                for(xp=x+1;xp<xpe;xp++){
        !           219:                        if(*(xp-1) > *xp){
        !           220:                                xj = *(xp-1);
        !           221:                                *(xp-1) = *xp;
        !           222:                                *xp = xj;
        !           223:                        }
        !           224:                }
        !           225:        } while(xj);
        !           226:        yy = y;
        !           227:        for(xp=x; xp<xpe;xp++){
        !           228:                xj = *xp++;
        !           229:                if (!cosp)
        !           230:                        line(xj, (double)yy/e1->scale, *xp, (double)yy/e1->scale);
        !           231:                else {
        !           232:                        xi = sinp * xj + cosp * yy;
        !           233:                        yi = -cosp * xj + sinp * yy;
        !           234:                        xii = sinp * *xp + cosp * yy;
        !           235:                        yii = -cosp * *xp + sinp * yy;
        !           236:                        line(xi, (double)yi/e1->scale, xii, (double)yii/e1->scale);
        !           237:                }
        !           238:        }
        !           239: 
        !           240: }
        !           241: 
        !           242: 
        !           243: double xcut(y, x1, y1, x2, y2)
        !           244: {
        !           245:        register double dx, dy, xy, x;
        !           246:        if (y1 == y2) {
        !           247:                x = x1 > x2 ? x1 : x2;
        !           248:                return(x);
        !           249:        }
        !           250:        dy = y1 - y2;
        !           251:        dx = x1 - x2;
        !           252:        xy = (float)y1 * x2 - x1 * (float)y2;
        !           253:        x = (y * dx + xy) / dy;
        !           254:        return(x);
        !           255: }
        !           256: place(p1, p2)
        !           257: struct seg *p1, *p2;
        !           258: {
        !           259:        if(p1->y > p2->y)
        !           260:                return(-1);
        !           261:        else if(p1->y < p2->y)
        !           262:                return(1);
        !           263:        else if(p1->x > p2->x)
        !           264:                return(-1);
        !           265:        else if(p1->x < p2->x)
        !           266:                return(1);
        !           267:        else if(p1->X > p2->X)
        !           268:                return(-1);
        !           269:        else if(p1->X < p2->X)
        !           270:                return(1);
        !           271:        else if(p1->Y > p2->Y)
        !           272:                return(-1);
        !           273:        else if(p1->Y < p2->Y)
        !           274:                return(1);
        !           275:        else return(0);
        !           276: }

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.