Actual source code: pops.c

  1: /* $Id: pops.c,v 1.11 2001/04/10 19:34:15 bsmith Exp $*/

  3: /*
  4:     Defines the operations for the Postscript PetscDraw implementation.
  5: */

 7:  #include src/sys/src/draw/impls/ps/psimpl.h

  9: /*@C
 10:       PetscDrawOpenPS - Opens a PetscViewer that generates Postscript

 12:   Collective on MPI_Comm

 14:   Input Parameters:
 15: +   comm - communicator that shares the PetscViewer
 16: -   file - name of file where Postscript is to be stored

 18:   Output Parameter:
 19: .  viewer - the PetscViewer object

 21:   Level: beginner

 23: .seealso: PetscDrawDestroy(), PetscDrawOpenX(), PetscDrawCreate(), PetscViewerDrawOpen(), PetscViewerDrawGetDraw()
 24: @*/
 25: int PetscDrawOpenPS(MPI_Comm comm,char *filename,PetscDraw *draw)
 26: {

 30:   PetscDrawCreate(comm,filename,0,0,0,0,0,draw);
 31:   PetscDrawSetType(*draw,PETSC_DRAW_PS);
 32:   return(0);
 33: }

 35: /*
 36:      These macros transform from the users coordinates to the Postscript
 37: */
 38: #define WIDTH  8.5*72
 39: #define HEIGHT 11*72
 40: #define XTRANS(win,x) 
 41:    ((WIDTH)*((win)->port_xl+(((x-(win)->coor_xl)*((win)->port_xr-(win)->port_xl))/((win)->coor_xr-(win)->coor_xl))))
 42: #define YTRANS(win,y) 
 43:    ((HEIGHT)*((win)->port_yl+(((y-(win)->coor_yl)*((win)->port_yr-(win)->port_yl))/((win)->coor_yr-(win)->coor_yl))))

 45: /*
 46:     Contains the RGB colors for the PETSc defined colors
 47: */
 48: static PetscReal  rgb[3][256];
 49: static PetscTruth rgbfilled = PETSC_FALSE;

 51: #define PSSetColor(ps,c)   (((c) == ps->currentcolor) ? 0 : 
 52: (ps->currentcolor = (c),PetscViewerASCIISynchronizedPrintf(ps->ps_file,"%g %g %g setrgbcolorn",rgb[0][c],rgb[1][c],rgb[2][c])))

 54: static int PetscDrawPoint_PS(PetscDraw draw,PetscReal x,PetscReal  y,int c)
 55: {
 56:   PetscReal     xx,yy;
 57:   int           ierr;
 58:   PetscDraw_PS* ps = (PetscDraw_PS*)draw->data;

 61:   xx = XTRANS(draw,x);  yy = YTRANS(draw,y);
 62:   PSSetColor(ps,c);
 63:   PetscViewerASCIISynchronizedPrintf(ps->ps_file,"%g %g moveto %g %g lineto stroken",xx,yy,xx+1,yy);
 64:   return(0);
 65: }

 67: static int PetscDrawLine_PS(PetscDraw draw,PetscReal xl,PetscReal yl,PetscReal xr,PetscReal yr,int c)
 68: {
 69:   PetscDraw_PS* ps = (PetscDraw_PS*)draw->data;
 70:   PetscReal     x1,y_1,x2,y2;
 71:   int           ierr;

 74:   x1 = XTRANS(draw,xl);   x2  = XTRANS(draw,xr);
 75:   y_1 = YTRANS(draw,yl);   y2  = YTRANS(draw,yr);
 76:   PSSetColor(ps,c);
 77:   PetscViewerASCIISynchronizedPrintf(ps->ps_file,"%g %g moveto %g %g lineto stroken",x1,y_1,x2,y2);
 78:   return(0);
 79: }

 81: static int PetscDrawStringSetSize_PS(PetscDraw draw,PetscReal x,PetscReal  y)
 82: {
 83:   PetscDraw_PS* ps = (PetscDraw_PS*)draw->data;
 84:   int           ierr,w,h;

 87:   w = (int)((WIDTH)*x*(draw->port_xr - draw->port_xl)/(draw->coor_xr - draw->coor_xl));
 88:   h = (int)((HEIGHT)*y*(draw->port_yr - draw->port_yl)/(draw->coor_yr - draw->coor_yl));
 89:   PetscViewerASCIIPrintf(ps->ps_file,"/Helvetica-normal findfont %g scalefont setfontn",(w+h)/2.0);
 90:   return(0);
 91: }

 93: static int PetscDrawStringGetSize_PS(PetscDraw draw,PetscReal *x,PetscReal  *y)
 94: {
 95:   PetscReal   w = 9,h = 9;

 98:   *x = w*(draw->coor_xr - draw->coor_xl)/(WIDTH)*(draw->port_xr - draw->port_xl);
 99:   *y = h*(draw->coor_yr - draw->coor_yl)/(HEIGHT)*(draw->port_yr - draw->port_yl);
100:   return(0);
101: }

103: static int PetscDrawString_PS(PetscDraw draw,PetscReal x,PetscReal  y,int c,char *chrs)
104: {
105:   PetscDraw_PS* ps = (PetscDraw_PS*)draw->data;
106:   PetscReal     x1,y_1;
107:   int           ierr;

110:   PSSetColor(ps,c);
111:   x1 = XTRANS(draw,x);
112:   y_1 = YTRANS(draw,y);
113:   PetscViewerASCIISynchronizedPrintf(ps->ps_file,"%g %g moveto (%s) shown",x1,y_1,chrs);
114:   return(0);
115: }

117: static int PetscDrawStringVertical_PS(PetscDraw draw,PetscReal x,PetscReal  y,int c,char *chrs)
118: {
119:   PetscDraw_PS* ps = (PetscDraw_PS*)draw->data;
120:   PetscReal     x1,y_1;
121:   int           ierr;

124:   PSSetColor(ps,c);
125:   x1 = XTRANS(draw,x);
126:   y_1 = YTRANS(draw,y);
127:   PetscViewerASCIISynchronizedPrintf(ps->ps_file,"gsave %g %g moveto 90 rotate (%s) show grestoren",x1,y_1,chrs);
128:   return(0);
129: }

131: static int PetscDrawInterpolatedTriangle_PS(PetscDraw_PS*,PetscReal,PetscReal,int,PetscReal,PetscReal,int,PetscReal,PetscReal,int);

133: static int PetscDrawTriangle_PS(PetscDraw draw,PetscReal X1,PetscReal Y_1,PetscReal X2,
134:                           PetscReal Y2,PetscReal X3,PetscReal Y3,int c1,int c2,int c3)
135: {
136:   PetscDraw_PS* ps = (PetscDraw_PS*)draw->data;
137:   int           ierr;
138:   PetscReal     x1,y_1,x2,y2,x3,y3;

141:   x1   = XTRANS(draw,X1);
142:   y_1  = YTRANS(draw,Y_1);
143:   x2   = XTRANS(draw,X2);
144:   y2   = YTRANS(draw,Y2);
145:   x3   = XTRANS(draw,X3);
146:   y3   = YTRANS(draw,Y3);

148:   if (c1 == c2 && c2 == c3) {
149:     PSSetColor(ps,c1);
150:     PetscViewerASCIISynchronizedPrintf(ps->ps_file,"%g %g moveto %g %g lineto %g %g lineto filln",x1,y_1,x2,y2,x3,y3);
151:   } else {
152:     PetscDrawInterpolatedTriangle_PS(ps,x1,y_1,c1,x2,y2,c2,x3,y3,c3);
153:   }
154:   return(0);
155: }

157: static int PetscDrawDestroy_PS(PetscDraw draw)
158: {
159:   PetscDraw_PS *ps = (PetscDraw_PS*)draw->data;
160:   int          ierr;
161:   PetscTruth   show;
162:   char         *filename,par[1024];
163: 
165:   PetscViewerASCIIPrintf(ps->ps_file,"nshowpagen");
166:   PetscOptionsHasName(draw->prefix,"-draw_ps_show",&show);
167:   if (show) {
168:     PetscViewerGetFilename(ps->ps_file,&filename);
169:     PetscStrcpy(par,"ghostview ");
170:     PetscStrcat(par,filename);
171:     PetscPOpen(draw->comm,PETSC_NULL,par,"r",PETSC_NULL);
172:   }
173:   PetscViewerDestroy(ps->ps_file);
174:   PetscFree(ps);
175:   return(0);
176: }

178: static int PetscDrawSynchronizedFlush_PS(PetscDraw draw)
179: {
180:   int           ierr;
181:   PetscDraw_PS* ps = (PetscDraw_PS*)draw->data;

184:   PetscViewerFlush(ps->ps_file);
185:   return(0);
186: }

188: static int PetscDrawSynchronizedClear_PS(PetscDraw draw)
189: {
190:   int           ierr;
191:   PetscDraw_PS* ps = (PetscDraw_PS*)draw->data;

194:   PetscViewerFlush(ps->ps_file);
195:   PetscViewerASCIIPrintf(ps->ps_file,"nshowpagen");

197:   return(0);
198: }

200: static struct _PetscDrawOps DvOps = { 0,
201:                                  0,
202:                                  PetscDrawLine_PS,
203:                                  0,
204:                                  0,
205:                                  PetscDrawPoint_PS,
206:                                  0,
207:                                  PetscDrawString_PS,
208:                                  PetscDrawStringVertical_PS,
209:                                  PetscDrawStringSetSize_PS,
210:                                  PetscDrawStringGetSize_PS,
211:                                  0,
212:                                  0,
213:                                  PetscDrawSynchronizedFlush_PS,
214:                                  0,
215:                                  PetscDrawTriangle_PS,
216:                                  0,
217:                                  0,
218:                                  0,
219:                                  PetscDrawSynchronizedClear_PS,
220:                                  0,
221:                                  0,
222:                                  0,
223:                                  0,
224:                                  0,
225:                                  0,
226:                                  PetscDrawDestroy_PS,
227:                                  0,
228:                                  0,
229:                                  0 };

231: EXTERN_C_BEGIN
232: int PetscDrawCreate_PS(PetscDraw draw)
233: {
234:   PetscDraw_PS  *ps;
235:   int           ierr,ncolors,i;
236:   unsigned char *red,*green,*blue;
237:   static int    filecount = 0;
238:   char          buff[32];

241:   if (!draw->display) {
242:     sprintf(buff,"defaultps%d.ps",filecount++);
243:     PetscStrallocpy(buff,&draw->display);
244:   }

246:   PetscNew(PetscDraw_PS,&ps);
247:   PetscMemcpy(draw->ops,&DvOps,sizeof(DvOps));
248:   PetscViewerASCIIOpen(draw->comm,draw->display,&ps->ps_file);
249:   PetscViewerASCIIPrintf(ps->ps_file,"%%!PS-Adobe-2.0n");
250:   PetscViewerASCIIPrintf(ps->ps_file,"%%%%Creator: PETSc %sn",PETSC_VERSION_NUMBER);
251:   PetscViewerASCIIPrintf(ps->ps_file,"%%%%Title: %sn",draw->display);
252:   PetscViewerASCIIPrintf(ps->ps_file,"%%%%Pages: 1n");
253:   PetscViewerASCIIPrintf(ps->ps_file,"%%%%PageOrder: Ascendn");
254:   PetscViewerASCIIPrintf(ps->ps_file,"%%%%BoundingBox: 0 0 612 792n");
255:   PetscViewerASCIIPrintf(ps->ps_file,"%%%%DocumentFonts: Helvetica-normal Symboln");
256:   PetscViewerASCIIPrintf(ps->ps_file,"%%%%EndCommentsn");
257:   PetscViewerASCIIPrintf(ps->ps_file,"/Helvetica-normal findfont 10 scalefont setfontn");
258:   PetscViewerASCIIPrintf(ps->ps_file,"/c {setrgbcolor} defn");
259:   PetscViewerASCIIPrintf(ps->ps_file,"/l {lineto stroke} defn");
260:   PetscViewerASCIIPrintf(ps->ps_file,"/m {moveto} defn");

262:   ps->currentcolor = PETSC_DRAW_BLACK;

264:   if (!rgbfilled) {
265:     rgbfilled = PETSC_TRUE;
266:     rgb[0][PETSC_DRAW_WHITE]       = 255/255;
267:     rgb[1][PETSC_DRAW_WHITE]       = 255/255;
268:     rgb[2][PETSC_DRAW_WHITE]       = 255/255;
269:     rgb[0][PETSC_DRAW_BLACK]       = 0;
270:     rgb[1][PETSC_DRAW_BLACK]       = 0;
271:     rgb[2][PETSC_DRAW_BLACK]       = 0;
272:     rgb[0][PETSC_DRAW_RED]         = 255/255;
273:     rgb[1][PETSC_DRAW_RED]         = 0;
274:     rgb[2][PETSC_DRAW_RED]         = 0;
275:     rgb[0][PETSC_DRAW_GREEN]       = 0;
276:     rgb[1][PETSC_DRAW_GREEN]       = 255./255;
277:     rgb[2][PETSC_DRAW_GREEN]       = 0;
278:     rgb[0][PETSC_DRAW_CYAN]        = 0;
279:     rgb[1][PETSC_DRAW_CYAN]        = 255./255;
280:     rgb[2][PETSC_DRAW_CYAN]        = 255./255;
281:     rgb[0][PETSC_DRAW_BLUE]        = 0;
282:     rgb[1][PETSC_DRAW_BLUE]        = 0;
283:     rgb[2][PETSC_DRAW_BLUE]        = 255./255;
284:     rgb[0][PETSC_DRAW_MAGENTA]     = 255./255;
285:     rgb[1][PETSC_DRAW_MAGENTA]     = 0;
286:     rgb[2][PETSC_DRAW_MAGENTA]     = 255./255;
287:     rgb[0][PETSC_DRAW_AQUAMARINE]  = 127./255;
288:     rgb[1][PETSC_DRAW_AQUAMARINE]  = 255./255;
289:     rgb[2][PETSC_DRAW_AQUAMARINE]  = 212./255;
290:     rgb[0][PETSC_DRAW_FORESTGREEN] = 34./255   ;
291:     rgb[1][PETSC_DRAW_FORESTGREEN] = 139./255.;
292:     rgb[2][PETSC_DRAW_FORESTGREEN] = 34./255. ;
293:     rgb[0][PETSC_DRAW_ORANGE]      = 255./255.    ;
294:     rgb[1][PETSC_DRAW_ORANGE]      = 165./255.;
295:     rgb[2][PETSC_DRAW_ORANGE]      = 0 ;
296:     rgb[0][PETSC_DRAW_VIOLET]      = 238./255.  ;
297:     rgb[1][PETSC_DRAW_VIOLET]      = 130./255.;
298:     rgb[2][PETSC_DRAW_VIOLET]      = 238./255.;
299:     rgb[0][PETSC_DRAW_BROWN]       = 165./255.   ;
300:     rgb[1][PETSC_DRAW_BROWN]       =  42./255.;
301:     rgb[2][PETSC_DRAW_BROWN]       = 42./255.;
302:     rgb[0][PETSC_DRAW_PINK]        = 255./255. ;
303:     rgb[1][PETSC_DRAW_PINK]        = 192./255. ;
304:     rgb[2][PETSC_DRAW_PINK]        = 203./255.;
305:     rgb[0][PETSC_DRAW_CORAL]       = 255./255.;
306:     rgb[1][PETSC_DRAW_CORAL]       = 127./255.;
307:     rgb[2][PETSC_DRAW_CORAL]       = 80./255.;
308:     rgb[0][PETSC_DRAW_GRAY]        = 190./255.  ;
309:     rgb[1][PETSC_DRAW_GRAY]        = 190./255.;
310:     rgb[2][PETSC_DRAW_GRAY]        = 190./255.;
311:     rgb[0][PETSC_DRAW_YELLOW]      = 255./255.    ;
312:     rgb[1][PETSC_DRAW_YELLOW]      = 255./255.;
313:     rgb[2][PETSC_DRAW_YELLOW]      = 0/255.;
314:     rgb[0][PETSC_DRAW_GOLD]        = 255./255.  ;
315:     rgb[1][PETSC_DRAW_GOLD]        =  215./255.;
316:     rgb[2][PETSC_DRAW_GOLD]        =  0;
317:     rgb[0][PETSC_DRAW_LIGHTPINK]   = 255./255.  ;
318:     rgb[1][PETSC_DRAW_LIGHTPINK]   = 182./255.;
319:     rgb[2][PETSC_DRAW_LIGHTPINK]   = 193./255.;
320:     rgb[0][PETSC_DRAW_MEDIUMTURQUOISE] = 72./255.;
321:     rgb[1][PETSC_DRAW_MEDIUMTURQUOISE] =  209./255.;
322:     rgb[2][PETSC_DRAW_MEDIUMTURQUOISE] =  204./255.;
323:     rgb[0][PETSC_DRAW_KHAKI]           = 240./255.  ;
324:     rgb[1][PETSC_DRAW_KHAKI]           = 230./255.;
325:     rgb[2][PETSC_DRAW_KHAKI]           = 140./255.;
326:     rgb[0][PETSC_DRAW_DIMGRAY]         = 105./255.  ;
327:     rgb[1][PETSC_DRAW_DIMGRAY]         = 105./255.;
328:     rgb[2][PETSC_DRAW_DIMGRAY]         = 105./255.;
329:     rgb[0][PETSC_DRAW_YELLOWGREEN]     = 154./255.   ;
330:     rgb[1][PETSC_DRAW_YELLOWGREEN]     = 205./255.;
331:     rgb[2][PETSC_DRAW_YELLOWGREEN]     =  50./255.;
332:     rgb[0][PETSC_DRAW_SKYBLUE]         = 135./255.  ;
333:     rgb[1][PETSC_DRAW_SKYBLUE]         = 206./255.;
334:     rgb[2][PETSC_DRAW_SKYBLUE]         = 235./255.;
335:     rgb[0][PETSC_DRAW_DARKGREEN]       = 0    ;
336:     rgb[1][PETSC_DRAW_DARKGREEN]       = 100./255.;
337:     rgb[2][PETSC_DRAW_DARKGREEN]       = 0;
338:     rgb[0][PETSC_DRAW_NAVYBLUE]        = 0   ;
339:     rgb[1][PETSC_DRAW_NAVYBLUE]        =  0;
340:     rgb[2][PETSC_DRAW_NAVYBLUE]        = 128./255.;
341:     rgb[0][PETSC_DRAW_SANDYBROWN]      = 244./255.   ;
342:     rgb[1][PETSC_DRAW_SANDYBROWN]      = 164./255.;
343:     rgb[2][PETSC_DRAW_SANDYBROWN]      = 96./255.;
344:     rgb[0][PETSC_DRAW_CADETBLUE]       =  95./255.  ;
345:     rgb[1][PETSC_DRAW_CADETBLUE]       = 158./255.;
346:     rgb[2][PETSC_DRAW_CADETBLUE]       = 160./255.;
347:     rgb[0][PETSC_DRAW_POWDERBLUE]      = 176./255.  ;
348:     rgb[1][PETSC_DRAW_POWDERBLUE]      = 224./255.;
349:     rgb[2][PETSC_DRAW_POWDERBLUE]      = 230./255.;
350:     rgb[0][PETSC_DRAW_DEEPPINK]        = 255./255. ;
351:     rgb[1][PETSC_DRAW_DEEPPINK]        =  20./255.;
352:     rgb[2][PETSC_DRAW_DEEPPINK]        = 147./255.;
353:     rgb[0][PETSC_DRAW_THISTLE]         = 216./255.  ;
354:     rgb[1][PETSC_DRAW_THISTLE]         = 191./255.;
355:     rgb[2][PETSC_DRAW_THISTLE]         = 216./255.;
356:     rgb[0][PETSC_DRAW_LIMEGREEN]       = 50./255.  ;
357:     rgb[1][PETSC_DRAW_LIMEGREEN]       = 205./255.;
358:     rgb[2][PETSC_DRAW_LIMEGREEN]       =  50./255.;
359:     rgb[0][PETSC_DRAW_LAVENDERBLUSH]   = 255./255.  ;
360:     rgb[1][PETSC_DRAW_LAVENDERBLUSH]   = 240./255.;
361:     rgb[2][PETSC_DRAW_LAVENDERBLUSH]   = 245./255.;

363:     /* now do the uniform hue part of the colors */
364:     ncolors = 256-PETSC_DRAW_BASIC_COLORS;
365:     ierr    = PetscMalloc(3*ncolors*sizeof(unsigned char),&red);
366:     green   = red   + ncolors;
367:     blue    = green + ncolors;
368:     ierr    = PetscDrawUtilitySetCmapHue(red,green,blue,ncolors);
369:     for (i=PETSC_DRAW_BASIC_COLORS; i<ncolors+PETSC_DRAW_BASIC_COLORS; i++) {
370:       rgb[0][i]  = ((double)red[i-PETSC_DRAW_BASIC_COLORS])/255.;
371:       rgb[1][i]  = ((double)green[i-PETSC_DRAW_BASIC_COLORS])/255.;
372:       rgb[2][i]  = ((double)blue[i-PETSC_DRAW_BASIC_COLORS])/255.;
373:     }
374:     PetscFree(red);
375:   }

377:   draw->data    = (void*)ps;
378:   return(0);
379: }
380: EXTERN_C_END



384: /*
385:          This works in Postscript coordinates
386: */
387: /*  

389:    this kind of thing should do contour triangles with Postscript level 3
390: 1000 dict begin
391:   /ShadingType 4 def
392:   /ColorSpace /DeviceRGB def
393:   /DataSource [0 10 10 255 255 255 0 400 10 0 0 0 0 200 400 255 0 0] def
394:   shfill

396:    once we have Postscript level 3 we should put this in as an option
397: */


400: static int PetscDrawInterpolatedTriangle_PS(PetscDraw_PS* ps,PetscReal x1,PetscReal y_1,int t1,
401:                                 PetscReal x2,PetscReal y2,int t2,PetscReal x3,PetscReal y3,int t3)
402: {
403:   PetscReal rfrac,lfrac;
404:   PetscReal lc,rc = 0.0,lx,rx = 0.0,xx,y;
405:   PetscReal rc_lc,rx_lx,t2_t1,x2_x1,t3_t1,x3_x1,t3_t2,x3_x2;
406:   PetscReal R_y2_y_1,R_y3_y_1,R_y3_y2;
407:   int       ierr,c;

410:   /*
411:         Is triangle even visible in window?
412:   */
413:   if (x1 < 0 && x2 < 0 && x3 < 0) return(0);
414:   if (y_1 < 0 && y2 < 0 && y3 < 0) return(0);
415:   if (x1 > 72*8.5 && x2 > 72*8.5 && x3 > 72*8.5) return(0);
416:   if (y_1 > 72*11 && y2 > 72*11 && y3 > 72*11) return(0);

418:   /* scale everything by two to reduce the huge file; note this reduces the quality */
419:   x1  /= 2.0;
420:   x2  /= 2.0;
421:   x3  /= 2.0;
422:   y_1 /= 2.0;
423:   y2  /= 2.0;
424:   y3  /= 2.0;
425:   PetscViewerASCIISynchronizedPrintf(ps->ps_file,"gsave 2 2 scalen");


428:   /* Sort the vertices */
429: #define SWAP(a,b) {PetscReal _a; _a=a; a=b; b=_a;}
430: #define ISWAP(a,b) {int _a; _a=a; a=b; b=_a;}
431:   if (y_1 > y2) {
432:     SWAP(y_1,y2);ISWAP(t1,t2); SWAP(x1,x2);
433:   }
434:   if (y_1 > y3) {
435:     SWAP(y_1,y3);ISWAP(t1,t3); SWAP(x1,x3);
436:   }
437:   if (y2 > y3) {
438:     SWAP(y2,y3);ISWAP(t2,t3); SWAP(x2,x3);
439:   }
440:   /* This code is decidely non-optimal; it is intended to be a start at
441:    an implementation */

443:   if (y2 != y_1) R_y2_y_1 = 1.0/((y2-y_1)); else R_y2_y_1 = 0.0;
444:   if (y3 != y_1) R_y3_y_1 = 1.0/((y3-y_1)); else R_y3_y_1 = 0.0;
445:   t2_t1   = t2 - t1;
446:   x2_x1   = x2 - x1;
447:   t3_t1   = t3 - t1;
448:   x3_x1   = x3 - x1;
449:   for (y=y_1; y<=y2; y++) {
450:     /* PetscDraw a line with the correct color from t1-t2 to t1-t3 */
451:     /* Left color is (y-y_1)/(y2-y_1) * (t2-t1) + t1 */
452:     lfrac = ((y-y_1)) * R_y2_y_1;
453:     lc    = (lfrac * (t2_t1) + t1);
454:     lx    = (lfrac * (x2_x1) + x1);
455:     /* Right color is (y-y_1)/(y3-y_1) * (t3-t1) + t1 */
456:     rfrac = ((y - y_1)) * R_y3_y_1;
457:     rc    = (rfrac * (t3_t1) + t1);
458:     rx    = (rfrac * (x3_x1) + x1);
459:     /* PetscDraw the line */
460:     rc_lc = rc - lc;
461:     rx_lx = rx - lx;
462:     if (rx > lx) {
463:       for (xx=lx; xx<=rx; xx++) {
464:         c = (int)(((xx-lx) * (rc_lc)) / (rx_lx) + lc);
465:         PetscViewerASCIISynchronizedPrintf(ps->ps_file,"%g %g %g cn",rgb[0][c],rgb[1][c],rgb[2][c]);
466:         PetscViewerASCIISynchronizedPrintf(ps->ps_file,"%g %g m %g %g ln",xx,y,xx+1,y);
467:       }
468:     } else if (rx < lx) {
469:       for (xx=lx; xx>=rx; xx--) {
470:         c = (int)(((xx-lx) * (rc_lc)) / (rx_lx) + lc);
471:         PetscViewerASCIISynchronizedPrintf(ps->ps_file,"%g %g %g cn",rgb[0][c],rgb[1][c],rgb[2][c]);
472:         PetscViewerASCIISynchronizedPrintf(ps->ps_file,"%g %g m %g %g ln",xx,y,xx+1,y);
473:       }
474:     } else {
475:       c = (int)lc;
476:       PetscViewerASCIISynchronizedPrintf(ps->ps_file,"%g %g %g cn",rgb[0][c],rgb[1][c],rgb[2][c]);
477:       PetscViewerASCIISynchronizedPrintf(ps->ps_file,"%g %g m %g %g ln",lx,y,lx+1,y);
478:     }
479:   }

481:   /* For simplicity,"move" t1 to the intersection of t1-t3 with the line y=y2.
482:      We take advantage of the previous iteration. */
483:   if (y2 >= y3) {
484:     PetscViewerASCIISynchronizedPrintf(ps->ps_file,"grestoren");
485:     return(0);
486:   }
487:   if (y_1 < y2) {
488:     t1  = (int)rc;
489:     y_1 = y2;
490:     x1  = rx;

492:     t3_t1   = t3 - t1;
493:     x3_x1   = x3 - x1;
494:   }
495:   t3_t2 = t3 - t2;
496:   x3_x2 = x3 - x2;
497:   if (y3 != y2) R_y3_y2 = 1.0/((y3-y2)); else R_y3_y2 = 0.0;
498:   if (y3 != y_1) R_y3_y_1 = 1.0/((y3-y_1)); else R_y3_y_1 = 0.0;
499:   for (y=y2; y<=y3; y++) {
500:     /* PetscDraw a line with the correct color from t2-t3 to t1-t3 */
501:     /* Left color is (y-y_1)/(y2-y_1) * (t2-t1) + t1 */
502:     lfrac = ((y-y2)) * R_y3_y2;
503:     lc    = (lfrac * (t3_t2) + t2);
504:     lx    = (lfrac * (x3_x2) + x2);
505:     /* Right color is (y-y_1)/(y3-y_1) * (t3-t1) + t1 */
506:     rfrac = ((y - y_1)) * R_y3_y_1;
507:     rc    = (rfrac * (t3_t1) + t1);
508:     rx    = (rfrac * (x3_x1) + x1);
509:     /* PetscDraw the line */
510:     rc_lc = rc - lc;
511:     rx_lx = rx - lx;
512:     if (rx > lx) {
513:       for (xx=lx; xx<=rx; xx++) {
514:         c = (int)(((xx-lx) * (rc_lc)) / (rx_lx) + lc);
515:         PetscViewerASCIISynchronizedPrintf(ps->ps_file,"%g %g %g cn",rgb[0][c],rgb[1][c],rgb[2][c]);
516:         PetscViewerASCIISynchronizedPrintf(ps->ps_file,"%g %g m %g %g ln",xx,y,xx+1,y);
517:       }
518:     } else if (rx < lx) {
519:       for (xx=lx; xx>=rx; xx--) {
520:         c = (int)(((xx-lx) * (rc_lc)) / (rx_lx) + lc);
521:         PetscViewerASCIISynchronizedPrintf(ps->ps_file,"%g %g %g cn",rgb[0][c],rgb[1][c],rgb[2][c]);
522:         PetscViewerASCIISynchronizedPrintf(ps->ps_file,"%g %g m %g %g ln",xx,y,xx+1,y);
523:       }
524:     } else {
525:       c = (int)lc;
526:       PetscViewerASCIISynchronizedPrintf(ps->ps_file,"%g %g %g cn",rgb[0][c],rgb[1][c],rgb[2][c]);
527:       PetscViewerASCIISynchronizedPrintf(ps->ps_file,"%g %g m %g %g ln",lx,y,lx+1,y);
528:     }
529:   }
530:   PetscViewerASCIISynchronizedPrintf(ps->ps_file,"grestoren");
531:   return(0);
532: }