home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Professional / OS2PRO194.ISO / os2 / progs / pari / pari_137 / src / plot.ori < prev    next >
Text File  |  1992-05-20  |  15KB  |  482 lines

  1. /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
  2. /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
  3. /*                                                                 */
  4. /*                     PLOT EN HAUTE RESOLUTION                    */
  5. /*                                                                 */
  6. /*                       copyright Babe Cool                       */
  7. /*                                                                 */
  8. /*                                                                 */
  9. /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
  10. /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
  11.  
  12. # include "genpari.h"
  13.  
  14. #include <X11/Xlib.h>
  15. #include <X11/Xutil.h>
  16. #include <X11/Xos.h>
  17.  
  18. #define IDEC 90
  19. #define JDEC 5
  20.  
  21. int xerror()
  22. {
  23.   err(ploter1);return 0;
  24. }
  25.  
  26. GEN  ploth(ep,a,b,ch)
  27.      entree *ep;
  28.      GEN a,b;
  29.      char *ch;   
  30. {
  31.   long av = avma,av2,jz,i,sig,is,js,iz;
  32.   GEN p1,p2,p3,p4,ysml,ybig,x,diff,dyj,dx,y[2000];
  33.   char c1[20], c2[20], c3[20], c4[20];
  34.   XPoint points[2000];
  35.  
  36.   Display *display;
  37.   Window win;
  38.   XSizeHints size_hints;
  39.   XEvent report;
  40.   GC gc;
  41.   XFontStruct *font_info;
  42.   int screen, width, height;
  43.   
  44.   sig=gcmp(b,a); if(!sig) return gnil;
  45.   if(sig<0) {x=a;a=b;b=x;}
  46.   
  47.   if (!(display = XOpenDisplay(NULL))) err(ploter2);
  48.   if (!(font_info = XLoadQueryFont(display, "9x15"))) err(ploter3);
  49.  
  50.   XSetErrorHandler(xerror);
  51.   XSetIOErrorHandler(xerror);
  52.  
  53.   screen = DefaultScreen(display);
  54.   width = DisplayWidth(display, screen) - 40;
  55.   height = DisplayHeight(display, screen) - 60;
  56.   is = width - IDEC - 5;
  57.   js = height - 25;
  58.   
  59.   for(i=0;i<=is;i++) y[i]=cgetr(3);
  60.   newvalue(ep,cgetr(3)); x=ep->value;
  61.   gaffect(a,x);
  62.   dx=gdivgs(gsub(b,a),is); ysml = ybig = gzero;
  63.   av2=avma;
  64.   for(i=0;i<=is;i++)
  65.     {
  66.       gaffect(lisexpr(ch), y[i]);
  67.       if(gcmp(y[i], ysml) < 0) ysml = y[i]; else if(gcmp(y[i], ybig) > 0) ybig = y[i];
  68.       gaddz(x,dx,x);avma=av2;
  69.     }
  70.   diff = gsub(ybig, ysml);
  71.   if(gcmp0(diff)) {ybig=gaddsg(1,ybig);diff=gun;}
  72.   dyj = gdivsg(js, diff); av2 = avma;
  73.   for(i = 0; i <= is; i++)
  74.     {
  75.       points[i].x = IDEC + i;
  76.       points[i].y = JDEC + itos(ground(gmul(gsub(ybig, y[i]),dyj)));
  77.       avma=av2;
  78.     }
  79.   jz = JDEC + itos(ground(gmul(ybig,dyj)));
  80.   if(gsigne(a)*gsigne(b)<0) iz = IDEC + itos(ground(gmulsg(is, gdiv(a, gsub(a, b)))));
  81.  
  82.   p1=cgetr(4); gaffect(ybig,p1); sprintf(c1,"%9.3lf",rtodbl(p1));
  83.   p2=cgetr(4); gaffect(ysml,p2); sprintf(c2,"%9.3lf",rtodbl(p2));
  84.   p3=cgetr(4); gaffect(a,p3); sprintf(c3,"%9.3lf",rtodbl(p3));
  85.   p4=cgetr(4); gaffect(b,p4); sprintf(c4,"%9.3lf",rtodbl(p4));
  86.  
  87.   win = XCreateSimpleWindow(display, RootWindow(display, screen), 0, 0, width, height, 4, BlackPixel(display, screen), WhitePixel(display, screen));
  88.  
  89.   size_hints.flags = PPosition | PSize;
  90.   size_hints.x = 0;
  91.   size_hints.y = 0;
  92.   size_hints.width = width;
  93.   size_hints.height = height;
  94.  
  95.   XSetStandardProperties(display, win, "ploth", NULL, NULL, NULL, 0, &size_hints);
  96.  
  97.   XSelectInput(display, win, ExposureMask | ButtonPressMask);
  98.  
  99.   gc = XCreateGC(display, win, 0, NULL);
  100.   XSetFont(display, gc, font_info->fid);
  101.   XSetForeground(display, gc, BlackPixel(display, screen)); 
  102.  
  103.   XMapWindow(display, win);
  104.  
  105.   for(;;)
  106.     {
  107.       XNextEvent(display, &report);
  108.       if (report.type != Expose) break;
  109.       while (XCheckTypedEvent(display, Expose, &report));
  110.       XDrawRectangle(display, win, gc, IDEC, JDEC, is, js);
  111.       XDrawLine(display, win, gc, IDEC, jz, IDEC + is, jz);
  112.       if (iz) XDrawLine(display, win, gc, iz, JDEC, iz, js + JDEC);
  113.       XDrawString(display, win, gc, 0, 15, c1, strlen(c1));
  114.       XDrawString(display, win, gc, 0, height - 30, c2, strlen(c2));
  115.       XDrawString(display, win, gc, IDEC - 20, height - 5, c3, strlen(c3));
  116.       XDrawString(display, win, gc, width - 90, height - 5, c4, strlen(c4));
  117.       XDrawLines(display, win, gc, points, is+1, 0);
  118.     }
  119.   XUnloadFont(display, font_info->fid);
  120.   XFreeGC(display, gc);
  121.   XCloseDisplay(display);
  122.   avma = av;
  123.   killvalue(ep);
  124.   return gnil;
  125. }
  126.  
  127. #define NBPOINTS 1500
  128.  
  129. GEN  ploth2(ep,a,b,ch)
  130.      entree *ep;
  131.      GEN a,b;
  132.      char *ch;   
  133. {
  134.   long av = avma,av2,jz,i,sig,is,js,iz;
  135.   GEN p1,p2,p3,p4,xsml,xbig,ysml,ybig,t,diffx,diffy,dy,dx,dt,x[NBPOINTS],y[NBPOINTS];
  136.   char c1[20], c2[20], c3[20], c4[20];
  137.   XPoint points[NBPOINTS];
  138.  
  139.   Display *display;
  140.   Window win;
  141.   XSizeHints size_hints;
  142.   XEvent report;
  143.   GC gc;
  144.   XFontStruct *font_info;
  145.   int screen, width, height;
  146.   
  147.   sig=gcmp(b,a); if(!sig) return gnil;
  148.   if(sig<0) {t=a;a=b;b=t;}
  149.   
  150.   if (!(display = XOpenDisplay(NULL))) err(ploter2);
  151.   if (!(font_info = XLoadQueryFont(display, "9x15"))) err(ploter3);
  152.  
  153.   XSetErrorHandler(xerror);
  154.   XSetIOErrorHandler(xerror);
  155.  
  156.   screen = DefaultScreen(display);
  157.   width = DisplayWidth(display, screen) - 40;
  158.   height = DisplayHeight(display, screen) - 60;
  159.   is = width - IDEC - 5;
  160.   js = height - 25;
  161.   
  162.   for(i=0;i<NBPOINTS;i++) {x[i] = cgetr(3); y[i]=cgetr(3);}
  163.   newvalue(ep,cgetr(3)); t=ep->value;
  164.   gaffect(a,t);
  165.   dt=gdivgs(gsub(b,a),NBPOINTS); xsml = xbig = ysml = ybig = gzero;
  166.   av2=avma;
  167.   for(i=0;i<NBPOINTS;i++)
  168.     {
  169.       p1 = lisexpr(ch);
  170.       gaffect(p1[1], x[i]); gaffect(p1[2], y[i]);
  171.       if(gcmp(x[i], xsml) < 0) xsml = x[i]; else if(gcmp(x[i], xbig) > 0) xbig = x[i];
  172.       if(gcmp(y[i], ysml) < 0) ysml = y[i]; else if(gcmp(y[i], ybig) > 0) ybig = y[i];
  173.       gaddz(t,dt,t);
  174.       avma=av2;
  175.     }
  176.   diffx = gsub(xbig, xsml);
  177.   if(gcmp0(diffx)) {xbig=gaddsg(1,xbig);diffx=gun;}
  178.   diffy = gsub(ybig, ysml);
  179.   if(gcmp0(diffy)) {ybig=gaddsg(1,ybig);diffy=gun;}
  180.   dy = gdivsg(js, diffy);
  181.   dx = gdivsg(is, diffx);
  182.   av2 = avma;
  183.   for(i = 0; i < NBPOINTS; i++)
  184.     {
  185.       points[i].x = IDEC + itos(ground(gmul(gsub(x[i],xsml),dx)));
  186.       points[i].y = JDEC + itos(ground(gmul(gsub(ybig, y[i]),dy)));
  187.       avma=av2;
  188.     }
  189.  
  190.   iz = IDEC - itos(ground(gmul(xsml, dx)));
  191.   jz = JDEC + itos(ground(gmul(ybig, dy)));
  192.  
  193.   p1=cgetr(4); gaffect(ybig,p1); sprintf(c1,"%9.3lf",rtodbl(p1));
  194.   p2=cgetr(4); gaffect(ysml,p2); sprintf(c2,"%9.3lf",rtodbl(p2));
  195.   p3=cgetr(4); gaffect(xsml,p3); sprintf(c3,"%9.3lf",rtodbl(p3));
  196.   p4=cgetr(4); gaffect(xbig,p4); sprintf(c4,"%9.3lf",rtodbl(p4));
  197.  
  198.   win = XCreateSimpleWindow(display, RootWindow(display, screen), 0, 0, width, height, 4,
  199.                             BlackPixel(display, screen), WhitePixel(display, screen));
  200.  
  201.   size_hints.flags = PPosition | PSize;
  202.   size_hints.x = 0;
  203.   size_hints.y = 0;
  204.   size_hints.width = width;
  205.   size_hints.height = height;
  206.  
  207.   XSetStandardProperties(display, win, "ploth2", NULL, NULL, NULL, 0, &size_hints);
  208.  
  209.   XSelectInput(display, win, ExposureMask | ButtonPressMask);
  210.  
  211.   gc = XCreateGC(display, win, 0, NULL);
  212.   XSetFont(display, gc, font_info->fid);
  213.   XSetForeground(display, gc, BlackPixel(display, screen));
  214.  
  215.   XMapWindow(display, win);
  216.  
  217.   for(;;)
  218.     {
  219.       XNextEvent(display, &report);
  220.       if (report.type != Expose) break;
  221.       while (XCheckTypedEvent(display, Expose, &report));
  222.       XDrawRectangle(display, win, gc, IDEC, JDEC, width - IDEC - 5, js);
  223.       XDrawLine(display, win, gc, iz, JDEC, iz, js + JDEC);
  224.       XDrawLine(display, win, gc, IDEC, jz, width - 5, jz);
  225.       XDrawString(display, win, gc, 0, 15, c1, strlen(c1));
  226.       XDrawString(display, win, gc, 0, height - 30, c2, strlen(c2));
  227.       XDrawString(display, win, gc, IDEC - 20, height - 5, c3, strlen(c3));
  228.       XDrawString(display, win, gc, width - 90, height - 5, c4, strlen(c4));
  229.       XDrawLines(display, win, gc, points, NBPOINTS, 0);
  230.     }
  231.   XUnloadFont(display, font_info->fid);
  232.   XFreeGC(display, gc);
  233.   XCloseDisplay(display);
  234.   avma = av;
  235.   killvalue(ep);
  236.   return gnil;
  237. }
  238.     
  239. GEN  plothraw(listx,listy)
  240.      GEN listx,listy;
  241. {
  242.   long av = avma,av2,i,lx,is,js;
  243.   XPoint *points;
  244.   GEN p1,xsml,xbig,ysml,ybig,dx,dy,scal,scaly;
  245.  
  246.   Display *display;
  247.   Window win;
  248.   XSizeHints size_hints;
  249.   XEvent report;
  250.   GC gc;
  251.   XFontStruct *font_info;
  252.   int screen, width, height;
  253.   
  254.   if((typ(listx)<17)||(typ(listx)>18)||(typ(listy)<17)||(typ(listy)>18))
  255.     err(ploter4);
  256.   lx=lg(listx);
  257.   if(lg(listy)!=lx) err(ploter5);
  258.   if(lx==1) return gnil;
  259.   points = (XPoint*)malloc(lx*sizeof(XPoint));
  260.   if(!points) err(ploter6);
  261.   if (!(display = XOpenDisplay(NULL))) err(ploter2);
  262.   if (!(font_info = XLoadQueryFont(display, "9x15"))) err(ploter3);
  263.  
  264.   XSetErrorHandler(xerror);
  265.   XSetIOErrorHandler(xerror);
  266.  
  267.   screen = DefaultScreen(display);
  268.   width = DisplayWidth(display, screen) - 40;
  269.   height = DisplayHeight(display, screen) - 60;
  270.   is = width - IDEC - 5;
  271.   js = height - 25;
  272.   
  273.   av=avma;xsml=xbig=(GEN)listx[1];ysml=ybig=(GEN)listy[1];
  274.   for(i = 0; i < lx-1; i++)
  275.     {
  276.       p1=(GEN)listx[i+1];
  277.       if(gcmp(p1,xsml)<0) xsml=p1;if(gcmp(p1,xbig)>0) xbig=p1;
  278.       p1=(GEN)listy[i+1];
  279.       if(gcmp(p1,ysml)<0) ysml=p1;if(gcmp(p1,ybig)>0) ybig=p1;
  280.     }
  281.   dx=gsub(xbig,xsml);dy=gsub(ybig,ysml);
  282.   if(gcmp0(dx))
  283.     {
  284.       if(gcmp0(dy)) 
  285.     {
  286.       scal=gun;dx=gsub(divis(stoi(is),2),xsml);
  287.       dy=gsub(divis(stoi(js),2),ysml);
  288.     }
  289.       else
  290.     {
  291.       scal=gdivsg(js,dy);
  292.       dx=gneg(gmul(scal,xsml));dy=gneg(gmul(scal,ysml));
  293.     }
  294.     }
  295.   else
  296.     {
  297.       scal=gdivsg(is,dx);
  298.       if(!gcmp0(dy))
  299.     {
  300.       scaly=gdivsg(js,dy);if(gcmp(scaly,scal)<0) scal=scaly;
  301.     }
  302.       dx=gneg(gmul(scal,xsml));dy=gneg(gmul(scal,ysml));
  303.     }
  304.   for(i = 0; i < lx-1; i++)
  305.     {
  306.       av2=avma;
  307.       points[i].x = IDEC + itos(ground(gadd(gmul(listx[i+1],scal),dx)));
  308.       points[i].y = JDEC + itos(ground(gadd(gmul(listy[i+1],scal),dy)));
  309.       avma=av2;
  310.     }
  311.   win = XCreateSimpleWindow(display, RootWindow(display, screen), 0, 0, width, height, 4, BlackPixel(display, screen), WhitePixel(display, screen));
  312.  
  313.   size_hints.flags = PPosition | PSize;
  314.   size_hints.x = 0;
  315.   size_hints.y = 0;
  316.   size_hints.width = width;
  317.   size_hints.height = height;
  318.  
  319.   XSetStandardProperties(display, win, "plothraw", NULL, NULL, NULL, 0, &size_hints);
  320.  
  321.   XSelectInput(display, win, ExposureMask | ButtonPressMask);
  322.  
  323.   gc = XCreateGC(display, win, 0, NULL);
  324.   XSetFont(display, gc, font_info->fid);
  325.   XSetForeground(display, gc, BlackPixel(display, screen)); 
  326.  
  327.   XMapWindow(display, win);
  328.  
  329.   for(;;)
  330.     {
  331.       XNextEvent(display, &report);
  332.       if (report.type != Expose) break;
  333.       while (XCheckTypedEvent(display, Expose, &report));
  334.       XDrawRectangle(display, win, gc, IDEC, JDEC, is, js);
  335.       XDrawPoints(display, win, gc, points, lx-1, 0);
  336.     }
  337.   XUnloadFont(display, font_info->fid);
  338.   XFreeGC(display, gc);
  339.   XCloseDisplay(display);
  340.   free(points);avma = av;
  341.   return gnil;
  342. }
  343.  
  344. GEN rectdraw(list)
  345.      GEN list;
  346. {
  347.   long *e,*p1,*ptx,*pty,*numpoints,*numtexts,*xtexts,*ytexts;
  348.   long n,i,j,x0,y0,av=avma;
  349.   long a,b,nd[10],ne;
  350.   char **texts;
  351.  
  352.   Display *display;
  353.   Window win;
  354.   XSizeHints size_hints;
  355.   XEvent report;
  356.   GC gc;
  357.   XFontStruct *font_info;
  358.   int screen, width, height;
  359.   XPoint *points,**lines;
  360.   XSegment *segments;
  361.   XRectangle *rectangles;
  362.  
  363.   if(typ(list)!=17) err(rploter3);
  364.   n=lg(list)-1;if(n%3) err(rploter4);
  365.   n=n/3;if(!n) return gnil;
  366.   if (!(display = XOpenDisplay(NULL))) err(ploter2);
  367.   if (!(font_info = XLoadQueryFont(display, "9x15"))) err(ploter3);
  368.   XSetErrorHandler(xerror);
  369.   XSetIOErrorHandler(xerror);
  370.   screen = DefaultScreen(display);
  371.   width = DisplayWidth(display, screen) - 40;
  372.   height = DisplayHeight(display, screen) - 60; 
  373.   nd[0]=nd[1]=nd[2]=nd[3]=nd[4]=nd[5]=nd[6]=0;
  374.   for(i=0;i<n;i++)
  375.     {
  376.       if(typ(list[3*i+1])!=1) err(rploter5);
  377.       ne=itos(list[3*i+1]);if((ne<0)||(ne>15)) err(rploter2);
  378.       e=rectgraph[ne];
  379.       p1=(long*)e[0];while((long)p1) 
  380.     {
  381.       if(p1[1]!=4) nd[p1[1]]++;
  382.       else nd[1]+=p1[2];
  383.       p1=(long*)p1[0];
  384.     }
  385.     }
  386.   points=(XPoint*)malloc(nd[1]*sizeof(XPoint));
  387.   segments=(XSegment*)malloc(nd[2]*sizeof(XSegment));
  388.   rectangles=(XRectangle*)malloc(nd[3]*sizeof(XRectangle));
  389.   lines=(XPoint**)malloc(nd[5]*sizeof(XPoint*));
  390.   numpoints=(long*)malloc(nd[5]*sizeof(long));
  391.   texts=(char**)malloc(nd[6]*sizeof(char*));
  392.   numtexts=(long*)malloc(nd[6]*sizeof(long));
  393.   xtexts=(long*)malloc(nd[6]*sizeof(long));
  394.   ytexts=(long*)malloc(nd[6]*sizeof(long));
  395.   nd[1]=nd[2]=nd[3]=nd[5]=nd[6]=0;
  396.   for(i=0;i<n;i++)
  397.     {
  398.       e=rectgraph[itos(list[3*i+1])];x0=list[3*i+2];y0=list[3*i+3];
  399.       if((typ(x0)!=1)||(typ(y0)!=1)) err(rploter5);
  400.       x0=itos(x0);y0=itos(y0);
  401.       p1=(long*)e[0];
  402.       while((long)p1)
  403.     {
  404.       switch(p1[1])
  405.         {
  406.         case 1: 
  407.           points[nd[1]].x=p1[2]+x0;
  408.           points[nd[1]].y=p1[3]+y0;
  409.           nd[1]++;break;
  410.         case 2:
  411.           segments[nd[2]].x1=p1[2]+x0;
  412.           segments[nd[2]].y1=p1[3]+y0;
  413.           segments[nd[2]].x2=p1[4]+x0;
  414.           segments[nd[2]].y2=p1[5]+y0;
  415.           nd[2]++;break;
  416.         case 3:
  417.           a=rectangles[nd[3]].x=p1[2]+x0;
  418.           b=rectangles[nd[3]].y=p1[3]+y0;
  419.           rectangles[nd[3]].width=p1[4]+x0-a;
  420.           rectangles[nd[3]].height=p1[5]+y0-b;
  421.           nd[3]++;break;
  422.         case 4:
  423.           ptx=(long*)p1[3];pty=(long*)p1[4];
  424.           for(j=0;j<p1[2];j++)
  425.         {
  426.           points[nd[1]+j].x=ptx[j]+x0;
  427.           points[nd[1]+j].y=pty[j]+y0;
  428.         }
  429.           nd[1]+=p1[2];break;
  430.         case 5:
  431.           ptx=(long*)p1[3];pty=(long*)p1[4];
  432.           numpoints[nd[5]]=p1[2];
  433.           lines[nd[5]]=(XPoint*)malloc(p1[2]*sizeof(XPoint));
  434.           for(j=0;j<p1[2];j++)
  435.         {
  436.           lines[nd[5]][j].x=ptx[j]+x0;
  437.           lines[nd[5]][j].y=pty[j]+y0;
  438.         }
  439.           nd[5]++;break;
  440.         case 6: 
  441.           texts[nd[6]]=(char*)p1[3];numtexts[nd[6]]=p1[2];
  442.           xtexts[nd[6]]=p1[4]+x0;ytexts[nd[6]]=p1[5]+y0;
  443.           nd[6]++;break;
  444.         default: break;
  445.         }
  446.       p1=(long*)p1[0];
  447.     }
  448.     }
  449.   win= XCreateSimpleWindow(display, RootWindow(display, screen), 0, 0, width, height, 4, BlackPixel(display, screen), WhitePixel(display, screen));
  450.   size_hints.flags = PPosition | PSize;
  451.   size_hints.x = 0;
  452.   size_hints.y = 0;
  453.   size_hints.width = width;
  454.   size_hints.height = height;
  455.   
  456.   XSetStandardProperties(display, win, "rectplot", NULL, NULL, NULL, 0, &size_hints);
  457.   XSelectInput(display, win, ExposureMask | ButtonPressMask);
  458.   gc = XCreateGC(display, win, 0, NULL);
  459.   XSetFont(display, gc, font_info->fid);
  460.   XSetForeground(display, gc, BlackPixel(display, screen)); 
  461.   XMapWindow(display, win);
  462.   for(;;)
  463.     {
  464.       XNextEvent(display, &report);
  465.       if (report.type != Expose) break;
  466.       while (XCheckTypedEvent(display, Expose, &report));
  467.       if(nd[1]) XDrawPoints(display, win, gc, points, nd[1], 0);
  468.       if(nd[2]) XDrawSegments(display, win, gc, segments, nd[2]);
  469.       if(nd[3]) XDrawRectangles(display, win, gc, rectangles, nd[3]);
  470.       for(i=0;i<nd[5];i++) XDrawLines(display,win,gc,lines[i],numpoints[i],0);
  471.       for(i=0;i<nd[6];i++) XDrawString(display,win,gc,xtexts[i],ytexts[i],texts[i],numtexts[i]);
  472.     }
  473.   XUnloadFont(display, font_info->fid);
  474.   XFreeGC(display, gc);
  475.   free(points);free(segments);free(rectangles);
  476.   free(numpoints);for(i=0;i<nd[5];i++) free(lines[i]);
  477.   free(lines);free(texts);free(numtexts);free(xtexts);free(ytexts);
  478.   free(gc);free(win);XCloseDisplay(display);
  479.   avma = av;return gnil;
  480. }
  481.  
  482.