1ae7e3b26SLisandro Dalcin 2ae7e3b26SLisandro Dalcin /* 3ae7e3b26SLisandro Dalcin Code for drawing color interpolated triangles using X-windows. 4ae7e3b26SLisandro Dalcin */ 5ae7e3b26SLisandro Dalcin #include <../src/sys/classes/draw/impls/x/ximpl.h> 6ae7e3b26SLisandro Dalcin 709440f25SLisandro Dalcin PETSC_INTERN PetscErrorCode PetscDrawInterpolatedTriangle_X(PetscDraw_X *, int, int, int, int, int, int, int, int, int); 809440f25SLisandro Dalcin 9ae7e3b26SLisandro Dalcin #define SHIFT_VAL 6 10ae7e3b26SLisandro Dalcin 11*9371c9d4SSatish Balay 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) { 12ae7e3b26SLisandro Dalcin PetscReal rfrac, lfrac; 13ae7e3b26SLisandro Dalcin PetscReal R_y2_y_1, R_y3_y_1, R_y3_y2; 14ae7e3b26SLisandro Dalcin int lc, rc = 0, lx, rx = 0, xx, y, c; 15ae7e3b26SLisandro Dalcin int rc_lc, rx_lx, t2_t1, x2_x1, t3_t1, x3_x1, t3_t2, x3_x2; 16ae7e3b26SLisandro Dalcin 17ae7e3b26SLisandro Dalcin PetscFunctionBegin; 18ae7e3b26SLisandro Dalcin /* 19ae7e3b26SLisandro Dalcin Is triangle even visible in window? 20ae7e3b26SLisandro Dalcin */ 21ae7e3b26SLisandro Dalcin if (x1 < 0 && x2 < 0 && x3 < 0) PetscFunctionReturn(0); 22ae7e3b26SLisandro Dalcin if (y_1 < 0 && y2 < 0 && y3 < 0) PetscFunctionReturn(0); 23ae7e3b26SLisandro Dalcin if (x1 > win->w && x2 > win->w && x3 > win->w) PetscFunctionReturn(0); 24ae7e3b26SLisandro Dalcin if (y_1 > win->h && y2 > win->h && y3 > win->h) PetscFunctionReturn(0); 25ae7e3b26SLisandro Dalcin 26ae7e3b26SLisandro Dalcin t1 = t1 << SHIFT_VAL; 27ae7e3b26SLisandro Dalcin t2 = t2 << SHIFT_VAL; 28ae7e3b26SLisandro Dalcin t3 = t3 << SHIFT_VAL; 29ae7e3b26SLisandro Dalcin 30ae7e3b26SLisandro Dalcin /* Sort the vertices */ 31*9371c9d4SSatish Balay #define SWAP(a, b) \ 32*9371c9d4SSatish Balay { \ 33*9371c9d4SSatish Balay int _a; \ 34*9371c9d4SSatish Balay _a = a; \ 35*9371c9d4SSatish Balay a = b; \ 36*9371c9d4SSatish Balay b = _a; \ 37*9371c9d4SSatish Balay } 38ae7e3b26SLisandro Dalcin if (y_1 > y2) { 39*9371c9d4SSatish Balay SWAP(y_1, y2); 40*9371c9d4SSatish Balay SWAP(t1, t2); 41*9371c9d4SSatish Balay SWAP(x1, x2); 42ae7e3b26SLisandro Dalcin } 43ae7e3b26SLisandro Dalcin if (y_1 > y3) { 44*9371c9d4SSatish Balay SWAP(y_1, y3); 45*9371c9d4SSatish Balay SWAP(t1, t3); 46*9371c9d4SSatish Balay SWAP(x1, x3); 47ae7e3b26SLisandro Dalcin } 48ae7e3b26SLisandro Dalcin if (y2 > y3) { 49*9371c9d4SSatish Balay SWAP(y2, y3); 50*9371c9d4SSatish Balay SWAP(t2, t3); 51*9371c9d4SSatish Balay SWAP(x2, x3); 52ae7e3b26SLisandro Dalcin } 53ae7e3b26SLisandro Dalcin /* This code is decidely non-optimal; it is intended to be a start at 54ae7e3b26SLisandro Dalcin an implementation */ 55ae7e3b26SLisandro Dalcin 56ae7e3b26SLisandro Dalcin if (y2 != y_1) R_y2_y_1 = 1.0 / ((double)(y2 - y_1)); 57ae7e3b26SLisandro Dalcin else R_y2_y_1 = 0.0; 58ae7e3b26SLisandro Dalcin if (y3 != y_1) R_y3_y_1 = 1.0 / ((double)(y3 - y_1)); 59ae7e3b26SLisandro Dalcin else R_y3_y_1 = 0.0; 60ae7e3b26SLisandro Dalcin t2_t1 = t2 - t1; 61ae7e3b26SLisandro Dalcin x2_x1 = x2 - x1; 62ae7e3b26SLisandro Dalcin t3_t1 = t3 - t1; 63ae7e3b26SLisandro Dalcin x3_x1 = x3 - x1; 64ae7e3b26SLisandro Dalcin for (y = y_1; y <= y2; y++) { 65ae7e3b26SLisandro Dalcin /* PetscDraw a line with the correct color from t1-t2 to t1-t3 */ 66ae7e3b26SLisandro Dalcin /* Left color is (y-y_1)/(y2-y_1) * (t2-t1) + t1 */ 67ae7e3b26SLisandro Dalcin lfrac = ((double)(y - y_1)) * R_y2_y_1; 68ae7e3b26SLisandro Dalcin lc = (int)(lfrac * (t2_t1) + t1); 69ae7e3b26SLisandro Dalcin lx = (int)(lfrac * (x2_x1) + x1); 70ae7e3b26SLisandro Dalcin /* Right color is (y-y_1)/(y3-y_1) * (t3-t1) + t1 */ 71ae7e3b26SLisandro Dalcin rfrac = ((double)(y - y_1)) * R_y3_y_1; 72ae7e3b26SLisandro Dalcin rc = (int)(rfrac * (t3_t1) + t1); 73ae7e3b26SLisandro Dalcin rx = (int)(rfrac * (x3_x1) + x1); 74ae7e3b26SLisandro Dalcin /* PetscDraw the line */ 75ae7e3b26SLisandro Dalcin rc_lc = rc - lc; 76ae7e3b26SLisandro Dalcin rx_lx = rx - lx; 77ae7e3b26SLisandro Dalcin if (rx > lx) { 78ae7e3b26SLisandro Dalcin for (xx = lx; xx <= rx; xx++) { 79ae7e3b26SLisandro Dalcin c = (((xx - lx) * (rc_lc)) / (rx_lx) + lc) >> SHIFT_VAL; 80ae7e3b26SLisandro Dalcin PetscDrawXiSetColor(win, c); 81ae7e3b26SLisandro Dalcin XDrawPoint(win->disp, PetscDrawXiDrawable(win), win->gc.set, xx, y); 82ae7e3b26SLisandro Dalcin } 83ae7e3b26SLisandro Dalcin } else if (rx < lx) { 84ae7e3b26SLisandro Dalcin for (xx = lx; xx >= rx; xx--) { 85ae7e3b26SLisandro Dalcin c = (((xx - lx) * (rc_lc)) / (rx_lx) + lc) >> SHIFT_VAL; 86ae7e3b26SLisandro Dalcin PetscDrawXiSetColor(win, c); 87ae7e3b26SLisandro Dalcin XDrawPoint(win->disp, PetscDrawXiDrawable(win), win->gc.set, xx, y); 88ae7e3b26SLisandro Dalcin } 89ae7e3b26SLisandro Dalcin } else { 90ae7e3b26SLisandro Dalcin c = lc >> SHIFT_VAL; 91ae7e3b26SLisandro Dalcin PetscDrawXiSetColor(win, c); 92ae7e3b26SLisandro Dalcin XDrawPoint(win->disp, PetscDrawXiDrawable(win), win->gc.set, lx, y); 93ae7e3b26SLisandro Dalcin } 94ae7e3b26SLisandro Dalcin } 95ae7e3b26SLisandro Dalcin 96ae7e3b26SLisandro Dalcin /* For simplicity,"move" t1 to the intersection of t1-t3 with the line y=y2. 97ae7e3b26SLisandro Dalcin We take advantage of the previous iteration. */ 98ae7e3b26SLisandro Dalcin if (y2 >= y3) PetscFunctionReturn(0); 99ae7e3b26SLisandro Dalcin if (y_1 < y2) { 100ae7e3b26SLisandro Dalcin t1 = rc; 101ae7e3b26SLisandro Dalcin y_1 = y2; 102ae7e3b26SLisandro Dalcin x1 = rx; 103ae7e3b26SLisandro Dalcin 104ae7e3b26SLisandro Dalcin t3_t1 = t3 - t1; 105ae7e3b26SLisandro Dalcin x3_x1 = x3 - x1; 106ae7e3b26SLisandro Dalcin } 107ae7e3b26SLisandro Dalcin t3_t2 = t3 - t2; 108ae7e3b26SLisandro Dalcin x3_x2 = x3 - x2; 109ae7e3b26SLisandro Dalcin if (y3 != y2) R_y3_y2 = 1.0 / ((double)(y3 - y2)); 110ae7e3b26SLisandro Dalcin else R_y3_y2 = 0.0; 111ae7e3b26SLisandro Dalcin if (y3 != y_1) R_y3_y_1 = 1.0 / ((double)(y3 - y_1)); 112ae7e3b26SLisandro Dalcin else R_y3_y_1 = 0.0; 113ae7e3b26SLisandro Dalcin 114ae7e3b26SLisandro Dalcin for (y = y2; y <= y3; y++) { 115ae7e3b26SLisandro Dalcin /* PetscDraw a line with the correct color from t2-t3 to t1-t3 */ 116ae7e3b26SLisandro Dalcin /* Left color is (y-y_1)/(y2-y_1) * (t2-t1) + t1 */ 117ae7e3b26SLisandro Dalcin lfrac = ((double)(y - y2)) * R_y3_y2; 118ae7e3b26SLisandro Dalcin lc = (int)(lfrac * (t3_t2) + t2); 119ae7e3b26SLisandro Dalcin lx = (int)(lfrac * (x3_x2) + x2); 120ae7e3b26SLisandro Dalcin /* Right color is (y-y_1)/(y3-y_1) * (t3-t1) + t1 */ 121ae7e3b26SLisandro Dalcin rfrac = ((double)(y - y_1)) * R_y3_y_1; 122ae7e3b26SLisandro Dalcin rc = (int)(rfrac * (t3_t1) + t1); 123ae7e3b26SLisandro Dalcin rx = (int)(rfrac * (x3_x1) + x1); 124ae7e3b26SLisandro Dalcin /* PetscDraw the line */ 125ae7e3b26SLisandro Dalcin rc_lc = rc - lc; 126ae7e3b26SLisandro Dalcin rx_lx = rx - lx; 127ae7e3b26SLisandro Dalcin if (rx > lx) { 128ae7e3b26SLisandro Dalcin for (xx = lx; xx <= rx; xx++) { 129ae7e3b26SLisandro Dalcin c = (((xx - lx) * (rc_lc)) / (rx_lx) + lc) >> SHIFT_VAL; 130ae7e3b26SLisandro Dalcin PetscDrawXiSetColor(win, c); 131ae7e3b26SLisandro Dalcin XDrawPoint(win->disp, PetscDrawXiDrawable(win), win->gc.set, xx, y); 132ae7e3b26SLisandro Dalcin } 133ae7e3b26SLisandro Dalcin } else if (rx < lx) { 134ae7e3b26SLisandro Dalcin for (xx = lx; xx >= rx; xx--) { 135ae7e3b26SLisandro Dalcin c = (((xx - lx) * (rc_lc)) / (rx_lx) + lc) >> SHIFT_VAL; 136ae7e3b26SLisandro Dalcin PetscDrawXiSetColor(win, c); 137ae7e3b26SLisandro Dalcin XDrawPoint(win->disp, PetscDrawXiDrawable(win), win->gc.set, xx, y); 138ae7e3b26SLisandro Dalcin } 139ae7e3b26SLisandro Dalcin } else { 140ae7e3b26SLisandro Dalcin c = lc >> SHIFT_VAL; 141ae7e3b26SLisandro Dalcin PetscDrawXiSetColor(win, c); 142ae7e3b26SLisandro Dalcin XDrawPoint(win->disp, PetscDrawXiDrawable(win), win->gc.set, lx, y); 143ae7e3b26SLisandro Dalcin } 144ae7e3b26SLisandro Dalcin } 145ae7e3b26SLisandro Dalcin PetscFunctionReturn(0); 146ae7e3b26SLisandro Dalcin } 147