Actual source code: tone.c

petsc-3.3-p7 2013-05-11
  2: /*
  3:     Code for drawing color interpolated triangles using X-windows.
  4: */
  5: #include <../src/sys/draw/impls/x/ximpl.h>

  7: #define SHIFT_VAL 6

 11: PetscErrorCode PetscDrawInterpolatedTriangle_X(PetscDraw_X* win,int x1,int y_1,int t1,int x2,int y2,int t2,int x3,int y3,int t3)
 12: {
 13:   PetscReal rfrac,lfrac;
 14:   PetscReal R_y2_y_1,R_y3_y_1,R_y3_y2;
 15:   int       lc,rc = 0,lx,rx = 0,xx,y,c;
 16:   int       rc_lc,rx_lx,t2_t1,x2_x1,t3_t1,x3_x1,t3_t2,x3_x2;

 19:   /*
 20:         Is triangle even visible in window?
 21:   */
 22:   if (x1 < 0 && x2 < 0 && x3 < 0) return(0);
 23:   if (y_1 < 0 && y2 < 0 && y3 < 0) return(0);
 24:   if (x1 > win->w && x2 > win->w && x3 > win->w) return(0);
 25:   if (y_1 > win->h && y2 > win->h && y3 > win->h) return(0);

 27:   t1 = t1 << SHIFT_VAL;
 28:   t2 = t2 << SHIFT_VAL;
 29:   t3 = t3 << SHIFT_VAL;

 31:   /* Sort the vertices */
 32: #define SWAP(a,b) {int _a; _a=a; a=b; b=_a;}
 33:   if (y_1 > y2) {
 34:     SWAP(y_1,y2);SWAP(t1,t2); SWAP(x1,x2);
 35:   }
 36:   if (y_1 > y3) {
 37:     SWAP(y_1,y3);SWAP(t1,t3); SWAP(x1,x3);
 38:   }
 39:   if (y2 > y3) {
 40:     SWAP(y2,y3);SWAP(t2,t3); SWAP(x2,x3);
 41:   }
 42:   /* This code is decidely non-optimal; it is intended to be a start at
 43:    an implementation */

 45:   if (y2 != y_1) R_y2_y_1 = 1.0/((double)(y2-y_1)); else R_y2_y_1 = 0.0;
 46:   if (y3 != y_1) R_y3_y_1 = 1.0/((double)(y3-y_1)); else R_y3_y_1 = 0.0;
 47:   t2_t1   = t2 - t1;
 48:   x2_x1   = x2 - x1;
 49:   t3_t1   = t3 - t1;
 50:   x3_x1   = x3 - x1;
 51:   for (y=y_1; y<=y2; y++) {
 52:     /* PetscDraw a line with the correct color from t1-t2 to t1-t3 */
 53:     /* Left color is (y-y_1)/(y2-y_1) * (t2-t1) + t1 */
 54:     lfrac = ((double)(y-y_1)) * R_y2_y_1;
 55:     lc    = (int)(lfrac * (t2_t1) + t1);
 56:     lx    = (int)(lfrac * (x2_x1) + x1);
 57:     /* Right color is (y-y_1)/(y3-y_1) * (t3-t1) + t1 */
 58:     rfrac = ((double)(y - y_1)) * R_y3_y_1;
 59:     rc    = (int)(rfrac * (t3_t1) + t1);
 60:     rx    = (int)(rfrac * (x3_x1) + x1);
 61:     /* PetscDraw the line */
 62:     rc_lc = rc - lc;
 63:     rx_lx = rx - lx;
 64:     if (rx > lx) {
 65:       for (xx=lx; xx<=rx; xx++) {
 66:         c = (((xx-lx) * (rc_lc)) / (rx_lx) + lc) >> SHIFT_VAL;
 67:         PetscDrawXiSetColor(win,c);
 68:         XDrawPoint(win->disp,PetscDrawXiDrawable(win),win->gc.set,xx,y);
 69:       }
 70:     } else if (rx < lx) {
 71:       for (xx=lx; xx>=rx; xx--) {
 72:         c = (((xx-lx) * (rc_lc)) / (rx_lx) + lc) >> SHIFT_VAL;
 73:         PetscDrawXiSetColor(win,c);
 74:         XDrawPoint(win->disp,PetscDrawXiDrawable(win),win->gc.set,xx,y);
 75:       }
 76:     } else {
 77:       c = lc >> SHIFT_VAL;
 78:       PetscDrawXiSetColor(win,c);
 79:       XDrawPoint(win->disp,PetscDrawXiDrawable(win),win->gc.set,lx,y);
 80:     }
 81:   }

 83:   /* For simplicity,"move" t1 to the intersection of t1-t3 with the line y=y2.
 84:      We take advantage of the previous iteration. */
 85:   if (y2 >= y3) return(0);
 86:   if (y_1 < y2) {
 87:     t1  = rc;
 88:     y_1 = y2;
 89:     x1  = rx;

 91:     t3_t1   = t3 - t1;
 92:     x3_x1   = x3 - x1;
 93:   }
 94:   t3_t2 = t3 - t2;
 95:   x3_x2 = x3 - x2;
 96:   if (y3 != y2) R_y3_y2 = 1.0/((double)(y3-y2)); else R_y3_y2 = 0.0;
 97:   if (y3 != y_1) R_y3_y_1 = 1.0/((double)(y3-y_1)); else R_y3_y_1 = 0.0;
 98:   for (y=y2; y<=y3; y++) {
 99:     /* PetscDraw a line with the correct color from t2-t3 to t1-t3 */
100:     /* Left color is (y-y_1)/(y2-y_1) * (t2-t1) + t1 */
101:     lfrac = ((double)(y-y2)) * R_y3_y2;
102:     lc    = (int)(lfrac * (t3_t2) + t2);
103:     lx    = (int)(lfrac * (x3_x2) + x2);
104:     /* Right color is (y-y_1)/(y3-y_1) * (t3-t1) + t1 */
105:     rfrac = ((double)(y - y_1)) * R_y3_y_1;
106:     rc    = (int)(rfrac * (t3_t1) + t1);
107:     rx    = (int)(rfrac * (x3_x1) + x1);
108:     /* PetscDraw the line */
109:     rc_lc = rc - lc;
110:     rx_lx = rx - lx;
111:     if (rx > lx) {
112:       for (xx=lx; xx<=rx; xx++) {
113:         c = (((xx-lx) * (rc_lc)) / (rx_lx) + lc) >> SHIFT_VAL;
114:         PetscDrawXiSetColor(win,c);
115:         XDrawPoint(win->disp,PetscDrawXiDrawable(win),win->gc.set,xx,y);
116:       }
117:     } else if (rx < lx) {
118:       for (xx=lx; xx>=rx; xx--) {
119:         c = (((xx-lx) * (rc_lc)) / (rx_lx) + lc) >> SHIFT_VAL;
120:         PetscDrawXiSetColor(win,c);
121:         XDrawPoint(win->disp,PetscDrawXiDrawable(win),win->gc.set,xx,y);
122:       }
123:     } else {
124:       c = lc >> SHIFT_VAL;
125:       PetscDrawXiSetColor(win,c);
126:       XDrawPoint(win->disp,PetscDrawXiDrawable(win),win->gc.set,lx,y);
127:     }
128:   }
129:   return(0);
130: }