Subversion Repositories wimsdev

Rev

Rev 18356 | Blame | Compare with Previous | Last modification | View Log | RSS feed

  1. /*    Copyright (C) 1998-2003 XIAO, Gang of Universite de Nice - Sophia Antipolis
  2.  *
  3.  *  This program is free software; you can redistribute it and/or modify
  4.  *  it under the terms of the GNU General Public License as published by
  5.  *  the Free Software Foundation; either version 2 of the License, or
  6.  *  (at your option) any later version.
  7.  *
  8.  *  This program is distributed in the hope that it will be useful,
  9.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  10.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  11.  *  GNU General Public License for more details.
  12.  *
  13.  *  You should have received a copy of the GNU General Public License
  14.  *  along with this program; if not, write to the Free Software
  15.  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16.  */
  17.  
  18. #include <errno.h>
  19. #include "flydraw.h"
  20. const double DEG=M_PI/180;
  21.  
  22. /* Tikz things */
  23.  
  24. static char *tikz_color (int the_color)
  25. {
  26.   static char buf[50];
  27.   int r=gdImageRed(image,the_color);
  28.   int g=gdImageGreen(image,the_color);
  29.   int b=gdImageBlue(image,the_color);
  30.   sprintf(buf, "{rgb,255:red,%i;green,%i;blue,%i}",r,g,b);
  31.   return buf;
  32. }
  33.  
  34. static char *tikz_options (int the_color, int the_fill)
  35. {
  36.   static char buf[100];
  37.   if (the_color == gdBrushed)
  38.     the_color = tikz_brushColor;
  39.   sprintf (buf, "draw=%s,cap=round",tikz_color(the_color));
  40.   if (the_color != tikz_brushColor) strcat(buf, ",thin");
  41.   if (the_fill == -1 || the_fill == 2) strcat(buf, ",dashed");
  42.   if (the_fill == 1 || the_fill == 2)
  43.     sprintf(buf+strlen(buf), ",fill=%s", tikz_color(the_color));
  44.   return buf;
  45. }
  46.  
  47. #define flip(y) (sizey-1-(y))
  48.  
  49. /* bug in gdImageFillToBorder */
  50.  
  51. void patchgdImageFillToBorder (gdImagePtr im, int x, int y, int border, int color)
  52. {
  53.   if(x>=im->sx || x<0 || y>=im->sy || y<0) return;
  54.   gdImageFillToBorder(im,x,y,border,color);
  55. }
  56.  
  57. void patchgdImageFill (gdImagePtr im, int x, int y, int color)
  58. {
  59.   if(x>=im->sx || x<0 || y>=im->sy || y<0) return;
  60.   gdImageFill(im,x,y,color);
  61. }
  62.  
  63. #define DASHPIXELS 8
  64.  
  65. void myDashedLine(gdImagePtr im, int x1, int y1, int x2, int y2, int color){
  66.   /**
  67.    * emulates gdImageDashedLine with gdImageSetStyle and gdImageLine
  68.    * GK: As gdImageDashedLine is slightly broken in latest libgd libraries,
  69.    * GK: I implemented an emulation named "myDashedLine"
  70.    **/
  71.  
  72.   int styleDashed[DASHPIXELS],i;
  73.   for (i=0; i< DASHPIXELS/2; i++) styleDashed[i]=color;
  74.   for (; i< DASHPIXELS; i++) styleDashed[i]=gdTransparent;
  75.   gdImageSetStyle(im, styleDashed, DASHPIXELS);
  76.   gdImageLine(im, x1, y1, x2, y2, gdStyled);
  77. }
  78.  
  79.  
  80. /* File opening: with security */
  81. FILE *open4read(char *n)
  82. {
  83.   char *p, *p1, *p2, namebuf[2048];
  84.   int t;
  85.   FILE *f;
  86.   n=find_word_start(n);
  87.   if(*n==0) return NULL;
  88.   p=getenv("flydraw_filebase");
  89.   p1=n+strlen(n)-4;if(p1<n || strcasecmp(p1,".gif")!=0) t=1; else t=0;
  90.   if(p!=NULL && *p!=0) {
  91.     char pbuf[MAX_LINELEN+1];
  92.     snprintf(pbuf,sizeof(pbuf),"%s",p);
  93.     p=find_word_start(pbuf); if(strstr(p,"..")!=NULL) return NULL;
  94.     if(*n=='/' || strstr(n,"..")!=NULL) return NULL;
  95.     /* prohibit unusual file/dir names */
  96.     for(p1=p;*p1;p1++)
  97.       if(!isalnum(*p1) && !isspace(*p1) && strchr("~_-/.",*p1)==NULL)
  98.         return NULL;
  99.     for(p1=n;*p1;p1++)
  100.       if(!isalnum(*p1) && !isspace(*p1) && strchr("~_-/.",*p1)==NULL)
  101.         return NULL;
  102.     f=NULL;
  103.     for(p1=p; *p1; p1=find_word_start(p2)) {
  104.       p2=find_word_end(p1);
  105.       if(*p2) *p2++=0;
  106.       snprintf(namebuf,sizeof(namebuf),"%s/%s",p1,n);
  107.       f=fopen(namebuf,"r"); if(f!=NULL) goto imgtype;
  108.     }
  109.     p1=getenv("w_wims_session");
  110.     if(p1!=NULL && strncmp(n,"insert",6)==0) {
  111.       snprintf(namebuf,sizeof(namebuf),"../s2/%s/%s",p1,n);
  112.       f=fopen(namebuf,"r");
  113.     }
  114.   }
  115.   else {
  116.     snprintf(namebuf,sizeof(namebuf),"%s",n);
  117.     f=fopen(namebuf,"r");
  118.   }
  119.   imgtype:
  120.   if(t && f!=NULL) {
  121.     char tbuf[1024],sbuf[4096];
  122.     fclose(f); f=NULL;
  123.     p1=getenv("TMPDIR"); if(p1==NULL || *p1==0) p1=".";
  124.     snprintf(tbuf,sizeof(tbuf),"%s/drawfile_.gif",p1);
  125.     snprintf(sbuf,sizeof(sbuf),"convert %s %s",namebuf,tbuf);
  126.     if (system(sbuf)) fly_error("system_failed");
  127.       f=fopen(tbuf,"r");
  128.   }
  129.   return f;
  130. }
  131.  
  132. /* Does nothing; just a comment. */
  133. void obj_comment(objparm *pm)
  134. {
  135.   return;
  136. }
  137.  
  138. /* define image size */
  139. void obj_size(objparm *pm)
  140. {
  141.   sizex=rint(pm->pd[0]); sizey=rint(pm->pd[1]);
  142.   if(sizex<0 || sizey<0 || sizex>MAX_SIZE || sizey>MAX_SIZE) {
  143.     fly_error("bad_size"); return;
  144.   }
  145.   if(image!=NULL) {
  146.     fly_error("size_already_defined"); return;
  147.   }
  148.   image=gdImageCreate(sizex,sizey);
  149.   if(tikz_file) fprintf(tikz_file,"\\clip (0,0) rectangle (%i, %i);\n", sizex, sizey);
  150.   if(image==NULL) fly_error("image_creation_failure");
  151.   else {
  152.     color_white=gdImageColorAllocate(image,255,255,255);
  153.     color_black=gdImageColorAllocate(image,0,0,0);
  154.     color_bounder=gdImageColorAllocate(image,1,2,3);
  155.     color_frame=gdImageColorAllocate(image,254,254,254);
  156.   }
  157. }
  158.  
  159. /* new image */
  160. void obj_new(objparm *pm)
  161. {
  162.   if(image) {
  163.     gdImageDestroy(image);image=NULL;
  164.   }
  165.   if(pm->pcnt>=2) { obj_size(pm); gdImageRectangle(image,0,0,sizex-1,sizey-1,color_frame);}
  166.   else sizex=sizey=0;
  167.   saved=0;
  168. }
  169.  
  170. /* new image */
  171. void obj_existing(objparm *pm)
  172. {
  173.   FILE *inf;
  174.   char *pp;
  175.  
  176.   if(image) {
  177.     gdImageDestroy(image);image=NULL;
  178.   }
  179.   pp=find_word_start(pm->str);*find_word_end(pp)=0;
  180.   inf=open4read(pp);
  181.   if(inf==NULL) {
  182.     fly_error("file_not_exist"); return;
  183.   }
  184.   image=gdImageCreateFromGif(inf); fclose(inf);
  185.   if(image==NULL) {
  186.     fly_error("bad_gif"); return;
  187.   }
  188.   sizex=image->sx; sizey=image->sy;
  189.   saved=0;
  190. }
  191. /* tikzfile */
  192. void obj_tikzfile(objparm *pm)
  193. {
  194.   char *p;
  195.   if(tikz_file){
  196.     fprintf(tikz_file,"\\end{tikzpicture}\n");
  197.     fclose(tikz_file);
  198.   }
  199.   p=find_word_start(pm->str); *find_word_end(p)=0;
  200.   snprintf(tikzfilename,sizeof(tikzfilename),"%s",p);
  201.   tikz_file=fopen(tikzfilename,"w");
  202.   if(!tikz_file)
  203.     fly_error("tikz_nopen");
  204.   fprintf(tikz_file,"\\begin{tikzpicture}\[x=0.02cm, y=0.02cm]\n");
  205. }
  206.  
  207. /* segment */
  208. void obj_line(objparm *pm)
  209. {
  210.   scale(pm->pd,pm->p,2);
  211.   if(pm->fill==-1 || pm->fill==2 )
  212.     myDashedLine(image,pm->p[0],pm->p[1],pm->p[2],pm->p[3],pm->color[0]);
  213.   else
  214.     gdImageLine(image,pm->p[0],pm->p[1],pm->p[2],pm->p[3],pm->color[0]);
  215.   if (tikz_file)
  216.     fprintf(tikz_file, "\\draw\[%s] (%i, %i) -- (%i, %i);\n",
  217.       tikz_options(pm->color[0],pm->fill),pm->p[0],flip(pm->p[1]),pm->p[2],flip(pm->p[3]));
  218.   if(vimg_enable) vimg_line(scale_buf[0],scale_buf[1],scale_buf[2],scale_buf[3]);
  219. }
  220.  
  221. void _obj_arrow(objparm *pm, int twoside)
  222. {
  223.   int l,xx,yy;
  224.   gdPoint ii[3];
  225.   double dx,dy,length,dd[6];
  226.   scale(pm->pd,pm->p,2);
  227.   xx=ii[0].x=pm->p[2];yy=ii[0].y=pm->p[3];
  228.   l=pm->pd[4];if(l<0) l=0; if(l>200) l=200;
  229.   scale2(pm->pd[0]-pm->pd[2],pm->pd[1]-pm->pd[3],&dx,&dy);
  230.   length=sqrt(dx*dx+dy*dy);
  231.   if(length<3 || l<5) goto stem;
  232.   dd[0]=l*dx/length; dd[1]=l*dy/length;
  233.   #define fat 0.27
  234.   dd[2]=dd[0]+dd[1]*fat; dd[3]=dd[1]-dd[0]*fat;
  235.   dd[4]=dd[0]-dd[1]*fat; dd[5]=dd[1]+dd[0]*fat;
  236.   ii[1].x=rint(dd[2])+ii[0].x; ii[1].y=rint(dd[3])+ii[0].y;
  237.   ii[2].x=rint(dd[4])+ii[0].x; ii[2].y=rint(dd[5])+ii[0].y;
  238.   gdImageFilledPolygon(image, ii,3,pm->color[0]);
  239.   if(tikz_file)
  240.     fprintf(tikz_file, "\\draw\[%s] (%i, %i) -- (%i, %i) -- (%i, %i) -- cycle;\n",
  241.       tikz_options(pm->color[0],1),ii[0].x,flip(ii[0].y),ii[1].x,flip(ii[1].y),ii[2].x,flip(ii[2].y));
  242.   if(vimg_enable) vimg_polyline(scale_buf,3,1);
  243.   xx=rint(dd[0])+ii[0].x;yy=rint(dd[1])+ii[0].y;
  244.   if(twoside) {
  245.     ii[0].x=pm->p[0]; ii[0].y=pm->p[1];
  246.     ii[1].x=-rint(dd[2])+ii[0].x; ii[1].y=-rint(dd[3])+ii[0].y;
  247.     ii[2].x=-rint(dd[4])+ii[0].x; ii[2].y=-rint(dd[5])+ii[0].y;
  248.     gdImageFilledPolygon(image, ii,3,pm->color[0]);
  249.   if(tikz_file)
  250.     fprintf(tikz_file, "\\draw\[%s] (%i, %i) -- (%i, %i) -- (%i, %i) -- cycle;\n",
  251.       tikz_options(pm->color[0],1),ii[0].x,flip(ii[0].y),ii[1].x,flip(ii[1].y),ii[2].x,flip(ii[2].y));
  252.   }
  253.   stem: if(pm->fill==-1 || pm->fill==2)
  254.     myDashedLine(image,pm->p[0],pm->p[1],xx,yy,pm->color[0]);
  255.   else
  256.     gdImageLine(image,pm->p[0],pm->p[1],xx,yy,pm->color[0]);
  257.   if(tikz_file)
  258.     fprintf(tikz_file, "\\draw\[%s] (%i, %i) -- (%i, %i);\n",
  259.       tikz_options(pm->color[0],pm->fill),pm->p[0],flip(pm->p[1]),xx,flip(yy));
  260.   if(vimg_enable) vimg_line(scale_buf[0],scale_buf[1],scale_buf[2],scale_buf[3]);
  261. }
  262.  
  263. /* Arrow */
  264. void obj_arrow(objparm *pm)
  265. {
  266.   _obj_arrow(pm,0);
  267. }
  268.  
  269. /* 2-sided arrow */
  270. void obj_arrow2(objparm *pm)
  271. {
  272.    _obj_arrow(pm,1);
  273. }
  274.  
  275. void _obj_arrows(objparm *pm, int twoside)
  276. {
  277.   #define fat 0.27
  278.   int i,l,xx,yy;
  279.   gdPoint ii[3];
  280.   double dx,dy,length,dd[6];
  281.   l=pm->pd[0];if(l<0) l=0; if(l>200) l=200;
  282.   scale(pm->pd+1,pm->p+1,pm->pcnt/2);
  283.   for (i=1;i<pm->pcnt;i+=4){
  284.     xx=ii[0].x=pm->p[i+2];yy=ii[0].y=pm->p[i+3];
  285.     scale2(pm->pd[i]-pm->pd[i+2],pm->pd[i+1]-pm->pd[i+3],&dx,&dy);
  286.     length=sqrt(dx*dx+dy*dy);
  287.     if(length<3 || l<5) goto stem;
  288.     dd[0]=l*dx/length; dd[1]=l*dy/length;
  289.     dd[2]=dd[0]+dd[1]*fat; dd[3]=dd[1]-dd[0]*fat;
  290.     dd[4]=dd[0]-dd[1]*fat; dd[5]=dd[1]+dd[0]*fat;
  291.     ii[1].x=rint(dd[2])+ii[0].x; ii[1].y=rint(dd[3])+ii[0].y;
  292.     ii[2].x=rint(dd[4])+ii[0].x; ii[2].y=rint(dd[5])+ii[0].y;
  293.     gdImageFilledPolygon(image, ii,3,pm->color[0]);
  294.     if(tikz_file)
  295.   fprintf(tikz_file, "\\draw\[%s] (%i, %i) -- (%i, %i) -- (%i, %i) -- cycle;\n",
  296.         tikz_options(pm->color[0],1),ii[0].x,flip(ii[0].y),ii[1].x,flip(ii[1].y),ii[2].x,flip(ii[2].y));
  297.       xx=rint(dd[0])+ii[0].x;yy=rint(dd[1])+ii[0].y;
  298.       if(twoside) {
  299.   ii[0].x=pm->p[i]; ii[0].y=pm->p[i+1];
  300.   ii[1].x=-rint(dd[2])+ii[0].x; ii[1].y=-rint(dd[3])+ii[0].y;
  301.   ii[2].x=-rint(dd[4])+ii[0].x; ii[2].y=-rint(dd[5])+ii[0].y;
  302.   gdImageFilledPolygon(image, ii,3,pm->color[0]);
  303.   if(tikz_file)
  304.     fprintf(tikz_file, "\\draw\[%s] (%i, %i) -- (%i, %i) -- (%i, %i) -- cycle;\n",
  305.         tikz_options(pm->color[0],1),ii[0].x,flip(ii[0].y),ii[1].x,flip(ii[1].y),ii[2].x,flip(ii[2].y));
  306.       }
  307.     stem: if(pm->fill==-1 || pm->fill==2)
  308.   myDashedLine(image,pm->p[i],pm->p[i+1],xx,yy,pm->color[0]);
  309.       else
  310.   gdImageLine(image,pm->p[i],pm->p[i+1],xx,yy,pm->color[0]);
  311.       if(tikz_file)
  312.   fprintf(tikz_file, "\\draw\[%s] (%i, %i) -- (%i, %i);\n",
  313.     tikz_options(pm->color[0],pm->fill),pm->p[i],flip(pm->p[i+1]),xx,flip(yy));
  314.     }
  315. }
  316.  
  317. /* Arrows */
  318. void obj_arrows(objparm *pm)
  319. {
  320.   _obj_arrows(pm,0);
  321. }
  322.  
  323. /* 2-sided arrows */
  324. void obj_arrows2(objparm *pm)
  325. {
  326.    _obj_arrows(pm,1);
  327. }
  328.  
  329. /* horizontal line */
  330. void obj_hline(objparm *pm)
  331. {
  332.   scale(pm->pd,pm->p,1);
  333.   if(pm->fill==-1 || pm->fill==2)
  334.     myDashedLine(image,0,pm->p[1],sizex,pm->p[1],pm->color[0]);
  335.   else
  336.     gdImageLine(image,0,pm->p[1],sizex,pm->p[1],pm->color[0]);
  337.   if (tikz_file)
  338.     fprintf(tikz_file, "\\draw\[%s] (%i, %i) -- (%i, %i);\n",
  339.       tikz_options(pm->color[0],pm->fill),0,flip(pm->p[1]),sizex,flip(pm->p[1]));
  340. }
  341.  
  342. static void line_extend(int x1, int y1, int x2, int y2, int* xend, int *yend)
  343. {
  344.   double t1, t2, t3;
  345.   if (x2 != x1){t1 = (x2 > x1) ? sizex : 0; t1 = (t1 - x1)/(x2 - x1);}
  346.   else t1 = INFINITY;
  347.   if (y2 != y1){t2 = (y2 > y1) ? sizey : 0; t2 = (t2 - y1)/(y2 - y1);}
  348.   else t2 = INFINITY;
  349.   t3 = (t1 < t2) ? t1 : t2;
  350.   *xend = x1+t3*(x2-x1);
  351.   *yend = y1+t3*(y2-y1);
  352. }
  353.  
  354. /* halfline */
  355. void obj_halfline(objparm *pm)
  356. {
  357.   int xend,yend;
  358.   scale(pm->pd,pm->p,2);
  359.   line_extend(pm->p[0],pm->p[1],pm->p[2],pm->p[3],&xend,&yend);
  360.   if(pm->fill==-1 || pm->fill==2)
  361.     myDashedLine(image,pm->p[0],pm->p[1],xend,yend,pm->color[0]);
  362.   else
  363.     gdImageLine(image,pm->p[0],pm->p[1],xend,yend,pm->color[0]);
  364.   if (tikz_file)
  365.     fprintf(tikz_file, "\\draw\[%s] (%i, %i) -- (%i, %i);\n",
  366.       tikz_options(pm->color[0],pm->fill),
  367.       pm->p[0],flip(pm->p[1]),xend,flip(yend));
  368. }
  369.  
  370. void obj_fullline(objparm *pm)
  371. {
  372.   int xstart,ystart,xend,yend;
  373.   scale(pm->pd,pm->p,2);
  374.   line_extend(pm->p[0],pm->p[1],pm->p[2],pm->p[3],&xend,&yend);
  375.   line_extend(pm->p[2],pm->p[3],pm->p[0],pm->p[1],&xstart,&ystart);
  376.   if(pm->fill==-1 || pm->fill==2)
  377.     myDashedLine(image,xstart,ystart,xend,yend,pm->color[0]);
  378.   else
  379.     gdImageLine(image,xstart,ystart,xend,yend,pm->color[0]);
  380.   if (tikz_file)
  381.     fprintf(tikz_file, "\\draw\[%s] (%i, %i) -- (%i, %i);\n",
  382.       tikz_options(pm->color[0],pm->fill),xstart,flip(ystart),xend,flip(yend));
  383. }
  384.  
  385. void obj_fulllines(objparm *pm)
  386. {
  387.   int i,xstart,ystart,xend,yend;
  388.   scale(pm->pd,pm->p,pm->pcnt/2);
  389.   if (tikz_file)
  390.       fprintf(tikz_file, "\\draw\[%s]",tikz_options(pm->color[0],pm->fill));
  391.   for (i=0;i<pm->pcnt;i+=4){
  392.     line_extend(pm->p[i],pm->p[i+1],pm->p[i+2],pm->p[i+3],&xend,&yend);
  393.     line_extend(pm->p[i+2],pm->p[i+3],pm->p[i],pm->p[i+1],&xstart,&ystart);
  394.     if(pm->fill==-1 || pm->fill==2)
  395.       myDashedLine(image,xstart,ystart,xend,yend,pm->color[0]);
  396.     else
  397.       gdImageLine(image,xstart,ystart,xend,yend,pm->color[0]);
  398.     if (tikz_file)
  399.       fprintf(tikz_file, "(%i,%i)--(%i, %i)",xstart,flip(ystart),xend,flip(yend));
  400.   }
  401.   if (tikz_file)
  402.       fprintf(tikz_file, ";\n");
  403. }
  404.  
  405. /* vertical line */
  406. void obj_vline(objparm *pm)
  407. {
  408.   scale(pm->pd,pm->p,1);
  409.   if(pm->fill==-1 || pm->fill==2)
  410.     myDashedLine(image,pm->p[0],0,pm->p[0],sizey,pm->color[0]);
  411.   else
  412.     gdImageLine(image,pm->p[0],0,pm->p[0],sizey,pm->color[0]);
  413.   if (tikz_file)
  414.     fprintf(tikz_file, "\\draw\[%s] (%i, %i) -- (%i, %i);\n",
  415.       tikz_options(pm->color[0],pm->fill),pm->p[0],0,pm->p[0],sizey);
  416. }
  417.  
  418. /* dashed line */
  419. void obj_dline(objparm *pm)
  420. {
  421.   scale(pm->pd,pm->p,2);
  422.   myDashedLine(image,pm->p[0],pm->p[1],pm->p[2],pm->p[3], pm->color[0]);
  423.   if (tikz_file)
  424.     fprintf(tikz_file, "\\draw\[%s] (%i, %i) -- (%i, %i);\n",
  425.       tikz_options(pm->color[0],-1),
  426.       pm->p[0],flip(pm->p[1]),pm->p[2],flip(pm->p[3]));
  427. }
  428.  
  429. /* parallel lines.
  430.  * x1,y1,x2,y2,xv,yv,n,color */
  431. void obj_parallel(objparm *pm)
  432. {
  433.   int i, n, xi,yi;
  434.   double xv,yv;
  435.   n=pm->pd[6]; if(n<0) return; if(n>256) n=256;
  436.   scale(pm->pd,pm->p,3);
  437.   scale2(pm->pd[4],pm->pd[5],&xv,&yv);
  438.   if (tikz_file)
  439.     fprintf(tikz_file, "\\draw\[%s]", tikz_options(pm->color[0],pm->fill));
  440.   for(i=0;i<n;i++) {
  441.     xi=rint(i*xv); yi=rint(i*yv);
  442.     if(pm->fill==-1 || pm->fill==2)
  443.       myDashedLine(image,pm->p[0]+xi,pm->p[1]+yi,pm->p[2]+xi,pm->p[3]+yi,
  444.         pm->color[0]);
  445.     else
  446.       gdImageLine(image,pm->p[0]+xi,pm->p[1]+yi,pm->p[2]+xi,pm->p[3]+yi,
  447.         pm->color[0]);
  448.     if (tikz_file)
  449.       fprintf(tikz_file, "(%i,%i)--(%i, %i)",
  450.         pm->p[0]+xi,flip(pm->p[1]+yi),pm->p[2]+xi,flip(pm->p[3]+yi));
  451.     if(vimg_enable) vimg_line(scale_buf[0]+i*(scale_buf[4]-transx),
  452.       scale_buf[1]+i*(scale_buf[5]-transy),
  453.       scale_buf[2]+i*(scale_buf[4]-transx),
  454.       scale_buf[3]+i*(scale_buf[5]-transy));
  455.   }
  456.  if(tikz_file) fprintf(tikz_file,";\n");
  457. }
  458.  
  459. /* rectangle */
  460. void obj_rect(objparm *pm)
  461. {
  462.   int x1,y1,x2,y2;
  463.   scale(pm->pd,pm->p,2);
  464.   x1=min(pm->p[0],pm->p[2]); x2=max(pm->p[0],pm->p[2]);
  465.   y1=min(pm->p[1],pm->p[3]); y2=max(pm->p[1],pm->p[3]);
  466.   if(pm->fill!=0){
  467.     if(pm->fill==-1 || pm->fill==2){
  468.       myDashedLine(image,x1,y1,x1,y2,pm->color[0]);
  469.       myDashedLine(image,x1,y2,x2,y2,pm->color[0]);
  470.       myDashedLine(image,x2,y2,x2,y1,pm->color[0]);
  471.       myDashedLine(image,x2,y1,x1,y1,pm->color[0]);
  472.     }
  473.     if(pm->fill==1 || pm->fill==2)
  474.       gdImageFilledRectangle(image,x1,y1,x2,y2,pm->color[0]);
  475.   }
  476.   else
  477.     gdImageRectangle(image,x1,y1,x2,y2,pm->color[0]);
  478.   if(tikz_file)
  479.     fprintf(tikz_file,
  480.       "\\draw\[%s] (%i,%i) rectangle (%i,%i);\n",
  481.       tikz_options(pm->color[0],pm->fill),x1,flip(y1),x2,flip(y2));
  482.   if(vimg_enable) vimg_rect(scale_buf[0],scale_buf[1],scale_buf[2],scale_buf[3]);
  483. }
  484.  
  485. /* square */
  486. void obj_square(objparm *pm)
  487. {
  488.   int w,h;
  489.   scale(pm->pd,pm->p,1);
  490.   w=rint(pm->pd[2]); h=rint(pm->pd[2]);
  491.   if(pm->fill!=0){
  492.     if(pm->fill==-1 || pm->fill==2){
  493.       myDashedLine(image,pm->p[0],pm->p[1],pm->p[0]+w,pm->p[1],pm->color[0]);
  494.       myDashedLine(image,pm->p[0]+w,pm->p[1],pm->p[0]+w,pm->p[1]+h,pm->color[0]);
  495.       myDashedLine(image,pm->p[0]+w,pm->p[1]+h,pm->p[0],pm->p[1]+h,pm->color[0]);
  496.       myDashedLine(image,pm->p[0],pm->p[1]+h,pm->p[0],pm->p[1],pm->color[0]);
  497.     }
  498.     if(pm->fill==1 || pm->fill==2)
  499.     gdImageFilledRectangle(image,pm->p[0],pm->p[1],
  500.         pm->p[0]+w,pm->p[1]+h,pm->color[0]);
  501.   }
  502.   else
  503.     gdImageRectangle(image,pm->p[0],pm->p[1],
  504.                pm->p[0]+w,pm->p[1]+h,pm->color[0]);
  505.   if(tikz_file){
  506.     fprintf(tikz_file,
  507.       "\\draw\[%s] (%i,%i) rectangle (%i,%i);\n",
  508.       tikz_options(pm->color[0],pm->fill),pm->p[0],flip(pm->p[1]),
  509.       pm->p[0]+w,flip(pm->p[1]+h));
  510.   }
  511.   if(vimg_enable) vimg_rect(scale_buf[0],scale_buf[1],
  512.                       scale_buf[0]+pm->pd[2],scale_buf[1]+pm->pd[2]);
  513. }
  514.  
  515. /* triangle */
  516. void obj_triangle(objparm *pm)
  517. {
  518.   scale(pm->pd,pm->p,3);
  519.   int i,n=2;
  520.   if(pm->fill!=0){
  521.     if(pm->fill==-1 || pm->fill==2){
  522.       for(i=0;i<n;i+=1)
  523.         myDashedLine(image,pm->p[2*i],pm->p[2*i+1],pm->p[2*i+2],pm->p[2*i+3],pm->color[0]);
  524.       myDashedLine(image,pm->p[2*n],pm->p[2*n+1],pm->p[0],pm->p[1],pm->color[0]);
  525.     }
  526.     if(pm->fill==1 || pm->fill==2)
  527.       gdImageFilledPolygon(image,(gdPointPtr) pm->p,3,pm->color[0]);
  528.   }
  529.   else
  530.     gdImagePolygon(image,(gdPointPtr) pm->p,3,pm->color[0]);
  531.   if (tikz_file)
  532.     fprintf(tikz_file, "\\draw\[%s] (%i, %i) -- (%i, %i) -- (%i, %i) -- cycle;\n",
  533.       tikz_options(pm->color[0],pm->fill),pm->p[0],flip(pm->p[1]),pm->p[2],flip(pm->p[3]),pm->p[4],flip(pm->p[5]));
  534.   if(vimg_enable) vimg_polyline(scale_buf,3,1);
  535. }
  536.  
  537. /* polygon */
  538. void obj_poly(objparm *pm)
  539. {
  540.   int cnt,i;
  541.   cnt=(pm->pcnt)/2;
  542.   scale(pm->pd,pm->p,cnt);
  543.   if(pm->fill==1 || pm->fill==2)
  544.     gdImageFilledPolygon(image,(gdPointPtr) pm->p,cnt,pm->color[0]);
  545.   else
  546.     gdImagePolygon(image,(gdPointPtr) pm->p,cnt,pm->color[0]);
  547.   if(tikz_file){
  548.     fprintf(tikz_file,"\\draw\[%s] (%i,%i) --",tikz_options(pm->color[0],pm->fill),pm->p[0],flip(pm->p[1]));
  549.     for (i= 1; i<cnt; i++)
  550.         fprintf(tikz_file,"(%i,%i)--", pm->p[2*i],flip(pm->p[2*i+1]));
  551.     fprintf(tikz_file," cycle;\n");
  552.   }
  553.   if(vimg_enable) vimg_polyline(scale_buf,cnt,1);
  554. }
  555.  
  556. /* rays */
  557. void obj_rays(objparm *pm)
  558. {
  559.   int i, n;
  560.   n=(pm->pcnt)/2;
  561.   scale(pm->pd,pm->p,n);
  562.   if (tikz_file)
  563.     fprintf(tikz_file, "\\draw\[%s]",tikz_options(pm->color[0],pm->fill));
  564.   for(i=2;i<2*n;i+=2) {
  565.     if(pm->fill==-1 || pm->fill==2)
  566.       myDashedLine(image,pm->p[0],pm->p[1],pm->p[i],pm->p[i+1],pm->color[0]);
  567.     else
  568.       gdImageLine(image,pm->p[0],pm->p[1],pm->p[i],pm->p[i+1],pm->color[0]);
  569.     if (tikz_file)
  570.       fprintf(tikz_file, "(%i,%i) -- (%i,%i)",
  571.         pm->p[0],flip(pm->p[1]),pm->p[i],flip(pm->p[i+1]));
  572.     if(vimg_enable) vimg_line(scale_buf[0],scale_buf[1],
  573.                         scale_buf[i],scale_buf[i+1]);
  574.   }
  575.   if (tikz_file) fprintf(tikz_file,";\n");
  576. }
  577. /* crosshair */
  578. void obj_crosshair(objparm *pm)
  579. {
  580.   scale(pm->pd,pm->p,2);
  581.   gdImageLine(image,pm->p[0]+width2,pm->p[1]+width2,pm->p[0]-width2,pm->p[1]-width2,pm->color[0]);
  582.   gdImageLine(image,pm->p[0]-width2,pm->p[1]+width2,pm->p[0]+width2,pm->p[1]-width2,pm->color[0]);
  583.   if (tikz_file){
  584.     fprintf(tikz_file, "\\draw\[%s] (%i, %i) -- (%i, %i);\n",
  585.       tikz_options(pm->color[0],pm->fill),pm->p[0]+width2,flip(pm->p[1]+width2),pm->p[0]-width2,flip(pm->p[1]-width2));
  586.     fprintf(tikz_file, "\\draw\[%s] (%i, %i) -- (%i, %i);\n",
  587.       tikz_options(pm->color[0],pm->fill),pm->p[0]-width2,flip(pm->p[1]+width2),pm->p[0]+width2,flip(pm->p[1]-width2));
  588.   }
  589. }
  590. /* crosshairs */
  591.  
  592. void obj_crosshairs(objparm *pm)
  593. {
  594.   int i, n;
  595.   n=(pm->pcnt)/2;
  596.   scale(pm->pd,pm->p,n);
  597.   if (tikz_file)
  598.       fprintf(tikz_file, "\\draw\[%s]",tikz_options(pm->color[0],pm->fill));
  599.   for(i=0;i<2*n;i+=2) {
  600.     gdImageLine(image,pm->p[i]+width2,pm->p[i+1]+width2,pm->p[i]-width2,pm->p[i+1]-width2,pm->color[0]);
  601.     gdImageLine(image,pm->p[i]-width2,pm->p[i+1]+width2,pm->p[i]+width2,pm->p[i+1]-width2,pm->color[0]);
  602.     if (tikz_file){
  603.       fprintf(tikz_file, "(%i, %i) -- (%i, %i) ",
  604.         pm->p[i]+width2,flip(pm->p[i+1]+width2),pm->p[i]-width2,flip(pm->p[i+1]-width2));
  605.       fprintf(tikz_file, "(%i, %i) -- (%i, %i)",
  606.         pm->p[i]-width2,flip(pm->p[i+1]+width2),pm->p[i]+width2,flip(pm->p[i+1]-width2));
  607.     }
  608.   }
  609.   if (tikz_file) fprintf(tikz_file,";\n");
  610. }
  611.  
  612.  
  613. /* segments */
  614. void obj_lines(objparm *pm)
  615. {
  616.  int i, n;
  617.  n=(pm->pcnt)/2;
  618.  scale(pm->pd,pm->p,n);
  619.  if (tikz_file){
  620.    fprintf(tikz_file,"\\draw\[%s] (%i,%i)",
  621.      tikz_options(pm->color[0],pm->fill),pm->p[0],flip(pm->p[1]));
  622.    for(i=2;i<2*n;i+=2)
  623.      fprintf(tikz_file, "--(%i,%i)",pm->p[i],flip(pm->p[i+1]));
  624.    fprintf(tikz_file, ";\n");
  625.  }
  626.  for(i=2;i<2*n;i+=2){
  627.   if(pm->fill==-1 || pm->fill==2 )
  628.     myDashedLine(image,pm->p[i-2],pm->p[i-1],pm->p[i],pm->p[i+1],pm->color[0]);
  629.   else
  630.     gdImageLine(image,pm->p[i-2],pm->p[i-1],pm->p[i],pm->p[i+1],pm->color[0]);
  631.   if(vimg_enable) vimg_polyline(scale_buf,n,0);
  632.   }
  633. }
  634. /*segments from x1,y1 to x2,y2, x3,y3 to x4,y4, etc */
  635. void obj_segments(objparm *pm)
  636. {
  637.   int i, n;
  638.   n=(pm->pcnt)/2;
  639.   scale(pm->pd,pm->p,n);
  640.   if (tikz_file)
  641.     fprintf(tikz_file, "\\draw\[%s]",tikz_options(pm->color[0],pm->fill));
  642.   for(i=0;i<2*n;i+=4){
  643.     if(pm->fill==-1 || pm->fill==2)
  644.       myDashedLine(image,pm->p[i],pm->p[i+1],pm->p[i+2],pm->p[i+3],pm->color[0]);
  645.     else
  646.       gdImageLine(image,pm->p[i],pm->p[i+1],pm->p[i+2],pm->p[i+3],pm->color[0]);
  647.     if (tikz_file)
  648.       fprintf(tikz_file, "(%i,%i)--(%i,%i)",
  649.        pm->p[i],flip(pm->p[i+1]),pm->p[i+2],flip(pm->p[i+3]));
  650.   }
  651.   if(tikz_file) fprintf(tikz_file, ";\n");
  652. }
  653.  
  654. /* segments */
  655. void obj_dlines(objparm *pm)
  656. {
  657.  int i, n;
  658.  n=(pm->pcnt)/2;
  659.  scale(pm->pd,pm->p,n);
  660.  if (tikz_file){
  661.    fprintf(tikz_file,"\\draw\[%s] (%i,%i)",
  662.      tikz_options(pm->color[0],pm->fill),pm->p[0],flip(pm->p[1]));
  663.    for(i=2;i<2*n;i+=2)
  664.      fprintf(tikz_file, "--(%i,%i)",pm->p[i],flip(pm->p[i+1]));
  665.    fprintf(tikz_file, ";\n");
  666.  }
  667.  for(i=2;i<2*n;i+=2)
  668.    myDashedLine(image,pm->p[i-2],pm->p[i-1],pm->p[i],pm->p[i+1],pm->color[0]);
  669.  if(vimg_enable) vimg_polyline(scale_buf,n,0);
  670. }
  671.  
  672. /* points */
  673. void obj_points(objparm *pm)
  674. {
  675.  int i, n;
  676.  n=(pm->pcnt)/2;
  677.  scale(pm->pd,pm->p,n);
  678.  for(i=0;i<2*n;i+=2) gdImageSetPixel(image,pm->p[i],pm->p[i+1],pm->color[0]);
  679.  if(tikz_file) {
  680.    fprintf(tikz_file, "\\draw\[%s]", tikz_options(pm->color[0],pm->fill));
  681.    for(i=0;i<2*n;i+=2)
  682.      fprintf(tikz_file,"(%i,%i)circle(1pt)",pm->p[i],flip(pm->p[i+1]));
  683.    fprintf(tikz_file,";\n");
  684.  }
  685. }
  686.  
  687. /* lattice.
  688.  * x0,y0,xv1,yv1,xv2,yv2,n1,n2,color */
  689. void obj_lattice(objparm *pm)
  690. {
  691.  int n1,n2,i1,i2,xi1,yi1,xi2,yi2;
  692.  double xv1,xv2,yv1,yv2;
  693.  n1=pm->pd[6];n2=pm->pd[7]; if(n1<0 || n2<0) return;
  694.  if(n1>256) n1=256;
  695.  if(n2>256) n2=256;
  696.  scale(pm->pd,pm->p,1);
  697.  scale2(pm->pd[2],pm->pd[3],&xv1,&yv1);
  698.  scale2(pm->pd[4],pm->pd[5],&xv2,&yv2);
  699.  if(tikz_file)
  700.    fprintf(tikz_file,"\\draw[%s]", tikz_options(pm->color[0],0));
  701.  for(i1=0;i1<n1;i1++) {
  702.    xi1=rint(i1*xv1)+pm->p[0]; yi1=rint(i1*yv1)+pm->p[1];
  703.    for(i2=0;i2<n2;i2++) {
  704.      xi2=i2*xv2+xi1;yi2=i2*yv2+yi1;
  705.      gdImageSetPixel(image,xi2,yi2,pm->color[0]);
  706.      if(tikz_file) fprintf(tikz_file,"(%i,%i)circle(1pt)",xi2,flip(yi2));
  707.    }
  708.  }
  709.  if(tikz_file) fprintf(tikz_file,";\n");
  710. }
  711.  
  712. /* arc */
  713. /*trouble with gdImageArc as the angles are of type int */
  714. void myGdImageArc(gdImagePtr im, double cx, double cy, double rx, double ry, double t1, double t2, int color)
  715. {
  716.   int i,nb;
  717.   double dt, r=rx>ry?rx:ry;
  718.   t2-=t1;
  719.   t2-=360*floor(t2/360);
  720.   if(t2==0) t2=360;
  721.   t1*=DEG;t2*=DEG;
  722.   nb = r*t2/3;
  723.   dt = t2/nb;
  724.   for (i=0; i<nb; ++i,t1+=dt)
  725.     gdImageLine(im,rint(cx+rx*cos(t1)),rint(cy+ry*sin(t1)),
  726.     rint(cx+rx*cos(t1+dt)),rint(cy+ry*sin(t1+dt)),color);
  727. }
  728.  
  729. void obj_arc(objparm *pm)
  730. {
  731.   scale(pm->pd,pm->p,1);
  732.   int rx=pm->pd[2]*xscale/2, ry=pm->pd[3]*yscale/2;
  733.   myGdImageArc(image, pm->p[0], pm->p[1], rx, ry, pm->pd[4],pm->pd[5],pm->color[0]);
  734.   if(tikz_file){
  735.     pm->pd[5]-=pm->pd[4];
  736.     pm->pd[5]-=360*floor(pm->pd[5]/360);
  737.     pm->pd[5]+=pm->pd[4];
  738.     fprintf(tikz_file,
  739.       "\\draw \[%s] (%i,%i) arc (%f:%f:%i and %i);\n",
  740.         tikz_options(pm->color[0],pm->fill),
  741.         (int)rint(pm->p[0]+rx*cos(DEG*pm->pd[4])),
  742.         flip((int)rint(pm->p[1]+ry*sin(DEG*pm->pd[4]))),
  743.         pm->pd[4],pm->pd[5],rx,-ry);
  744.   }
  745.   /*FIXME echelle mauvaise*/
  746.   if(vimg_enable) vimg_arc(scale_buf[0],scale_buf[1],
  747.                      0.5*pm->pd[2],0.5*pm->pd[3],pm->pd[4],pm->pd[5]);
  748. }
  749.  
  750. void obj_angle(objparm *pm)
  751. {
  752. /* dans la doc: mettre les angles dans l'ordre ???? demander qu'ils soient entre 0 et 360 */
  753.   double dpts[6];
  754.   int pts[6], wx=rint(2*pm->pd[2]*xscale), wy=rint(2*pm->pd[2]*yscale);
  755.   dpts[0]=pm->pd[0];
  756.   dpts[1]=pm->pd[1];
  757.   dpts[2]=pm->pd[0]+pm->pd[2]*cos(DEG*pm->pd[3]);
  758.   dpts[3]=pm->pd[1]+pm->pd[2]*sin(DEG*pm->pd[3]);
  759.   dpts[4]=pm->pd[0]+pm->pd[2]*cos(DEG*pm->pd[4]);
  760.   dpts[5]=pm->pd[1]+pm->pd[2]*sin(DEG*pm->pd[4]);
  761.   scale(dpts, pts, 3);
  762.   gdImageLine(image,pts[0],pts[1],pts[2],pts[3],pm->color[0]);
  763.   gdImageLine(image,pts[0],pts[1],pts[4],pts[5],pm->color[0]);
  764.   gdImageArc(image,pts[0],pts[1],wx,wy,pm->pd[3],pm->pd[4],pm->color[0]);
  765.   if(tikz_file) {
  766.     fprintf(tikz_file, "\\draw\[%s, domain=%f:%f] plot ({%i+%f*cos(\\x)}, {%i+%f*sin(\\x)});\n",
  767.       tikz_options(pm->color[0],pm->fill),pm->pd[3],pm->pd[4],pts[0],wx*0.5,flip(pts[1]),wy*(-0.5));
  768.     fprintf(tikz_file, "\\draw\[%s] (%i, %i) -- (%i, %i) (%i, %i) -- (%i, %i);\n",
  769.         tikz_options(pm->color[0],pm->fill),pts[0],flip(pts[1]),pts[2],flip(pts[3]),pts[0],flip(pts[1]),pts[4],flip(pts[5]));
  770.   }
  771. }
  772. /* Ellipse: centre 0,1, width 2, hight 3, color 4,5,6 */
  773. void obj_ellipse(objparm *pm)
  774. {
  775.   scale(pm->pd,pm->p,1);
  776.   pm->p[2]=pm->pd[2]*xscale; pm->p[3]=pm->pd[3]*yscale;
  777.   if(pm->fill==1 || pm->fill==2) {
  778.     gdImageArc(image,pm->p[0],pm->p[1],pm->p[2],pm->p[3],0,360,
  779.              color_bounder);
  780.     patchgdImageFillToBorder(image,pm->p[0],pm->p[1],
  781.                     color_bounder,pm->color[0]);
  782.   }
  783.   gdImageArc(image,pm->p[0],pm->p[1],rint(pm->p[2]),rint(pm->p[3]),0,360,pm->color[0]);
  784.   if(tikz_file) fprintf(tikz_file,"\\draw\[%s] (%i,%i) ellipse (%i and %i);\n\n",
  785.     tikz_options(pm->color[0],pm->fill),pm->p[0],flip(pm->p[1]),(int) rint(0.5*pm->p[2]),(int) rint(0.5*pm->p[3]));
  786.   if(vimg_enable) vimg_ellipse(scale_buf[0],scale_buf[1],0.5*pm->pd[2],0.5*pm->pd[3]);
  787. }
  788.  
  789. /* Circle radius in pixels*/
  790. void obj_circle(objparm *pm)
  791. {
  792.   scale(pm->pd,pm->p,1);
  793.   pm->p[2]=rint(pm->pd[2]); pm->p[3]=rint(pm->pd[2]);
  794.   if(pm->fill==1 || pm->fill==2) {
  795.     gdImageArc(image,pm->p[0],pm->p[1],pm->p[2],pm->p[3],0,360,
  796.              color_bounder);
  797.     patchgdImageFillToBorder(image,pm->p[0],pm->p[1],
  798.                     color_bounder,pm->color[0]);
  799.   }
  800.   gdImageArc(image,pm->p[0],pm->p[1],pm->p[2],pm->p[3],0,360,pm->color[0]);
  801.   if(tikz_file) fprintf(tikz_file, "\\draw\[%s] (%i, %i) circle (%i);\n",
  802.       tikz_options(pm->color[0],pm->fill),pm->p[0],flip(pm->p[1]),(int) rint(pm->pd[2]/2.));
  803. }
  804. /* Circle, radius in xrange */
  805. void obj_circles(objparm *pm)
  806. {
  807.   int i, n;
  808.   n=(pm->pcnt)/3;
  809.   for (i=0; i<n; ++i) scale(pm->pd+3*i, pm->p+3*i, 1);
  810.   if (tikz_file) fprintf(tikz_file,"\\draw\[%s]",tikz_options(pm->color[0],pm->fill));
  811.   for(i=0;i<3*n;i+=3){
  812.     if(pm->fill==1 || pm->fill==2) {
  813.       gdImageArc(image,pm->p[i],pm->p[i+1],
  814.         rint(2*pm->pd[i+2]*xscale),rint(2*pm->pd[i+2]*xscale),0,360,
  815.           color_bounder);
  816.       patchgdImageFillToBorder(image,pm->p[i],pm->p[i+1],
  817.                     color_bounder,pm->color[0]);
  818.     }
  819.     gdImageArc(image,pm->p[i],pm->p[i+1],
  820.         rint(2*pm->pd[i+2]*xscale),rint(2*pm->pd[i+2]*xscale),0,360,pm->color[0]);
  821.     if (tikz_file){
  822.       fprintf(tikz_file, "(%i, %i) circle (%i and %i)",
  823.         pm->p[i],flip(pm->p[i+1]),(int) rint(pm->pd[i+2]*xscale),(int) rint(pm->pd[i+2]*xscale));
  824.     }
  825.   }
  826.   if (tikz_file) fprintf(tikz_file,";\n");
  827. }
  828. /* ellipse, width in xrange,height in yrange */
  829. void obj_ellipses(objparm *pm)
  830. {
  831.   int i, n;
  832.   n=(pm->pcnt)/4;
  833.   for (i=0; i<n; ++i) scale(pm->pd+4*i, pm->p+4*i, 1);
  834.   if (tikz_file) fprintf(tikz_file,"\\draw\[%s]",tikz_options(pm->color[0],pm->fill));
  835.   for(i=0;i<4*n;i+=4){
  836.     if(pm->fill==1 || pm->fill==2){
  837.       gdImageArc(image,pm->p[i],pm->p[i+1],
  838.         rint(pm->pd[i+2]*xscale),rint(pm->pd[i+2]*xscale),0,360,
  839.         color_bounder);
  840.       patchgdImageFillToBorder(image,pm->p[i],pm->p[i+1],
  841.         color_bounder,pm->color[0]);
  842.     } else
  843.       gdImageArc(image,pm->p[i],pm->p[i+1],rint(pm->pd[i+2]*xscale),rint(pm->pd[i+3]*yscale),0,360,pm->color[0]);
  844.     if (tikz_file){
  845.       fprintf(tikz_file, "(%i, %i) circle (%i and %i)",
  846.         pm->p[i],flip(pm->p[i+1]),(int) rint(pm->pd[i+2]*xscale/2),(int) rint(pm->pd[i+3]*yscale/2));
  847.     }
  848.   }
  849.   if (tikz_file) fprintf(tikz_file,";\n");
  850. }
  851. typedef enum tikz_fill_options {grid, dot, hatch, diamond} tfo;
  852. int compar(const void *a, const void *b) {return *(int *)b - *(int *)a;}
  853.  
  854. static void tikz_fill(int x, int y, int nx, int ny, int wall, int the_color, tfo opt)
  855. {
  856.   int numpix=sizex*sizey, top = 0, a, c, cy, lx=0, rx=0, seed, nt,ct=0;
  857.   int *stack = xmalloc(numpix*sizeof(int));
  858.   int *stack2 = xmalloc(numpix*sizeof(int));
  859.   if (nx==0) nx=1;
  860.   if (ny==0) ny=1;
  861.   nt = abs(nx*ny);
  862.   seed = gdImageGetPixel(image,x,y);
  863.   char *check = xmalloc(numpix);
  864.   while (numpix--) check[numpix]=0;
  865.   a=x+y*sizex;
  866.   check[a]=1;
  867.   stack[top++]=a;
  868.   while(top)
  869.     {
  870.       a = stack[--top];
  871.       x = a%sizex; y = a/sizex;
  872.       c = gdImageGetPixel(image,x,y);
  873.       if ((wall && (c == wall))||(!wall && c!=seed))
  874.         continue;
  875.       if (x>0 && !check[a-1]) {check[a-1]=1; stack[top++]=a-1;}
  876.       if (x+1<sizex && !check[a+1]) {check[a+1]=1; stack[top++]=a+1;}
  877.       if (y>0 && !check[a-sizex]) {check[a-sizex]=1; stack[top++]=a-sizex;}
  878.       if (y+1<sizey && !check[a+sizex]) {check[a+sizex]=1; stack[top++]=a+sizex;}
  879.       switch(opt)
  880.   {
  881.   case grid: if(((x%nx)!=(nx/2))&&((y%ny)!=(ny/2))) continue; break;
  882.   case dot: if(x%nx!=nx/2||y%ny!=ny/2) continue; break;
  883.   case hatch: if((ny*x-nx*y+nt)%nt) continue; break;
  884.   case diamond: if((ny*x+nx*y)%nt && (ny*x-nx*y+nt)%nt)continue;
  885.   default: break;
  886.   }
  887.       stack2[ct++]=a;
  888.     }
  889.   free(stack);
  890.   free(check);
  891.   if (ct==0) return;
  892.   fprintf(tikz_file,"\\draw\[%s]", tikz_options(the_color,1));
  893.   qsort(stack2, ct, sizeof(int), compar);
  894.   cy=-1;
  895.   while (ct--)
  896.     {
  897.       a = stack2[ct];
  898.       x = a%sizex; y = a/sizex;
  899.       if (y != cy || x != rx+1)
  900.       {
  901.   if (cy >= 0)
  902.     fprintf(tikz_file,
  903.       "(%.2f,%.2f)rectangle(%.2f,%.2f)",
  904.       lx-0.0,flip(cy-0.0),rx+0.0,flip(cy+0.0));
  905.   cy = y;
  906.   lx = rx = x;
  907.       }
  908.       else
  909.   rx++;
  910.     }
  911.   fprintf(tikz_file,
  912.     "(%.2f,%.2f)rectangle(%.2f,%.2f);\n",
  913.      lx-0.0,flip(cy-0.0),rx+0.0,flip(cy+0.0));
  914.   free(stack2);
  915. }
  916. void obj_dashed(objparm *pm){ dashed=1;}
  917. void obj_filled(objparm *pm){ filled=1;}
  918. void obj_noreset(objparm *pm){ noreset=1;}
  919. void obj_reset(objparm *pm){ noreset=0;dashed=0;filled=0;}
  920.  
  921. /* flood fill */
  922. void obj_fill(objparm *pm)
  923. {
  924.   scale(pm->pd,pm->p,1);
  925.   if (tikz_file) tikz_fill(pm->p[0],pm->p[1],1,1,0,pm->color[0],grid);
  926.   patchgdImageFill(image,pm->p[0],pm->p[1],pm->color[0]);
  927. }
  928.  
  929. /* flood fill to border*/
  930. void obj_fillb(objparm *pm)
  931. {
  932.   scale(pm->pd,pm->p,1);
  933.   if (tikz_file) tikz_fill(pm->p[0],pm->p[1],1,1,pm->color[0],pm->color[1],grid);
  934.   patchgdImageFillToBorder(image,pm->p[0],pm->p[1],pm->color[0],pm->color[1]);
  935. }
  936.  
  937. gdImagePtr himg;
  938.  
  939. int makehatchimage(int x, int y, int px, int py, int col)
  940. {
  941.   int c1,c2,r,g,b;
  942.   gdImagePtr saveimg;
  943.   himg=gdImageCreate(x,y);
  944.   c1=gdImageGetPixel(image,px,py);
  945.   r=gdImageRed(image,c1); g=gdImageGreen(image,c1); b=gdImageBlue(image,c1);
  946.   if(r>=255) r--; else r++; if(g>=255) g--; else g++; if(b>=255) b--; else b++;
  947.   c1=gdImageColorAllocate(himg,r,g,b);
  948.   r=gdImageRed(image,col); g=gdImageGreen(image,col); b=gdImageBlue(image,col);
  949.   c2=gdImageColorAllocate(himg,r,g,b);
  950.   if(width>1) {
  951.     savew=-1; saveimg=image;
  952.     image=himg; c2=widthcolor(width,c2); image=saveimg;
  953.     c2=gdBrushed; savew=-1;
  954.   }
  955.   return c2;
  956. }
  957.  
  958. /* flood fill with hatching */
  959. void obj_hatchfill(objparm *pm)
  960. {
  961.   int nx,ny,ax,ay, dir, c;
  962.   scale(pm->pd,pm->p,1);
  963.   nx=pm->pd[2]; ny=pm->pd[3]; ax=abs(nx); ay=abs(ny);
  964.   if(nx==0 && ny==0) {fly_error("bad displacement vector"); return;}
  965.   if((nx>0 && ny>0) || (nx<0 && ny<0)) dir=1; else dir=-1;
  966.   if(ax==0) {ax=100; dir=2;}
  967.   if(ay==0) {ay=100; dir=3;}
  968.   c=makehatchimage(ax,ay,pm->p[0],pm->p[1],pm->color[0]);
  969.   switch(dir) {
  970.     case -1: {
  971.       gdImageLine(himg,0,ay-1,ax-1,0,c);
  972.       if(width>1) {
  973.         gdImageLine(himg,-ax,ay-1,-1,0,c);
  974.         gdImageLine(himg,ax,ay-1,2*ax-1,0,c);
  975.         gdImageLine(himg,0,-1,ax-1,-ay,c);
  976.         gdImageLine(himg,0,2*ay-1,ax-1,ay,c);
  977.       }
  978.       break;
  979.     }
  980.     case 1: {
  981.       gdImageLine(himg,0,0,ax-1,ay-1,c);
  982.       if(width>1) {
  983.         gdImageLine(himg,-ax,0,-1,ay-1,c);
  984.         gdImageLine(himg,ax,0,2*ax-1,ay-1,c);
  985.         gdImageLine(himg,0,-ay,ax-1,-1,c);
  986.         gdImageLine(himg,0,ay,ax-1,2*ay-1,c);
  987.       }
  988.       break;
  989.     }
  990.     case 2: gdImageLine(himg,0,ay/2,ax-1,ay/2,c); break;
  991.     case 3: gdImageLine(himg,ax/2,0,ax/2,ay-1,c); break;
  992.   }
  993.   if (tikz_file) tikz_fill(pm->p[0],pm->p[1],nx,ny,0,pm->color[0],hatch);
  994.   gdImageSetTile(image,himg);
  995.   patchgdImageFill(image,pm->p[0],pm->p[1],gdTiled);
  996.   gdImageDestroy(himg);
  997.   if(tiled) gdImageSetTile(image,tileimg);
  998. }
  999.  
  1000. /* flood fill with grid */
  1001. void obj_gridfill(objparm *pm)
  1002. {
  1003.   int nx,ny, c;
  1004.   scale(pm->pd,pm->p,1);
  1005.   nx=pm->pd[2]; ny=pm->pd[3]; nx=abs(nx); ny=abs(ny);
  1006.   if(nx==0 && ny==0) {fly_error("bad grid size"); return;}
  1007.   c=makehatchimage(nx,ny,pm->p[0],pm->p[1],pm->color[0]);
  1008.   if (tikz_file) tikz_fill(pm->p[0],pm->p[1],nx,ny,0,pm->color[0],grid);
  1009.   gdImageLine(himg,0,ny/2,nx-1,ny/2,c); gdImageLine(himg,nx/2,0,nx/2,ny-1,c);
  1010.   gdImageSetTile(image,himg);
  1011.   patchgdImageFill(image,pm->p[0],pm->p[1],gdTiled);
  1012.   gdImageDestroy(himg);
  1013.   if(tiled) gdImageSetTile(image,tileimg);
  1014. }
  1015.  
  1016. /* flood fill with double hatching */
  1017. void obj_diafill(objparm *pm)
  1018. {
  1019.   int nx,ny, c;
  1020.   scale(pm->pd,pm->p,1);
  1021.   nx=pm->pd[2]; ny=pm->pd[3]; nx=abs(nx); ny=abs(ny);
  1022.   if(nx==0 && ny==0) {fly_error("bad grid size"); return;}
  1023.   c=makehatchimage(nx,ny,pm->p[0],pm->p[1],pm->color[0]);
  1024.   if (tikz_file) tikz_fill(pm->p[0],pm->p[1],nx,ny,0,pm->color[0],diamond);
  1025.   gdImageLine(himg,0,0,nx-1,ny-1,c); gdImageLine(himg,0,ny-1,nx-1,0,c);
  1026.   gdImageSetTile(image,himg);
  1027.   patchgdImageFill(image,pm->p[0],pm->p[1],gdTiled);
  1028.   gdImageDestroy(himg);
  1029.   if(tiled) gdImageSetTile(image,tileimg);
  1030. }
  1031.  
  1032. /* flood fill with dots */
  1033. void obj_dotfill(objparm *pm)
  1034. {
  1035.   int nx,ny, c;
  1036.   scale(pm->pd,pm->p,1);
  1037.   nx=pm->pd[2]; ny=pm->pd[3]; nx=abs(nx); ny=abs(ny);
  1038.   if(nx==0 && ny==0) {fly_error("bad grid size"); return;}
  1039.   c=makehatchimage(nx,ny,pm->p[0],pm->p[1],pm->color[0]);
  1040.   if (tikz_file) tikz_fill(pm->p[0],pm->p[1],nx,ny,0,pm->color[0],dot);
  1041.   gdImageSetPixel(himg,nx/2,ny/2,c);
  1042.   gdImageSetTile(image,himg);
  1043.   patchgdImageFill(image,pm->p[0],pm->p[1],gdTiled);
  1044.   gdImageDestroy(himg);
  1045.   if(tiled) gdImageSetTile(image,tileimg);
  1046. }
  1047.  
  1048. struct {
  1049.   char *name;
  1050.   gdFontPtr *fpt;
  1051.   char *ftikz;
  1052. } fonttab[]={
  1053.   {"tiny",  &gdFontTiny,"0.3"},
  1054.   {"small", &gdFontSmall,"0.5"},
  1055.   {"medium",&gdFontMediumBold,"0.7"},
  1056.   {"large", &gdFontLarge,"0.9"},
  1057.   {"giant", &gdFontGiant,"1"},
  1058.   {"huge",  &gdFontGiant,"1"}
  1059. };
  1060.  
  1061. #define fonttab_no (sizeof(fonttab)/sizeof(fonttab[0]))
  1062.  
  1063. /* string */
  1064. void obj_string(objparm *pm)
  1065. {
  1066.   char *pp, *pe, *p2;
  1067.   int i;
  1068.   pp=pm->str; pe=strchr(pp,','); if(pe==NULL) {
  1069.     fly_error("too_few_parms"); return;
  1070.   }
  1071.   *pe++=0; pp=find_word_start(pp); *find_word_end(pp)=0;
  1072.   pe=find_word_start(pe); strip_trailing_spaces(pe);
  1073.   if(*pp) {
  1074.     for(i=0;i<fonttab_no && strcmp(pp,fonttab[i].name)!=0; i++);
  1075.     if(i>=fonttab_no) i=1;
  1076.   }
  1077.   else i=1;
  1078.   scale(pm->pd,pm->p,1);
  1079.   if(*pe=='"') {
  1080.     p2=strchr(pe+1,'"');
  1081.     if(p2 && *(p2+1)==0) {*p2=0; pe++;}
  1082.   }
  1083.   if(pm->fill){
  1084.     gdImageStringUp(image,*(fonttab[i].fpt),pm->p[0],pm->p[1], (unsigned char*) pe,
  1085.         pm->color[0]);
  1086.     if(tikz_file) fprintf(tikz_file,"\\draw (%i,%i) node[scale=%s,rotate=90,color=%s,anchor=north west,inner sep=0pt] {\\(%s\\)};\n",
  1087.       pm->p[0],flip(pm->p[1]),fonttab[i].ftikz,tikz_color(pm->color[0]),(unsigned char*) pe);
  1088.   }
  1089.   else {
  1090.     gdImageString(image,*(fonttab[i].fpt),pm->p[0],pm->p[1], (unsigned char*) pe,
  1091.         pm->color[0]);
  1092.     if(tikz_file) fprintf(tikz_file,"\\draw (%i,%i) node[scale=%s,color=%s,anchor=north west,inner sep=0pt] {\\(%s\\)};\n",
  1093.       pm->p[0],flip(pm->p[1]),fonttab[i].ftikz,tikz_color(pm->color[0]),(unsigned char*) pe);
  1094.   }
  1095. }
  1096. /*FIXME; giant does not exist in tikz and ... samething as huge */
  1097. /* point */
  1098. void obj_point(objparm *pm)
  1099. {
  1100.   scale(pm->pd,pm->p,1);
  1101.   gdImageSetPixel(image,pm->p[0],pm->p[1],pm->color[0]);
  1102.   if(tikz_file) fprintf(tikz_file,"\\draw[%s] (%i,%i) circle (1pt);\n",
  1103.     tikz_options(pm->color[0],0),pm->p[0],flip(pm->p[1]));
  1104. }
  1105.  
  1106. /* copy an image file */
  1107. void obj_copy(objparm *pm)
  1108. {
  1109.   char *pp;
  1110.   FILE *inf;
  1111.   gdImagePtr insimg;
  1112.  
  1113.   pp=find_word_start(pm->str);*find_word_end(pp)=0;
  1114.   inf=open4read(pp);
  1115.   if(inf==NULL) {
  1116.     fly_error("file_does_not_exist"); return;
  1117.   }
  1118.   insimg=gdImageCreateFromGif(inf); fclose(inf);
  1119.   if(insimg==NULL) {
  1120.     fly_error("bad_gif"); return;
  1121.   }
  1122.   scale(pm->pd,pm->p,1);
  1123.   if(pm->pd[2]<0 && pm->pd[3]<0 && pm->pd[4]<0 && pm->pd[5]<0)
  1124.     gdImageCopy(image,insimg,pm->p[0],pm->p[1],0,0,
  1125.             insimg->sx,insimg->sy);
  1126.   else
  1127.     gdImageCopy(image,insimg,pm->p[0],pm->p[1],pm->pd[2],pm->pd[3],
  1128.             pm->pd[4]-pm->pd[2],pm->pd[5]-pm->pd[3]);
  1129.   if(tikz_file) fprintf(tikz_file,
  1130.     "\\node[anchor=north west,inner sep=0pt] at (%i,%i) {\\includegraphics\[width=.05\\linewidth]{%s}};\n",
  1131.       pm->p[0],flip(pm->p[1]),pm->str);
  1132.     gdImageDestroy(insimg);
  1133. /*FIXME position non correcte; on ne peut pas utiliser une image gif */
  1134. }
  1135.  
  1136. /* copy an image file, with resizing */
  1137. void obj_copyresize(objparm *pm)
  1138. {
  1139.   char *pp;
  1140.   FILE *inf;
  1141.   gdImagePtr insimg;
  1142.  
  1143.   pp=find_word_start(pm->str);*find_word_end(pp)=0;
  1144.   inf=open4read(pp);
  1145.   if(inf==NULL) {
  1146.     fly_error("file_not_found"); return;
  1147.   }
  1148.   insimg=gdImageCreateFromGif(inf); fclose(inf);
  1149.   if(insimg==NULL) {
  1150.     fly_error("bad_gif"); return;
  1151.   }
  1152.   scale(pm->pd+4,pm->p+4,2);
  1153.   if(pm->pd[0]<0 && pm->pd[1]<0 && pm->pd[2]<0 && pm->pd[3]<0)
  1154.     gdImageCopyResized(image,insimg,pm->p[4],pm->p[5],0,0,
  1155.                  pm->p[6]-pm->p[4]+1,pm->p[7]-pm->p[5]+1,
  1156.                  insimg->sx,insimg->sy);
  1157.   else
  1158.     gdImageCopyResized(image,insimg,pm->p[4],pm->p[5],pm->pd[0],pm->pd[1],
  1159.                  pm->p[6]-pm->p[4]+1,pm->p[7]-pm->p[5]+1,
  1160.                  pm->pd[2]-pm->pd[0]+1,pm->pd[3]-pm->pd[1]+1);
  1161.   gdImageDestroy(insimg);
  1162. }
  1163.  
  1164. /* set brush or tile */
  1165. void obj_setbrush(objparm *pm)
  1166. {
  1167.   char *pp;
  1168.   FILE *inf;
  1169.   gdImagePtr insimg;
  1170.  
  1171.   pp=find_word_start(pm->str); *find_word_end(pp)=0;
  1172.   inf=open4read(pp); if(inf==NULL) {
  1173.     fly_error("file_not_found"); return;
  1174.   }
  1175.   insimg=gdImageCreateFromGif(inf); fclose(inf);
  1176.   if(insimg==NULL) {
  1177.     fly_error("bad_gif"); return;
  1178.   }
  1179.   if(pm->fill) {
  1180.     gdImageSetTile(image,insimg); tiled=1; tileimg=insimg;
  1181.   }
  1182.   else {
  1183.     gdImageSetBrush(image,insimg);brushed=1; brushimg=insimg;
  1184.   }
  1185. }
  1186.  
  1187. /* kill brush */
  1188. void obj_killbrush(objparm *pm)
  1189. {
  1190.   if(brushimg) gdImageDestroy(brushimg);
  1191.   brushed=0; brushimg=NULL;
  1192. }
  1193.  
  1194. /* kill tile */
  1195. void obj_killtile(objparm *pm)
  1196. {
  1197.   if(tileimg) gdImageDestroy(tileimg);
  1198.   tiled=0; tileimg=NULL;
  1199. }
  1200.  
  1201. /* set style */
  1202. void obj_setstyle(objparm *pm)
  1203. {
  1204.   int i,t;
  1205.   t=pm->pcnt/3; if(t<=0) {
  1206.     fly_error("too_few_parms"); return;
  1207.   }
  1208.   for(i=0;i<t;i++) {
  1209.     if(pm->pd[3*i]<0 || pm->pd[3*i+1]<0 || pm->pd[3*i+2]<0)
  1210.       pm->p[i]=gdTransparent;
  1211.     else
  1212.       pm->p[i]=getcolor(pm->pd[3*i],pm->pd[3*i+1],pm->pd[3*i+2]);
  1213.   }
  1214.   gdImageSetStyle(image,pm->p,t); styled=1;
  1215. }
  1216.  
  1217. /* kill style */
  1218. void obj_killstyle(objparm *pm)
  1219. {
  1220.   styled=0;
  1221. }
  1222.  
  1223. /* set transparent */
  1224. void obj_transp(objparm *pm)
  1225. {
  1226.   gdImageColorTransparent(image,pm->color[0]);
  1227. }
  1228.  
  1229. /* set interlace */
  1230. void obj_interlace(objparm *pm)
  1231. {
  1232.   gdImageInterlace(image,1);
  1233. }
  1234.  
  1235. /* set linewidth */
  1236. void obj_linewidth(objparm *pm)
  1237. {
  1238.   if(pm->pd[0]<1 || pm->pd[0]>255) fly_error("bad_parms");
  1239.   width=pm->pd[0];
  1240.   if(tikz_file) fprintf(tikz_file,"\\pgfsetlinewidth{%fpt}\n",width*0.7);
  1241. }
  1242.  
  1243. /* set crosshairsize */
  1244. void obj_crosshairsize(objparm *pm)
  1245. {
  1246.   if(pm->pd[0]<1 || pm->pd[0]>255) fly_error("bad_parms");
  1247.   else width2=pm->pd[0];
  1248. }
  1249.  
  1250. /* set x range */
  1251. void obj_xrange(objparm *pm)
  1252. {
  1253.   double dd;
  1254.   dd=pm->pd[1]-pm->pd[0];
  1255.   xstart=pm->pd[0]; xscale=sizex/dd;
  1256. }
  1257.  
  1258. /* set y range */
  1259. void obj_yrange(objparm *pm)
  1260. {
  1261.   double dd;
  1262.   dd=pm->pd[1]-pm->pd[0];
  1263.   ystart=pm->pd[1]; yscale=-sizey/dd;
  1264. }
  1265.  
  1266. /* set x et y range */
  1267. void obj_range(objparm *pm)
  1268. {
  1269.   double d1,d2;
  1270.   d1=pm->pd[1]-pm->pd[0]; d2=pm->pd[3]-pm->pd[2];
  1271.   xstart=pm->pd[0]; xscale=(sizex-1)/d1;
  1272.   ystart=pm->pd[3]; yscale=-(sizey-1)/d2;
  1273. }
  1274.  
  1275. /* set t range */
  1276. void obj_trange(objparm *pm)
  1277. {
  1278.   /*double dd;
  1279.   dd=pm->pd[1]-pm->pd[0];*/
  1280.   tstart=pm->pd[0]; tend=pm->pd[1];
  1281.   tranged=1;
  1282. }
  1283.  
  1284. /* set tstep (plotting step) */
  1285. void obj_tstep(objparm *pm)
  1286. {
  1287.   int dd;
  1288.   dd=pm->pd[0];
  1289.   if(dd<3) {
  1290.     fly_error("bad_step"); return;
  1291.   }
  1292.   if(dd>2000) dd=2000;
  1293.   tstep=dd;
  1294. }
  1295.  
  1296. /* set plotjump (plot jump break threashold) */
  1297. void obj_plotjump(objparm *pm)
  1298. {
  1299.   int dd;
  1300.   dd=pm->pd[0];
  1301.   if(dd<3) dd=3;
  1302.   if(dd>MAX_SIZE) dd=MAX_SIZE;
  1303.   plotjump=dd;
  1304. }
  1305.  
  1306. /* plot a curve, either parametric or explicit */
  1307. void _obj_plot(objparm *pm,int dash)
  1308. {
  1309.   int i,j,n,dist,xx,yy,varpos;
  1310.   char p1[MAX_LINELEN+1], p2[MAX_LINELEN+1];
  1311.   char *varn, *pp;
  1312.   double dc[2],t,v;
  1313.   int ic[2],oc[2];
  1314.  
  1315.   n=itemnum(pm->str);
  1316.   if(n<1) {
  1317.     fly_error("bad_parms"); return;
  1318.   }
  1319.   fnd_item(pm->str,1,p1); fnd_item(pm->str,2,p2);
  1320.   if(n==1 && !tranged) v=sizex/xscale/tstep;
  1321.   else v=(tend-tstart)/tstep;
  1322.   if(n==1) varn="x"; else varn="t";
  1323.   for(pp=varchr(p1,varn); pp; pp=varchr(pp,varn)) {
  1324.     string_modify(p1,pp,pp+strlen(varn),EV_T);
  1325.     pp+=strlen(EV_T);
  1326.   }
  1327.   for(pp=varchr(p2,varn); pp; pp=varchr(pp,varn)) {
  1328.     string_modify(p2,pp,pp+strlen(varn),EV_T);
  1329.       pp+=strlen(EV_T);
  1330.   }
  1331.   varpos=eval_getpos(EV_T);
  1332.   if(varpos<0) return; /* unknown error */
  1333.   evalue_compile(p1); evalue_compile(p2);
  1334.   if(vimg_enable) vimg_plotstart();
  1335.   if (tikz_file) fprintf(tikz_file,"\%\%begin plot\n\\draw\[%s]",
  1336.     tikz_options(pm->color[0],-dash));
  1337.   for(i=j=0;i<=tstep;i++) {
  1338.     if(n==1) {
  1339.       if(tranged) t=tstart+i*v; else t=xstart+i*v;
  1340.       eval_setval(varpos,t); dc[0]=t; dc[1]=strevalue(p1);
  1341.     }
  1342.     else {
  1343.       t=tstart+i*v; eval_setval(varpos,t);
  1344.       dc[0]=strevalue(p1); dc[1]=strevalue(p2);
  1345.     }
  1346.     if(!isfinite(dc[0]) || !isfinite(dc[1])) ic[0]=ic[1]=-BOUND;
  1347.     else scale(dc,ic,1);
  1348.     if(vimg_enable) vimg_plot1 (scale_buf[0],scale_buf[1]);
  1349.     if(j==0) {
  1350.       gdImageSetPixel(image,ic[0],ic[1],pm->color[0]); j++;
  1351.       if (tikz_file)
  1352.         fprintf(tikz_file,"(%i,%i)rectangle(%i,%i)",
  1353.           ic[0],flip(ic[1]),ic[0]+1,flip(ic[1]+1));
  1354.     }
  1355.     else {
  1356.       xx=ic[0]-oc[0]; yy=ic[1]-oc[1];
  1357.       dist=sqrt(xx*xx+yy*yy);
  1358.       if(dist>0){
  1359.         if(dist>plotjump || dist==1) {
  1360.           gdImageSetPixel(image,ic[0],ic[1],pm->color[0]);
  1361.           if (tikz_file)
  1362.             fprintf(tikz_file,"(%i,%i)rectangle(%i,%i)",
  1363.               ic[0],flip(ic[1]),ic[0]+1,flip(ic[1]+1));
  1364.         }
  1365.         else {
  1366.           if (dash==1)
  1367.             myDashedLine(image,oc[0],oc[1],ic[0],ic[1],pm->color[0]);
  1368.           else
  1369.             gdImageLine(image,oc[0],oc[1],ic[0],ic[1],pm->color[0]);
  1370.           if (tikz_file)
  1371.             fprintf(tikz_file, "(%i,%i)--(%i,%i)",
  1372.                 oc[0],flip(oc[1]),ic[0],flip(ic[1]));
  1373.         }
  1374.       }
  1375.     }
  1376.     memmove(oc,ic,sizeof(oc));
  1377.   }
  1378.   if (tikz_file) fprintf(tikz_file,";\n\%\%end plot\n");
  1379.   if(vimg_enable) vimg_plotend();
  1380. }
  1381.  
  1382. void obj_plot(objparm *pm) { _obj_plot( pm,0) ;}
  1383. void obj_dplot(objparm *pm) { _obj_plot( pm,1) ;}
  1384.  
  1385. /* set levelcurve granularity */
  1386. void obj_levelstep(objparm *pm)
  1387. {
  1388.   int dd;
  1389.   dd=pm->pd[0];
  1390.   if(dd<1) return;
  1391.   if(dd>16) dd=16;
  1392.   lstep=dd;
  1393. }
  1394.  
  1395. /* level curve */
  1396. void obj_levelcurve(objparm *pm)
  1397. {
  1398.   char fn[MAX_LINELEN+1],tc[MAX_LINELEN+1];
  1399.   int n,i;
  1400.   double d;
  1401.   leveldata *ld;
  1402.  
  1403.   ld=xmalloc(sizeof(leveldata)+16);
  1404.   ld->xname="x"; ld->yname="y";
  1405.   ld->xsize=sizex; ld->ysize=sizey; ld->datacnt=0;
  1406.   ld->xrange[0]=xstart; ld->xrange[1]=sizex/xscale+xstart;
  1407.   ld->yrange[0]=sizey/yscale+ystart; ld->yrange[1]=ystart;
  1408.   ld->grain=lstep;
  1409.   fnd_item(pm->str,1,fn); ld->fn=fn;
  1410.   n=itemnum(pm->str);
  1411.   if(n<=1) {ld->levelcnt=1; ld->levels[0]=0;}
  1412.   else {
  1413.     if(n>LEVEL_LIM+1) n=LEVEL_LIM+1;
  1414.     for(i=0;i<n-1;i++) {
  1415.       fnd_item(pm->str,i+2,tc);
  1416.       d=strevalue(tc);
  1417.       if(isfinite(d)) ld->levels[i]=d; else ld->levels[i]=0;
  1418.     }
  1419.     ld->levelcnt=n-1;
  1420.   }
  1421.   levelcurve(ld);
  1422.   if (tikz_file)
  1423.     fprintf(tikz_file,"\%\%begin levelcurve\n\\draw\[%s]"
  1424.       ,tikz_options(pm->color[0],1));
  1425.   for(i=0;i<ld->datacnt;i++) {
  1426.     gdImageSetPixel(image,ld->xdata[i],ld->ydata[i],pm->color[0]);
  1427.     if (tikz_file)
  1428.       fprintf(tikz_file,
  1429.         "(%i,%i)rectangle(%i,%i)",
  1430.         ld->xdata[i],flip(ld->ydata[i]),ld->xdata[i]+1,flip(ld->ydata[i]+1));
  1431.   }
  1432.   if (tikz_file) fprintf(tikz_file,";\n\%\%end levelcurve\n");
  1433.   free(ld);
  1434. }
  1435.  
  1436. /* set animation step */
  1437. void obj_animstep(objparm *pm)
  1438. {
  1439.   animstep=pm->pd[0];
  1440.   setvar("animstep",pm->pd[0]);
  1441. }
  1442.  
  1443. /* Starts a line counting */
  1444. void obj_linecount(objparm *pm)
  1445. {
  1446.   linecnt=pm->pd[0];
  1447. }
  1448.  
  1449. /* marks end of execution */
  1450. void obj_end(objparm *pm)
  1451. {
  1452.   fly_error("successful_end_of_execution");
  1453. }
  1454.  
  1455. /* output */
  1456. void obj_output(objparm *pm)
  1457. {
  1458.   char *p, namebuf[1024];
  1459.   p=find_word_start(pm->str); *find_word_end(p)=0;
  1460.   snprintf(namebuf,sizeof(namebuf),"%s",imagefilename);
  1461.   snprintf(imagefilename,sizeof(imagefilename),"%s",p);
  1462.   output();
  1463.   snprintf(imagefilename,sizeof(imagefilename),"%s",namebuf);
  1464. }
  1465.  
  1466. /* vimgfile */
  1467. void obj_vimgfile(objparm *pm)
  1468. {
  1469.   char *p;
  1470.   p=find_word_start(pm->str); *find_word_end(p)=0;
  1471.   snprintf(vimgfilename,sizeof(vimgfilename),"%s",p);
  1472.   if(vimg_ready) vimg_close();
  1473. }
  1474.  
  1475. /* vimg enable/disable */
  1476. void obj_vimg(objparm *pm)
  1477. {
  1478.   vimg_enable=pm->pd[0];
  1479.   if(vimg_enable>0 && vimg_ready==0) vimg_init();
  1480. }
  1481.  
  1482. /* Set affine transformation */
  1483. void obj_affine(objparm *pm)
  1484. {
  1485.   int i;
  1486.   for(i=0;i<4;i++) matrix[i]=pm->pd[i];
  1487.   transx=pm->pd[4]; transy=pm->pd[5];
  1488.   transform=1;
  1489. }
  1490.  
  1491. /* Set affine transformation */
  1492. void obj_rotation(objparm *pm)
  1493. {
  1494.   double r;
  1495.   r=M_PI*pm->pd[0]/180;
  1496.   matrix[0]=matrix[3]=cos(r);
  1497.   matrix[1]=-sin(r); matrix[2]=sin(r);
  1498.   transform=1;
  1499. }
  1500.  
  1501. /* Set linear transformation */
  1502. void obj_linear(objparm *pm)
  1503. {
  1504.   int i;
  1505.   for(i=0;i<4;i++) matrix[i]=pm->pd[i];
  1506.   transform=1;
  1507. }
  1508.  
  1509. /* Set translation transformation */
  1510. void obj_translation(objparm *pm)
  1511. {
  1512.   transx=pm->pd[0]; transy=pm->pd[1];
  1513. }
  1514.  
  1515. /* kill affine transformation */
  1516. void obj_killaffine(objparm *pm)
  1517. {
  1518.   matrix[0]=matrix[3]=1;
  1519.   matrix[1]=matrix[2]=transx=transy=0;
  1520.   transform=0;
  1521. }
  1522.  
  1523. /* kill affine transformation */
  1524. void obj_killlinear(objparm *pm)
  1525. {
  1526.   matrix[0]=matrix[3]=1;
  1527.   matrix[1]=matrix[2]=0;
  1528.   transform=0;
  1529. }
  1530.  
  1531. /* kill affine transformation */
  1532. void obj_killtranslation(objparm *pm)
  1533. {
  1534.   transx=transy=0;
  1535. }
  1536.  
  1537. /***** Les modifs de Jean-Christophe Leger Fev 2006 *****/
  1538.  
  1539. /* arguments: un numero de matrice entre 1 et JC_NB_MATRICES, une liste de 4 nombres reels pour la matrice */
  1540. void obj_setmatrix(objparm *pm)
  1541. {
  1542.   int nummatrix = (int) (pm->pd[0]);
  1543.   if((nummatrix < 1) ||(nummatrix>JC_NB_MATRICES)) {
  1544.     fly_error("bad_matrix_number");
  1545.     return;
  1546.   }
  1547.   matrices_pavage[nummatrix][0] = pm->pd[1];
  1548.   matrices_pavage[nummatrix][1] = pm->pd[2];
  1549.   matrices_pavage[nummatrix][2] = pm->pd[3];
  1550.   matrices_pavage[nummatrix][3] = pm->pd[4];
  1551. }
  1552.  
  1553. /* arguments: un numero de matrice entre 1 et JC_NB_MATRICES */
  1554. void obj_resetmatrix(objparm *pm)
  1555. {
  1556.   int nummatrix = (int) (pm->pd[0]);
  1557.   if((nummatrix < 1) ||(nummatrix>JC_NB_MATRICES)) {
  1558.     fly_error("bad_matrix_number");
  1559.     return;
  1560.   }
  1561.   matrices_pavage[nummatrix][0] = 1;
  1562.   matrices_pavage[nummatrix][1] = 0;
  1563.   matrices_pavage[nummatrix][2] = 0;
  1564.   matrices_pavage[nummatrix][3] = 1;
  1565.  
  1566. }
  1567. /* arguments: un numero de vecteur entre 1 et JC_NB_MATRICES, une liste de 2 nombres reels pour le vecteur */
  1568. void obj_setvector(objparm *pm)
  1569. {
  1570.   int nummatrix = (int) (pm->pd[0]);
  1571.   if((nummatrix < 1) ||(nummatrix>JC_NB_MATRICES)) {
  1572.     fly_error("bad_vector_number");
  1573.     return;
  1574.   }
  1575.   vecteurs_pavage[nummatrix][0] = pm->pd[1];
  1576.   vecteurs_pavage[nummatrix][1] = pm->pd[2];
  1577. }
  1578.  
  1579. /* arguments: un numero de matrice entre 1 et JC_NB_MATRICES */
  1580. void obj_resetvector(objparm *pm)
  1581. {
  1582.   int nummatrix = (int) (pm->pd[0]);
  1583.   if((nummatrix < 1) ||(nummatrix>JC_NB_MATRICES)) {
  1584.     fly_error("bad_vector_number");
  1585.     return;
  1586.   }
  1587.   vecteurs_pavage[nummatrix][0] = 0;
  1588.   vecteurs_pavage[nummatrix][1] = 0;
  1589. }
  1590.  
  1591. /* arguments: un numero de vecteur entre 1 et JC_NB_MATRICES, une liste de 6 nombres reels pour la matrice et le vecteur */
  1592. void obj_settransform(objparm *pm)
  1593. {
  1594.   int nummatrix = (int) (pm->pd[0]);
  1595.   if((nummatrix < 1) ||(nummatrix>JC_NB_MATRICES)) {
  1596.     fly_error("bad_vector_number");
  1597.     return;
  1598.   }
  1599.   matrices_pavage[nummatrix][0] = pm->pd[1];
  1600.   matrices_pavage[nummatrix][1] = pm->pd[2];
  1601.   matrices_pavage[nummatrix][2] = pm->pd[3];
  1602.   matrices_pavage[nummatrix][3] = pm->pd[4];
  1603.   vecteurs_pavage[nummatrix][0] = pm->pd[5];
  1604.   vecteurs_pavage[nummatrix][1] = pm->pd[6];
  1605. }
  1606.  
  1607.  
  1608. /* arguments: un numero de matrice entre 1 et JC_NB_MATRICES */
  1609. void obj_resettransform(objparm *pm)
  1610. {
  1611.   int nummatrix = (int) (pm->pd[0]);
  1612.   if((nummatrix < 1) ||(nummatrix>JC_NB_MATRICES)) {
  1613.     fly_error("bad_vector_number");
  1614.     return;
  1615.   }
  1616.   vecteurs_pavage[nummatrix][0] = 0;
  1617.   vecteurs_pavage[nummatrix][1] = 0;
  1618.   matrices_pavage[nummatrix][0] = 1;
  1619.   matrices_pavage[nummatrix][1] = 0;
  1620.   matrices_pavage[nummatrix][2] = 0;
  1621.   matrices_pavage[nummatrix][3] = 1;
  1622. }
  1623.  
  1624. /* arguments: une liste de 6 nombres reels pour les coordonnees de l'origine et des vecteurs de base */
  1625. void obj_setparallelogram(objparm *pm)
  1626. {
  1627.   int i;
  1628.   for(i=0;i<6;i++)  parallogram_fonda[i]=pm->pd[i];
  1629. }
  1630.  
  1631. /* arguments :aucun */
  1632. void obj_resetparallelogram(objparm *pm)
  1633. {
  1634.   parallogram_fonda[0]=0;
  1635.   parallogram_fonda[1]=0;
  1636.   parallogram_fonda[2]=100;
  1637.   parallogram_fonda[3]=0;
  1638.   parallogram_fonda[4]=0;
  1639.   parallogram_fonda[5]=100;
  1640. }
  1641.  
  1642. /* argument : la liste des numeros des matrices a faire agir, le nom du fichier de l'image a inclure */
  1643. void obj_multicopy(objparm *pm)
  1644. {
  1645.   char *pp,*pe,*cptr;
  1646.   FILE *inf;
  1647.   gdImagePtr insimg;
  1648.   int i,j;
  1649.   int imax,jmax;
  1650.   int c; /* la couleur reperee */
  1651.   int mat;
  1652.   int nummatrices[JC_NB_MATRICES];
  1653.   double pt[2]; /* les coordonnees math. du point repere dans le parallelogramme */
  1654.   double ptr[2]; /* les coordonnees math. du point transforme */
  1655.   int pti[2]; /* les coordonnees img du point a placer sur le canevas */
  1656.   int t;
  1657.   /* gestion palette de couleur de l'image a inserer */
  1658.   int colorMap[gdMaxColors];
  1659.   int comptmat=0; /* combien de matrices en tout ? */
  1660.  
  1661.   for (i=0; (i<gdMaxColors); i++) {
  1662.     colorMap[i] = (-1);
  1663.   }
  1664.  
  1665.   /* Gestion des parametres la liste est censee finir avec le nom du fichier */
  1666.   t=pm->pcnt-1; /* il faut enlever le nom de fichier ! c'est le nombre de parametres en plus */
  1667.   pp=find_word_start(pm->str);
  1668.   /* visiblement find_word_start n'arrive pas a trouver le dernier mot dans un contexte ou le nombre de parameters est variable
  1669.      * on cherche donc l'emplacement de la derniere virgule:
  1670.      * il y en a une car t>0, on retrouve ensuite le debut de mot
  1671.      */
  1672.   for(pe=pp;*pe!=0;pe++);/* trouve la fin de la chaine */
  1673.   if(t>0){
  1674.     for(cptr = pe; cptr > pp && *cptr != ','; cptr--);
  1675.     pp=find_word_start(cptr+1);
  1676.   }
  1677.   pe=find_word_end(pp);*pe=0; /* strip junk final */
  1678.   //      printf("pp contient %s\n",pp);
  1679.  
  1680.  
  1681.   inf=open4read(pp);
  1682.   if(inf==NULL) {
  1683.     fly_error("file_not_exist"); return;
  1684.   }
  1685.   insimg=gdImageCreateFromGif(inf); fclose(inf);
  1686.   if(insimg==NULL) {
  1687.     fly_error("bad_gif"); return;
  1688.   }
  1689.  
  1690.   /* On recupere les numeros des matrices/vecteurs a faire agir,
  1691.     * s'il n'y en a pas, on les fait toutes agir
  1692.   */
  1693.   for(i=0;i<t && i< JC_NB_MATRICES;i++) {
  1694.     if(pm->pd[i]>=1 && pm->pd[i]<=JC_NB_MATRICES){
  1695.       nummatrices[comptmat] = pm->pd[i];
  1696.       comptmat++;
  1697.     }
  1698.   }
  1699.   if(t<=0){
  1700.     for(i=0;i<JC_NB_MATRICES;i++) {
  1701.       nummatrices[i] = i+1;
  1702.     }
  1703.     comptmat=JC_NB_MATRICES;
  1704.   }
  1705.  
  1706.  
  1707.   imax = gdImageSX(insimg);
  1708.   jmax = gdImageSY(insimg);
  1709.  
  1710.   for(i=0;i<imax;i++){
  1711.     for(j=0;j<jmax;j++){
  1712.       int nc;
  1713.       c=gdImageGetPixel(insimg,i,j);
  1714.       /* Le code suivant est une copie du code dans gdImageCopy  Added 7/24/95: support transparent copies */
  1715.       if (gdImageGetTransparent(insimg) != c) {
  1716.         /* Have we established a mapping for this color? */
  1717.         if (colorMap[c] == (-1)) {
  1718.           /* If it's the same image, mapping is trivial, dans notre cas ca n'arrive jamais... */
  1719.           if (image == insimg) {
  1720.             nc = c;
  1721.           } else {
  1722.             /* First look for an exact match */
  1723.             nc = gdImageColorExact(image,
  1724.                    insimg->red[c], insimg->green[c],
  1725.                            insimg->blue[c]);
  1726.           }
  1727.           if (nc == (-1)) {
  1728.           /* No, so try to allocate it */
  1729.             nc = gdImageColorAllocate(image,
  1730.                              insimg->red[c], insimg->green[c],
  1731.                              insimg->blue[c]);
  1732.              /* If we're out of colors, go for the closest color */
  1733.             if (nc == (-1)) {
  1734.               nc = gdImageColorClosest(image,
  1735.                               insimg->red[c], insimg->green[c],
  1736.                               insimg->blue[c]);
  1737.             }
  1738.           }
  1739.           colorMap[c] = nc;
  1740.         }
  1741.  
  1742.         pt[0]= i*(parallogram_fonda[2])/(imax-1)+(jmax-j)*(parallogram_fonda[4])/(jmax-1)+parallogram_fonda[0];
  1743.         pt[1]= i*(parallogram_fonda[3])/(imax-1)+(jmax-j)*(parallogram_fonda[5])/(jmax-1)+parallogram_fonda[1];
  1744.         for(mat=0;mat<comptmat;mat++){
  1745.           double *matricecourante = matrices_pavage[nummatrices[mat]];
  1746.           double *vecteurcourant = vecteurs_pavage[nummatrices[mat]];
  1747.           ptr[0] = matricecourante[0]*pt[0]+matricecourante[1]*pt[1]+vecteurcourant[0];
  1748.           ptr[1] = matricecourante[2]*pt[0]+matricecourante[3]*pt[1]+vecteurcourant[1];
  1749.           scale(ptr,pti,1);
  1750.           gdImageSetPixel(image,pti[0],pti[1],colorMap[c]);
  1751.         }
  1752.       }
  1753.     }
  1754.   }
  1755.   gdImageDestroy(insimg);
  1756. }
  1757.  
  1758. /**** Fin modifs JC Fev 06 ******/
  1759.  
  1760. /* get color from value */
  1761. int calc_color(char *p, struct objtab *o)
  1762. {
  1763.   int k, cc[3];
  1764.   char buf[MAX_LINELEN+1];
  1765.   for(k=0;k<3;k++) {
  1766.     fnd_item(p,k+1,buf);
  1767.     cc[k]=strevalue(buf);
  1768.   }
  1769.   collapse_item(p,3);
  1770.   if(cc[0]==-1 && cc[1]==-1 && cc[2]==-255) {
  1771.  
  1772.     if(brushed && o->subst&1) return gdBrushed;
  1773.     else return color_black;
  1774.   }
  1775.   if(cc[0]==-1 && cc[1]==-255 && cc[2]==-1) {
  1776.     if(styled && o->subst&2)  return gdStyled;
  1777.     else return color_black;
  1778.   }
  1779.   if(cc[0]==-255 && cc[1]==-1 && cc[2]==-1) {
  1780.     if(tiled && o->fill_tag==1)  return gdTiled;
  1781.     else return color_black;
  1782.   }
  1783.   return getcolor(cc[0],cc[1],cc[2]);
  1784. }
  1785.  
  1786. /* Routine to parse parameters */
  1787. int parse_parms(char *p,objparm *pm,struct objtab *o)
  1788. {
  1789.   char buf[MAX_LINELEN+1];
  1790.   char *p1, *p2;
  1791.   int j,t,c,c1,c2;
  1792.  
  1793.   c=o->color_pos;c1=c2=0;
  1794.   pm->color[0]=pm->color[1]=0;
  1795.   if(c>0) c1=c;
  1796.   if(c<0) c2=-c;
  1797.   c=c1+c2;
  1798.   t=itemnum(p);if(t<o->required_parms+3*c) return -1;
  1799.   if(c1>0 && t>o->required_parms+3*c) t=o->required_parms+3*c;
  1800.   pm->pcnt=t-3*c;
  1801.   if(pm->pcnt>MAX_PARMS) pm->pcnt=MAX_PARMS;
  1802.   if(c2>0) {
  1803.     for(j=0;j<2 && j<c2; j++) pm->color[j]=calc_color(p,o);
  1804.   }
  1805.   snprintf(buf,sizeof(buf),"%s",p);
  1806.   for(j=0, p1=buf; j<pm->pcnt; j++, p1=p2) {
  1807.     p2=find_item_end(p1); if(*p2) *p2++=0;
  1808.     p1=find_word_start(p1);
  1809.     if(*p1) pm->pd[j]=strevalue(p1); else pm->pd[j]=0;
  1810.     if(!isfinite(pm->pd[j])) {
  1811.         if(j<o->required_parms) return -1;
  1812.         else {pm->pd[j]=0;break;}
  1813.     }
  1814.   }
  1815.   collapse_item(p,o->required_parms);
  1816.   if(c1>0) {
  1817.     for(j=0;j<c1 && j<2; j++) pm->color[j]=calc_color(p,o);
  1818.   }
  1819.   if(width>1 && o->subst&1 && pm->color[0]!=gdBrushed
  1820.       && pm->color[0]!=gdStyled && pm->color[0]!=gdTiled) {
  1821.     pm->color[1]=pm->color[0];
  1822.     pm->color[0]=widthcolor(width,pm->color[0]);
  1823.   }
  1824.   pm->fill=o->fill_tag;
  1825.   if(o->required_parms!=0){
  1826.     if(dashed) {
  1827.       if(pm->fill==1) pm->fill=2;
  1828.       if(pm->fill==0) pm->fill=-1;
  1829.        if(noreset==0) dashed=0;
  1830.     }
  1831.     if(filled) {
  1832.       if(pm->fill==-1) pm->fill=2;
  1833.       if(pm->fill==0) pm->fill=1;
  1834.       if(noreset==0) filled=0;
  1835.     }
  1836.   }
  1837.   ovlstrcpy(pm->str,p); return 0;
  1838. }
  1839.  
  1840. /* Create the struct objparm pm corresponding to the objparm p
  1841.  * Execute a command. Returns 0 if OK.
  1842.  */
  1843. int obj_main(char *p)
  1844. {
  1845.   int i;
  1846.   char *pp,*name,*pt;
  1847.   char tbuf2[MAX_LINELEN+1];
  1848.   struct objparm pm;
  1849.  
  1850.   p=find_word_start(p);
  1851.   if(*p==exec_prefix_char || *p==0) return 0; /* comment */
  1852.   name=p;
  1853.   pp=find_name_end(p);
  1854.   pt=find_word_start(pp); if(*pt=='=') *pt=' ';
  1855.   if(*pt==':' && *(pt+1)=='=') *pt=*(pt+1)=' ';
  1856.   pp=find_word_end(p);
  1857.   if(*pp!=0) {
  1858.     *(pp++)=0; pp=find_word_start(pp);
  1859.   }
  1860.   if(strlen(p)==1 || (strlen(p)==2 && isdigit(*(p+1)))) {
  1861.     setvar(p,strevalue(pp)); return 0;
  1862.   }
  1863.   /* search the number of the object */
  1864.   i=search_list(objtab,obj_no,sizeof(objtab[0]),name);
  1865.   if(i<0) {
  1866.     fly_error("bad_cmd"); return 1;
  1867.   }
  1868.   if(image==NULL && (objtab[i].color_pos || objtab[i].required_parms>2)) {
  1869.     fly_error("image_not_defined"); return 1;
  1870.   }
  1871.   ovlstrcpy(tbuf2,pp);
  1872.   if(objtab[i].color_pos || objtab[i].routine==obj_setstyle) {
  1873.     substit(tbuf2);
  1874.   }
  1875.   if(parse_parms(tbuf2,&pm,objtab+i)!=0) fly_error("bad_parms");
  1876.   else objtab[i].routine(&pm);
  1877.   return 0;
  1878. }
  1879.  
  1880. /* for hyperbolic geodesics, return data for obj_hypgeods + a point on the arc
  1881.   to be used for filling hyperbolic triangle*/
  1882. int hypgeodaux(double *q, double* res)
  1883. {
  1884.   double r,cx,cy,a1,a2,a3,tmp,
  1885.     nx = -q[0]*q[2]*q[2]+(q[0]*q[0]+q[1]*q[1]+1)*q[2]-q[0]*q[3]*q[3]-q[0],
  1886.     ny = -q[1]*q[2]*q[2]-q[1]*q[3]*q[3]+(q[0]*q[0]+q[1]*q[1]+1)*q[3]-q[1],
  1887.     dy = -2*q[1]*q[2]+2*q[0]*q[3];
  1888.   if (dy*dy*1e4 < nx*nx+ny*ny){
  1889.     res[5]=(q[0]+q[2])/2;
  1890.     res[6]=(q[1]+q[3])/2;
  1891.     return 0;}
  1892.   cx = ny/dy; cy=-nx/dy;
  1893.   a1 = atan2(q[1]-cy, q[0]-cx);
  1894.   a2 = atan2(q[3]-cy, q[2]-cx);
  1895.   if (fabs(a2-a1)>M_PI){if(a1<a2) a1+=2*M_PI; else a2+=2*M_PI;};
  1896.   a3 = (a1+a2)/2;
  1897.   if(a1>a2) {tmp=a1; a1=a2; a2=tmp;}
  1898.   r = sqrt(cx*cx+cy*cy-1);
  1899.   res[0]=cx;
  1900.   res[1]=cy;
  1901.   res[2]=r;
  1902.   res[3]=a1;
  1903.   res[4]=a2;
  1904.   res[5]=cx+r*cos(a3);
  1905.   res[6]=cy+r*sin(a3);
  1906.   return 1;
  1907. }
  1908. /* hyperbolic geodesics from x1,y1 to x2,y2, x3,y3 to x4,y4 in Poincaré disk */
  1909. void obj_hypgeods(objparm *pm)
  1910. {
  1911.   int i, *qq=pm->p;
  1912.   double rx,ry,res[7];
  1913.   if (tikz_file) fprintf(tikz_file, "\\draw[%s]",tikz_options(pm->color[0],pm->fill));
  1914.   for (i = 0; i < pm->pcnt; i+=4)
  1915.     if (hypgeodaux(pm->pd+i,res)){
  1916.       scale2(res[2],res[2],&rx,&ry);
  1917.       scale(res,qq,1);
  1918.       myGdImageArc(image,qq[0],qq[1],rx,ry,res[3]/DEG,res[4]/DEG,pm->color[0]);
  1919.       if(tikz_file)
  1920.         fprintf(tikz_file,"(%i,%i)arc(%f:%f:%i and %i)",
  1921.           (int)rint(qq[0]+rx*cos(res[3])),
  1922.           flip((int)rint(qq[1]+ry*sin(res[3]))),
  1923.           res[3]/DEG,res[4]/DEG,(int)rint(rx),-(int)rint(ry));
  1924.    }
  1925.     else {
  1926.       scale(pm->pd+i,qq,2);
  1927.       gdImageLine(image,qq[0],qq[1],qq[2],qq[3],pm->color[0]);
  1928.       if(tikz_file) fprintf(tikz_file,"(%i,%i)--(%i,%i)",
  1929.           qq[0],flip(qq[1]),qq[2],flip(qq[3]));
  1930.     }
  1931.   if(tikz_file) fprintf(tikz_file,";\n");
  1932. }
  1933.  
  1934. /* hyperbolic triangle, can be filled */
  1935. void obj_hyptriangle(objparm *pm)
  1936. {
  1937.   double rx,ry,data[8],res[7];
  1938.   int i, *qq=pm->p;
  1939.   for(i=0; i<8; i++) data[i]=pm->pd[i%6];
  1940.   if (tikz_file) fprintf(tikz_file, "\\draw[%s]",tikz_options(pm->color[0],0));
  1941.   for(i=0; i<3; i++)
  1942.     if(hypgeodaux(data+2*i,res)){
  1943.       scale2(res[2],res[2],&rx,&ry);
  1944.       scale(res,qq,1);
  1945.       myGdImageArc(image,qq[0],qq[1],rx,ry,res[3]/DEG,res[4]/DEG,pm->color[0]);
  1946.       if(tikz_file)
  1947.         fprintf(tikz_file, "(%i,%i) arc (%f:%f:%i and %i)",
  1948.           (int)rint(qq[0]+rx*cos(res[3])),
  1949.           flip((int)rint(qq[1]+ry*sin(res[3]))),
  1950.           res[3]/DEG,res[4]/DEG,(int)rint(rx),-(int)rint(ry));
  1951.     }
  1952.     else {
  1953.       scale(data+2*i,qq,2);
  1954.       gdImageLine(image,qq[0],qq[1],qq[2],qq[3],pm->color[0]);
  1955.       if(tikz_file) fprintf(tikz_file,"(%i,%i)--(%i,%i)",
  1956.             qq[0],flip(qq[1]),qq[2],flip(qq[3]));
  1957.     }
  1958.     if(tikz_file) fprintf(tikz_file,";\n");
  1959.     if(pm->fill){
  1960.       data[0]=res[5];
  1961.       data[1]=res[6];
  1962.       hypgeodaux(data,res);
  1963.       scale(res+5,qq,1);
  1964.       //patchgdImageFill(image,qq[0],qq[1],pm->color[0]);
  1965.       patchgdImageFillToBorder(image,qq[0],qq[1],pm->color[0],pm->color[0]);
  1966.       if(tikz_file) tikz_fill(qq[0],qq[1],1,1,0,pm->color[0],grid);
  1967.     }
  1968. }
  1969.  
  1970. /* complete hyperbolic geodesic through the two first points, then the following two points, etc */
  1971.  
  1972. void obj_hyplines(objparm *pm)
  1973. {
  1974.   double rx,ry,res[7],*pd = pm->pd;
  1975.   int i;
  1976.   if (tikz_file) fprintf(tikz_file, "\\draw[%s]",tikz_options(pm->color[0],0));
  1977.   for (i=0; i < pm->pcnt; i+=4,pd+=4)
  1978.     if (hypgeodaux(pd,res)) {
  1979.       double alpha=atan(1/res[2]), beta = atan2(res[1],res[0]);
  1980.       scale(res,pm->p,1);
  1981.       scale2(res[2],res[2],&rx,&ry);
  1982.       myGdImageArc(image,pm->p[0],pm->p[1],rx,ry,180+(beta-alpha)/DEG,180+(beta+alpha)/DEG,pm->color[0]);
  1983.       if (tikz_file) fprintf(tikz_file, "(%i,%i) arc (%f:%f:%i and %i);\n",
  1984.         (int)rint(pm->p[0]-rx*cos(beta-alpha)), flip((int)rint(pm->p[1]-ry*sin(beta-alpha))),
  1985.           180+(beta-alpha)/DEG,180+(beta+alpha)/DEG, (int)rint(rx),-(int)rint(ry));
  1986.     }
  1987.     else {
  1988.       double gamma;
  1989.       if (pd[1]*pd[1]+pd[0]*pd[0] > pd[2]*pd[2]+pd[3]*pd[3])
  1990.         gamma = atan2(pd[1],pd[0]);
  1991.       else
  1992.         gamma = atan2(pd[3],pd[2]);
  1993.         res[0]=cos(gamma);
  1994.         res[1]=sin(gamma);
  1995.         res[2]=-cos(gamma);
  1996.         res[3]=-sin(gamma);
  1997.         scale(res,pm->p,2);
  1998.         gdImageLine(image,pm->p[0],pm->p[1],pm->p[2],pm->p[3],pm->color[0]);
  1999.         if(tikz_file) fprintf(tikz_file, "(%i,%i)--(%i,%i)",
  2000.           pm->p[0],flip(pm->p[1]),pm->p[2],flip(pm->p[3]));
  2001.     }
  2002.     if (tikz_file) fprintf(tikz_file, ";\n");
  2003. }
  2004.