Actual source code: tone.c
1: /*$Id: tone.c,v 1.36 2001/03/23 23:20:15 balay Exp $*/
3: /*
4: Code for drawing color interpolated triangles using X-windows.
5: */
6: #include src/sys/src/draw/impls/x/ximpl.h
8: #define SHIFT_VAL 6
10: int PetscDrawInterpolatedTriangle_X(PetscDraw_X* win,int x1,int y_1,int t1,int x2,int y2,int t2,int x3,int y3,int t3)
11: {
12: PetscReal rfrac,lfrac;
13: PetscReal R_y2_y_1,R_y3_y_1,R_y3_y2;
14: int lc,rc = 0,lx,rx = 0,xx,y,c;
15: int rc_lc,rx_lx,t2_t1,x2_x1,t3_t1,x3_x1,t3_t2,x3_x2;
18: /*
19: Is triangle even visible in window?
20: */
21: if (x1 < 0 && x2 < 0 && x3 < 0) return(0);
22: if (y_1 < 0 && y2 < 0 && y3 < 0) return(0);
23: if (x1 > win->w && x2 > win->w && x3 > win->w) return(0);
24: if (y_1 > win->h && y2 > win->h && y3 > win->h) return(0);
26: t1 = t1 << SHIFT_VAL;
27: t2 = t2 << SHIFT_VAL;
28: t3 = t3 << SHIFT_VAL;
30: /* Sort the vertices */
31: #define SWAP(a,b) {int _a; _a=a; a=b; b=_a;}
32: if (y_1 > y2) {
33: SWAP(y_1,y2);SWAP(t1,t2); SWAP(x1,x2);
34: }
35: if (y_1 > y3) {
36: SWAP(y_1,y3);SWAP(t1,t3); SWAP(x1,x3);
37: }
38: if (y2 > y3) {
39: SWAP(y2,y3);SWAP(t2,t3); SWAP(x2,x3);
40: }
41: /* This code is decidely non-optimal; it is intended to be a start at
42: an implementation */
44: if (y2 != y_1) R_y2_y_1 = 1.0/((double)(y2-y_1)); else R_y2_y_1 = 0.0;
45: if (y3 != y_1) R_y3_y_1 = 1.0/((double)(y3-y_1)); else R_y3_y_1 = 0.0;
46: t2_t1 = t2 - t1;
47: x2_x1 = x2 - x1;
48: t3_t1 = t3 - t1;
49: x3_x1 = x3 - x1;
50: for (y=y_1; y<=y2; y++) {
51: /* PetscDraw a line with the correct color from t1-t2 to t1-t3 */
52: /* Left color is (y-y_1)/(y2-y_1) * (t2-t1) + t1 */
53: lfrac = ((double)(y-y_1)) * R_y2_y_1;
54: lc = (int)(lfrac * (t2_t1) + t1);
55: lx = (int)(lfrac * (x2_x1) + x1);
56: /* Right color is (y-y_1)/(y3-y_1) * (t3-t1) + t1 */
57: rfrac = ((double)(y - y_1)) * R_y3_y_1;
58: rc = (int)(rfrac * (t3_t1) + t1);
59: rx = (int)(rfrac * (x3_x1) + x1);
60: /* PetscDraw the line */
61: rc_lc = rc - lc;
62: rx_lx = rx - lx;
63: if (rx > lx) {
64: for (xx=lx; xx<=rx; xx++) {
65: c = (((xx-lx) * (rc_lc)) / (rx_lx) + lc) >> SHIFT_VAL;
66: XiSetColor(win,c);
67: XDrawPoint(win->disp,XiDrawable(win),win->gc.set,xx,y);
68: }
69: } else if (rx < lx) {
70: for (xx=lx; xx>=rx; xx--) {
71: c = (((xx-lx) * (rc_lc)) / (rx_lx) + lc) >> SHIFT_VAL;
72: XiSetColor(win,c);
73: XDrawPoint(win->disp,XiDrawable(win),win->gc.set,xx,y);
74: }
75: } else {
76: c = lc >> SHIFT_VAL;
77: XiSetColor(win,c);
78: XDrawPoint(win->disp,XiDrawable(win),win->gc.set,lx,y);
79: }
80: }
82: /* For simplicity,"move" t1 to the intersection of t1-t3 with the line y=y2.
83: We take advantage of the previous iteration. */
84: if (y2 >= y3) return(0);
85: if (y_1 < y2) {
86: t1 = rc;
87: y_1 = y2;
88: x1 = rx;
90: t3_t1 = t3 - t1;
91: x3_x1 = x3 - x1;
92: }
93: t3_t2 = t3 - t2;
94: x3_x2 = x3 - x2;
95: if (y3 != y2) R_y3_y2 = 1.0/((double)(y3-y2)); else R_y3_y2 = 0.0;
96: if (y3 != y_1) R_y3_y_1 = 1.0/((double)(y3-y_1)); else R_y3_y_1 = 0.0;
97: for (y=y2; y<=y3; y++) {
98: /* PetscDraw a line with the correct color from t2-t3 to t1-t3 */
99: /* Left color is (y-y_1)/(y2-y_1) * (t2-t1) + t1 */
100: lfrac = ((double)(y-y2)) * R_y3_y2;
101: lc = (int)(lfrac * (t3_t2) + t2);
102: lx = (int)(lfrac * (x3_x2) + x2);
103: /* Right color is (y-y_1)/(y3-y_1) * (t3-t1) + t1 */
104: rfrac = ((double)(y - y_1)) * R_y3_y_1;
105: rc = (int)(rfrac * (t3_t1) + t1);
106: rx = (int)(rfrac * (x3_x1) + x1);
107: /* PetscDraw the line */
108: rc_lc = rc - lc;
109: rx_lx = rx - lx;
110: if (rx > lx) {
111: for (xx=lx; xx<=rx; xx++) {
112: c = (((xx-lx) * (rc_lc)) / (rx_lx) + lc) >> SHIFT_VAL;
113: XiSetColor(win,c);
114: XDrawPoint(win->disp,XiDrawable(win),win->gc.set,xx,y);
115: }
116: } else if (rx < lx) {
117: for (xx=lx; xx>=rx; xx--) {
118: c = (((xx-lx) * (rc_lc)) / (rx_lx) + lc) >> SHIFT_VAL;
119: XiSetColor(win,c);
120: XDrawPoint(win->disp,XiDrawable(win),win->gc.set,xx,y);
121: }
122: } else {
123: c = lc >> SHIFT_VAL;
124: XiSetColor(win,c);
125: XDrawPoint(win->disp,XiDrawable(win),win->gc.set,lx,y);
126: }
127: }
128: return(0);
129: }