Subversion Repositories wimsdev

Rev

Rev 18432 | Rev 18455 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
17676 bpr 1
/*    Copyright (C) 1998-2003 XIAO, Gang of Universite de Nice - Sophia Antipolis
10 reyssat 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
 */
8177 bpr 17
 
18
#include <errno.h>
8102 bpr 19
#include "flydraw.h"
18317 bpr 20
const double DEG=M_PI/180;
8177 bpr 21
 
17574 bpr 22
/* Tikz things */
17599 bpr 23
 
17608 bpr 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
 
17574 bpr 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;
17608 bpr 39
  sprintf (buf, "draw=%s,cap=round",tikz_color(the_color));
17574 bpr 40
  if (the_color != tikz_brushColor) strcat(buf, ",thin");
18125 bpr 41
  if (the_fill == -1 || the_fill == 2) strcat(buf, ",dashed");
42
  if (the_fill == 1 || the_fill == 2)
17608 bpr 43
    sprintf(buf+strlen(buf), ",fill=%s", tikz_color(the_color));
17574 bpr 44
  return buf;
45
}
46
 
17662 bpr 47
#define flip(y) (sizey-1-(y))
17574 bpr 48
 
8010 bpr 49
/* bug in gdImageFillToBorder */
10 reyssat 50
 
8162 bpr 51
void patchgdImageFillToBorder (gdImagePtr im, int x, int y, int border, int color)
8076 bpr 52
{
12473 bpr 53
  if(x>=im->sx || x<0 || y>=im->sy || y<0) return;
54
  gdImageFillToBorder(im,x,y,border,color);
8076 bpr 55
}
56
 
8162 bpr 57
void patchgdImageFill (gdImagePtr im, int x, int y, int color)
8156 bpr 58
{
12473 bpr 59
  if(x>=im->sx || x<0 || y>=im->sy || y<0) return;
60
  gdImageFill(im,x,y,color);
8156 bpr 61
}
8076 bpr 62
 
9651 georgesk 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
 
7675 bpr 80
/* File opening: with security */
10 reyssat 81
FILE *open4read(char *n)
82
{
12473 bpr 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;
10 reyssat 108
    }
12473 bpr 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);
7675 bpr 112
      f=fopen(namebuf,"r");
10 reyssat 113
    }
12473 bpr 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;
10 reyssat 130
}
131
 
7675 bpr 132
/* Does nothing; just a comment. */
10 reyssat 133
void obj_comment(objparm *pm)
134
{
12473 bpr 135
  return;
10 reyssat 136
}
137
 
7675 bpr 138
/* define image size */
10 reyssat 139
void obj_size(objparm *pm)
140
{
12473 bpr 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);
17574 bpr 149
  if(tikz_file) fprintf(tikz_file,"\\clip (0,0) rectangle (%i, %i);\n", sizex, sizey);
12473 bpr 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
  }
10 reyssat 157
}
158
 
7675 bpr 159
/* new image */
10 reyssat 160
void obj_new(objparm *pm)
161
{
12473 bpr 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;
10 reyssat 168
}
169
 
7675 bpr 170
/* new image */
10 reyssat 171
void obj_existing(objparm *pm)
172
{
12473 bpr 173
  FILE *inf;
174
  char *pp;
7615 bpr 175
 
12473 bpr 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;
10 reyssat 190
}
18066 bpr 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
}
10 reyssat 206
 
18123 bpr 207
/* segment */
10 reyssat 208
void obj_line(objparm *pm)
209
{
12473 bpr 210
  scale(pm->pd,pm->p,2);
18125 bpr 211
  if(pm->fill==-1 || pm->fill==2 )
18123 bpr 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]);
17574 bpr 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]));
12473 bpr 218
  if(vimg_enable) vimg_line(scale_buf[0],scale_buf[1],scale_buf[2],scale_buf[3]);
10 reyssat 219
}
220
 
1024 bpr 221
void _obj_arrow(objparm *pm, int twoside)
10 reyssat 222
{
12473 bpr 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]);
17578 bpr 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);
12473 bpr 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;
8163 bpr 248
    gdImageFilledPolygon(image, ii,3,pm->color[0]);
17578 bpr 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));
12473 bpr 252
  }
18125 bpr 253
  stem: if(pm->fill==-1 || pm->fill==2)
12473 bpr 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]);
17578 bpr 257
  if(tikz_file)
258
    fprintf(tikz_file, "\\draw\[%s] (%i, %i) -- (%i, %i);\n",
17584 bpr 259
      tikz_options(pm->color[0],pm->fill),pm->p[0],flip(pm->p[1]),xx,flip(yy));
12473 bpr 260
  if(vimg_enable) vimg_line(scale_buf[0],scale_buf[1],scale_buf[2],scale_buf[3]);
10 reyssat 261
}
262
 
7675 bpr 263
/* Arrow */
1024 bpr 264
void obj_arrow(objparm *pm)
265
{
12473 bpr 266
  _obj_arrow(pm,0);
1024 bpr 267
}
268
 
7675 bpr 269
/* 2-sided arrow */
1024 bpr 270
void obj_arrow2(objparm *pm)
271
{
12473 bpr 272
   _obj_arrow(pm,1);
1024 bpr 273
}
274
 
18047 bpr 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){
18119 bpr 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)
18065 bpr 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));
18047 bpr 306
      }
18125 bpr 307
    stem: if(pm->fill==-1 || pm->fill==2)
18065 bpr 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
    }
18047 bpr 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
 
7675 bpr 329
/* horizontal line */
10 reyssat 330
void obj_hline(objparm *pm)
331
{
12473 bpr 332
  scale(pm->pd,pm->p,1);
18125 bpr 333
  if(pm->fill==-1 || pm->fill==2)
12473 bpr 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]);
17574 bpr 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]));
10 reyssat 340
}
341
 
18119 bpr 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
}
18066 bpr 353
 
18065 bpr 354
/* halfline */
355
void obj_halfline(objparm *pm)
356
{
18119 bpr 357
  int xend,yend;
18065 bpr 358
  scale(pm->pd,pm->p,2);
18119 bpr 359
  line_extend(pm->p[0],pm->p[1],pm->p[2],pm->p[3],&xend,&yend);
18125 bpr 360
  if(pm->fill==-1 || pm->fill==2)
18119 bpr 361
    myDashedLine(image,pm->p[0],pm->p[1],xend,yend,pm->color[0]);
18065 bpr 362
  else
18119 bpr 363
    gdImageLine(image,pm->p[0],pm->p[1],xend,yend,pm->color[0]);
18065 bpr 364
  if (tikz_file)
365
    fprintf(tikz_file, "\\draw\[%s] (%i, %i) -- (%i, %i);\n",
366
      tikz_options(pm->color[0],pm->fill),
18119 bpr 367
      pm->p[0],flip(pm->p[1]),xend,flip(yend));
18065 bpr 368
}
17574 bpr 369
 
18119 bpr 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);
18125 bpr 376
  if(pm->fill==-1 || pm->fill==2)
18119 bpr 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);
18125 bpr 394
    if(pm->fill==-1 || pm->fill==2)
18119 bpr 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
 
7675 bpr 405
/* vertical line */
10 reyssat 406
void obj_vline(objparm *pm)
407
{
12473 bpr 408
  scale(pm->pd,pm->p,1);
18125 bpr 409
  if(pm->fill==-1 || pm->fill==2)
12473 bpr 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]);
17574 bpr 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);
10 reyssat 416
}
417
 
7675 bpr 418
/* dashed line */
10 reyssat 419
void obj_dline(objparm *pm)
420
{
12473 bpr 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]);
17574 bpr 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]));
10 reyssat 427
}
428
 
7675 bpr 429
/* parallel lines.
430
 * x1,y1,x2,y2,xv,yv,n,color */
10 reyssat 431
void obj_parallel(objparm *pm)
432
{
18123 bpr 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);
18125 bpr 442
    if(pm->fill==-1 || pm->fill==2)
18123 bpr 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
  }
17914 bpr 456
 if(tikz_file) fprintf(tikz_file,";\n");
10 reyssat 457
}
458
 
7675 bpr 459
/* rectangle */
10 reyssat 460
void obj_rect(objparm *pm)
461
{
12473 bpr 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]);
18140 bpr 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
  }
12473 bpr 476
  else
477
    gdImageRectangle(image,x1,y1,x2,y2,pm->color[0]);
17584 bpr 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));
12473 bpr 482
  if(vimg_enable) vimg_rect(scale_buf[0],scale_buf[1],scale_buf[2],scale_buf[3]);
10 reyssat 483
}
484
 
7675 bpr 485
/* square */
10 reyssat 486
void obj_square(objparm *pm)
487
{
12473 bpr 488
  int w,h;
489
  scale(pm->pd,pm->p,1);
490
  w=rint(pm->pd[2]); h=rint(pm->pd[2]);
18142 bpr 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)
12473 bpr 499
    gdImageFilledRectangle(image,pm->p[0],pm->p[1],
18123 bpr 500
        pm->p[0]+w,pm->p[1]+h,pm->color[0]);
18142 bpr 501
  }
12473 bpr 502
  else
503
    gdImageRectangle(image,pm->p[0],pm->p[1],
504
               pm->p[0]+w,pm->p[1]+h,pm->color[0]);
17574 bpr 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
  }
12473 bpr 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]);
10 reyssat 513
}
514
 
7675 bpr 515
/* triangle */
10 reyssat 516
void obj_triangle(objparm *pm)
517
{
12473 bpr 518
  scale(pm->pd,pm->p,3);
18131 bpr 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
  }
12473 bpr 529
  else
530
    gdImagePolygon(image,(gdPointPtr) pm->p,3,pm->color[0]);
17574 bpr 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]));
12473 bpr 534
  if(vimg_enable) vimg_polyline(scale_buf,3,1);
10 reyssat 535
}
536
 
7675 bpr 537
/* polygon */
10 reyssat 538
void obj_poly(objparm *pm)
539
{
17574 bpr 540
  int cnt,i;
12473 bpr 541
  cnt=(pm->pcnt)/2;
542
  scale(pm->pd,pm->p,cnt);
18125 bpr 543
  if(pm->fill==1 || pm->fill==2)
12473 bpr 544
    gdImageFilledPolygon(image,(gdPointPtr) pm->p,cnt,pm->color[0]);
545
  else
546
    gdImagePolygon(image,(gdPointPtr) pm->p,cnt,pm->color[0]);
17614 bpr 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
  }
12473 bpr 553
  if(vimg_enable) vimg_polyline(scale_buf,cnt,1);
10 reyssat 554
}
555
 
7675 bpr 556
/* rays */
10 reyssat 557
void obj_rays(objparm *pm)
558
{
12473 bpr 559
  int i, n;
560
  n=(pm->pcnt)/2;
561
  scale(pm->pd,pm->p,n);
17920 bpr 562
  if (tikz_file)
563
    fprintf(tikz_file, "\\draw\[%s]",tikz_options(pm->color[0],pm->fill));
12473 bpr 564
  for(i=2;i<2*n;i+=2) {
18125 bpr 565
    if(pm->fill==-1 || pm->fill==2)
18123 bpr 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]);
17574 bpr 569
    if (tikz_file)
17920 bpr 570
      fprintf(tikz_file, "(%i,%i) -- (%i,%i)",
571
        pm->p[0],flip(pm->p[1]),pm->p[i],flip(pm->p[i+1]));
12473 bpr 572
    if(vimg_enable) vimg_line(scale_buf[0],scale_buf[1],
573
                        scale_buf[i],scale_buf[i+1]);
574
  }
17920 bpr 575
  if (tikz_file) fprintf(tikz_file,";\n");
10 reyssat 576
}
8414 bpr 577
/* crosshair */
578
void obj_crosshair(objparm *pm)
579
{
12473 bpr 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]);
17574 bpr 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
  }
8414 bpr 589
}
590
/* crosshairs */
10 reyssat 591
 
8414 bpr 592
void obj_crosshairs(objparm *pm)
593
{
12473 bpr 594
  int i, n;
595
  n=(pm->pcnt)/2;
596
  scale(pm->pd,pm->p,n);
17921 bpr 597
  if (tikz_file)
598
      fprintf(tikz_file, "\\draw\[%s]",tikz_options(pm->color[0],pm->fill));
12473 bpr 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]);
17574 bpr 602
    if (tikz_file){
17921 bpr 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));
17574 bpr 607
    }
12473 bpr 608
  }
17921 bpr 609
  if (tikz_file) fprintf(tikz_file,";\n");
8414 bpr 610
}
611
 
612
 
7675 bpr 613
/* segments */
10 reyssat 614
void obj_lines(objparm *pm)
615
{
17914 bpr 616
 int i, n;
617
 n=(pm->pcnt)/2;
618
 scale(pm->pd,pm->p,n);
18123 bpr 619
 if (tikz_file){
17914 bpr 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){
18125 bpr 627
  if(pm->fill==-1 || pm->fill==2 )
18123 bpr 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]);
17914 bpr 631
  if(vimg_enable) vimg_polyline(scale_buf,n,0);
17574 bpr 632
  }
10 reyssat 633
}
16344 bpr 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;
18317 bpr 638
  n=(pm->pcnt)/2;
639
  scale(pm->pd,pm->p,n);
17914 bpr 640
  if (tikz_file)
641
    fprintf(tikz_file, "\\draw\[%s]",tikz_options(pm->color[0],pm->fill));
18317 bpr 642
  for(i=0;i<2*n;i+=4){
18125 bpr 643
    if(pm->fill==-1 || pm->fill==2)
18123 bpr 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]);
17914 bpr 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]));
17574 bpr 650
  }
17914 bpr 651
  if(tikz_file) fprintf(tikz_file, ";\n");
16344 bpr 652
}
18317 bpr 653
 
7675 bpr 654
/* segments */
10 reyssat 655
void obj_dlines(objparm *pm)
656
{
17914 bpr 657
 int i, n;
658
 n=(pm->pcnt)/2;
659
 scale(pm->pd,pm->p,n);
18123 bpr 660
 if (tikz_file){
17914 bpr 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);
10 reyssat 670
}
671
 
7675 bpr 672
/* points */
10 reyssat 673
void obj_points(objparm *pm)
674
{
17914 bpr 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
 }
10 reyssat 685
}
686
 
7675 bpr 687
/* lattice.
688
 * x0,y0,xv1,yv1,xv2,yv2,n1,n2,color */
10 reyssat 689
void obj_lattice(objparm *pm)
690
{
17914 bpr 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");
10 reyssat 710
}
711
 
7675 bpr 712
/* arc */
18317 bpr 713
/*trouble with gdImageArc as the angles are of type int */
18350 bpr 714
void myGdImageArc(gdImagePtr im, double cx, double cy, double rx, double ry, double t1, double t2, int color)
10 reyssat 715
{
18308 bpr 716
  int i,nb;
18317 bpr 717
  double dt, r=rx>ry?rx:ry;
18308 bpr 718
  t2-=t1;
18317 bpr 719
  t2-=360*floor(t2/360);
18325 bpr 720
  if(t2==0) t2=360;
18317 bpr 721
  t1*=DEG;t2*=DEG;
18308 bpr 722
  nb = r*t2/3;
723
  dt = t2/nb;
724
  for (i=0; i<nb; ++i,t1+=dt)
18350 bpr 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);
18317 bpr 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];
17914 bpr 738
    fprintf(tikz_file,
18317 bpr 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);
17574 bpr 744
  }
745
  /*FIXME echelle mauvaise*/
12473 bpr 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]);
10 reyssat 748
}
749
 
17676 bpr 750
void obj_angle(objparm *pm)
751
{
18320 bpr 752
/* dans la doc: mettre les angles dans l'ordre ???? demander qu'ils soient entre 0 et 360 */
17676 bpr 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);
18350 bpr 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]);
18308 bpr 764
  gdImageArc(image,pts[0],pts[1],wx,wy,pm->pd[3],pm->pd[4],pm->color[0]);
17676 bpr 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));
17950 bpr 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]));
17676 bpr 770
  }
771
}
7675 bpr 772
/* Ellipse: centre 0,1, width 2, hight 3, color 4,5,6 */
10 reyssat 773
void obj_ellipse(objparm *pm)
774
{
12473 bpr 775
  scale(pm->pd,pm->p,1);
17676 bpr 776
  pm->p[2]=pm->pd[2]*xscale; pm->p[3]=pm->pd[3]*yscale;
18125 bpr 777
  if(pm->fill==1 || pm->fill==2) {
12473 bpr 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
  }
17676 bpr 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",
17912 bpr 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]));
17574 bpr 786
  if(vimg_enable) vimg_ellipse(scale_buf[0],scale_buf[1],0.5*pm->pd[2],0.5*pm->pd[3]);
10 reyssat 787
}
788
 
17656 bpr 789
/* Circle radius in pixels*/
10 reyssat 790
void obj_circle(objparm *pm)
791
{
12473 bpr 792
  scale(pm->pd,pm->p,1);
793
  pm->p[2]=rint(pm->pd[2]); pm->p[3]=rint(pm->pd[2]);
18125 bpr 794
  if(pm->fill==1 || pm->fill==2) {
12473 bpr 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]);
17676 bpr 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.));
10 reyssat 803
}
17656 bpr 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);
17942 bpr 810
  if (tikz_file) fprintf(tikz_file,"\\draw\[%s]",tikz_options(pm->color[0],pm->fill));
17656 bpr 811
  for(i=0;i<3*n;i+=3){
18125 bpr 812
    if(pm->fill==1 || pm->fill==2) {
18118 bpr 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
    }
17656 bpr 819
    gdImageArc(image,pm->p[i],pm->p[i+1],
18118 bpr 820
        rint(2*pm->pd[i+2]*xscale),rint(2*pm->pd[i+2]*xscale),0,360,pm->color[0]);
17656 bpr 821
    if (tikz_file){
17942 bpr 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));
17656 bpr 824
    }
825
  }
17942 bpr 826
  if (tikz_file) fprintf(tikz_file,";\n");
17656 bpr 827
}
17676 bpr 828
/* ellipse, width in xrange,height in yrange */
17656 bpr 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);
17943 bpr 834
  if (tikz_file) fprintf(tikz_file,"\\draw\[%s]",tikz_options(pm->color[0],pm->fill));
17656 bpr 835
  for(i=0;i<4*n;i+=4){
18125 bpr 836
    if(pm->fill==1 || pm->fill==2){
18123 bpr 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]);
17656 bpr 844
    if (tikz_file){
17943 bpr 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));
17656 bpr 847
    }
848
  }
17943 bpr 849
  if (tikz_file) fprintf(tikz_file,";\n");
17656 bpr 850
}
17940 bpr 851
typedef enum tikz_fill_options {grid, dot, hatch, diamond} tfo;
17599 bpr 852
int compar(const void *a, const void *b) {return *(int *)b - *(int *)a;}
853
 
17662 bpr 854
static void tikz_fill(int x, int y, int nx, int ny, int wall, int the_color, tfo opt)
17599 bpr 855
{
17662 bpr 856
  int numpix=sizex*sizey, top = 0, a, c, cy, lx=0, rx=0, seed, nt,ct=0;
17599 bpr 857
  int *stack = xmalloc(numpix*sizeof(int));
858
  int *stack2 = xmalloc(numpix*sizeof(int));
18132 bpr 859
  if (nx==0) nx=1;
860
  if (ny==0) ny=1;
861
  nt = abs(nx*ny);
17599 bpr 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;
17662 bpr 872
      c = gdImageGetPixel(image,x,y);
873
      if ((wall && (c == wall))||(!wall && c!=seed))
874
        continue;
17599 bpr 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)
17621 bpr 880
  {
17940 bpr 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;
17939 bpr 883
  case hatch: if((ny*x-nx*y+nt)%nt) continue; break;
17940 bpr 884
  case diamond: if((ny*x+nx*y)%nt && (ny*x-nx*y+nt)%nt)continue;
17621 bpr 885
  default: break;
886
  }
17599 bpr 887
      stack2[ct++]=a;
888
    }
889
  free(stack);
890
  free(check);
891
  if (ct==0) return;
17938 bpr 892
  fprintf(tikz_file,"\\draw\[%s]", tikz_options(the_color,1));
17599 bpr 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
      {
17621 bpr 901
  if (cy >= 0)
902
    fprintf(tikz_file,
17933 bpr 903
      "(%.2f,%.2f)rectangle(%.2f,%.2f)",
904
      lx-0.0,flip(cy-0.0),rx+0.0,flip(cy+0.0));
17621 bpr 905
  cy = y;
906
  lx = rx = x;
17599 bpr 907
      }
908
      else
17621 bpr 909
  rx++;
17599 bpr 910
    }
911
  fprintf(tikz_file,
17933 bpr 912
    "(%.2f,%.2f)rectangle(%.2f,%.2f);\n",
913
     lx-0.0,flip(cy-0.0),rx+0.0,flip(cy+0.0));
17599 bpr 914
  free(stack2);
915
}
18125 bpr 916
void obj_dashed(objparm *pm){ dashed=1;}
917
void obj_filled(objparm *pm){ filled=1;}
18159 bpr 918
void obj_noreset(objparm *pm){ noreset=1;}
919
void obj_reset(objparm *pm){ noreset=0;dashed=0;filled=0;}
17599 bpr 920
 
7675 bpr 921
/* flood fill */
10 reyssat 922
void obj_fill(objparm *pm)
923
{
12473 bpr 924
  scale(pm->pd,pm->p,1);
17940 bpr 925
  if (tikz_file) tikz_fill(pm->p[0],pm->p[1],1,1,0,pm->color[0],grid);
12473 bpr 926
  patchgdImageFill(image,pm->p[0],pm->p[1],pm->color[0]);
10 reyssat 927
}
928
 
7675 bpr 929
/* flood fill to border*/
10 reyssat 930
void obj_fillb(objparm *pm)
931
{
12473 bpr 932
  scale(pm->pd,pm->p,1);
17940 bpr 933
  if (tikz_file) tikz_fill(pm->p[0],pm->p[1],1,1,pm->color[0],pm->color[1],grid);
12473 bpr 934
  patchgdImageFillToBorder(image,pm->p[0],pm->p[1],pm->color[0],pm->color[1]);
10 reyssat 935
}
936
 
937
gdImagePtr himg;
938
 
939
int makehatchimage(int x, int y, int px, int py, int col)
940
{
12473 bpr 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;
10 reyssat 956
}
957
 
7675 bpr 958
/* flood fill with hatching */
10 reyssat 959
void obj_hatchfill(objparm *pm)
960
{
12473 bpr 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);
7675 bpr 977
      }
12473 bpr 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);
7675 bpr 987
      }
12473 bpr 988
      break;
989
    }
990
    case 2: gdImageLine(himg,0,ay/2,ax-1,ay/2,c); break;
17574 bpr 991
    case 3: gdImageLine(himg,ax/2,0,ax/2,ay-1,c); break;
12473 bpr 992
  }
17662 bpr 993
  if (tikz_file) tikz_fill(pm->p[0],pm->p[1],nx,ny,0,pm->color[0],hatch);
12473 bpr 994
  gdImageSetTile(image,himg);
995
  patchgdImageFill(image,pm->p[0],pm->p[1],gdTiled);
996
  gdImageDestroy(himg);
997
  if(tiled) gdImageSetTile(image,tileimg);
10 reyssat 998
}
999
 
7675 bpr 1000
/* flood fill with grid */
10 reyssat 1001
void obj_gridfill(objparm *pm)
1002
{
12473 bpr 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]);
17940 bpr 1008
  if (tikz_file) tikz_fill(pm->p[0],pm->p[1],nx,ny,0,pm->color[0],grid);
12473 bpr 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);
10 reyssat 1014
}
1015
 
7675 bpr 1016
/* flood fill with double hatching */
10 reyssat 1017
void obj_diafill(objparm *pm)
1018
{
12473 bpr 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]);
17940 bpr 1024
  if (tikz_file) tikz_fill(pm->p[0],pm->p[1],nx,ny,0,pm->color[0],diamond);
12473 bpr 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);
10 reyssat 1030
}
1031
 
17599 bpr 1032
/* flood fill with dots */
10 reyssat 1033
void obj_dotfill(objparm *pm)
1034
{
12473 bpr 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]);
17940 bpr 1040
  if (tikz_file) tikz_fill(pm->p[0],pm->p[1],nx,ny,0,pm->color[0],dot);
12473 bpr 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);
10 reyssat 1046
}
1047
 
1048
struct {
12473 bpr 1049
  char *name;
1050
  gdFontPtr *fpt;
17964 bpr 1051
  char *ftikz;
10 reyssat 1052
} fonttab[]={
17964 bpr 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"}
10 reyssat 1059
};
1060
 
1061
#define fonttab_no (sizeof(fonttab)/sizeof(fonttab[0]))
1062
 
7675 bpr 1063
/* string */
10 reyssat 1064
void obj_string(objparm *pm)
1065
{
12473 bpr 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
  }
17585 bpr 1083
  if(pm->fill){
12473 bpr 1084
    gdImageStringUp(image,*(fonttab[i].fpt),pm->p[0],pm->p[1], (unsigned char*) pe,
1085
        pm->color[0]);
17964 bpr 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);
17585 bpr 1088
  }
1089
  else {
12473 bpr 1090
    gdImageString(image,*(fonttab[i].fpt),pm->p[0],pm->p[1], (unsigned char*) pe,
1091
        pm->color[0]);
17964 bpr 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);
17585 bpr 1094
  }
10 reyssat 1095
}
17585 bpr 1096
/*FIXME; giant does not exist in tikz and ... samething as huge */
7675 bpr 1097
/* point */
10 reyssat 1098
void obj_point(objparm *pm)
1099
{
12473 bpr 1100
  scale(pm->pd,pm->p,1);
1101
  gdImageSetPixel(image,pm->p[0],pm->p[1],pm->color[0]);
17917 bpr 1102
  if(tikz_file) fprintf(tikz_file,"\\draw[%s] (%i,%i) circle (1pt);\n",
17574 bpr 1103
    tikz_options(pm->color[0],0),pm->p[0],flip(pm->p[1]));
10 reyssat 1104
}
1105
 
7675 bpr 1106
/* copy an image file */
10 reyssat 1107
void obj_copy(objparm *pm)
1108
{
12473 bpr 1109
  char *pp;
1110
  FILE *inf;
1111
  gdImagePtr insimg;
7615 bpr 1112
 
12473 bpr 1113
  pp=find_word_start(pm->str);*find_word_end(pp)=0;
1114
  inf=open4read(pp);
1115
  if(inf==NULL) {
17599 bpr 1116
    fly_error("file_does_not_exist"); return;
12473 bpr 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]);
17587 bpr 1129
  if(tikz_file) fprintf(tikz_file,
17618 bpr 1130
    "\\node[anchor=north west,inner sep=0pt] at (%i,%i) {\\includegraphics\[width=.05\\linewidth]{%s}};\n",
17587 bpr 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 */
10 reyssat 1134
}
1135
 
7675 bpr 1136
/* copy an image file, with resizing */
10 reyssat 1137
void obj_copyresize(objparm *pm)
1138
{
12473 bpr 1139
  char *pp;
1140
  FILE *inf;
1141
  gdImagePtr insimg;
7615 bpr 1142
 
12473 bpr 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);
10 reyssat 1162
}
1163
 
7675 bpr 1164
/* set brush or tile */
10 reyssat 1165
void obj_setbrush(objparm *pm)
1166
{
12473 bpr 1167
  char *pp;
1168
  FILE *inf;
1169
  gdImagePtr insimg;
10 reyssat 1170
 
12473 bpr 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
  }
10 reyssat 1185
}
1186
 
7675 bpr 1187
/* kill brush */
10 reyssat 1188
void obj_killbrush(objparm *pm)
1189
{
12473 bpr 1190
  if(brushimg) gdImageDestroy(brushimg);
1191
  brushed=0; brushimg=NULL;
10 reyssat 1192
}
1193
 
7675 bpr 1194
/* kill tile */
10 reyssat 1195
void obj_killtile(objparm *pm)
1196
{
12473 bpr 1197
  if(tileimg) gdImageDestroy(tileimg);
1198
  tiled=0; tileimg=NULL;
10 reyssat 1199
}
1200
 
7675 bpr 1201
/* set style */
10 reyssat 1202
void obj_setstyle(objparm *pm)
1203
{
12473 bpr 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;
10 reyssat 1215
}
1216
 
7675 bpr 1217
/* kill style */
10 reyssat 1218
void obj_killstyle(objparm *pm)
1219
{
12473 bpr 1220
  styled=0;
10 reyssat 1221
}
1222
 
7675 bpr 1223
/* set transparent */
10 reyssat 1224
void obj_transp(objparm *pm)
1225
{
12473 bpr 1226
  gdImageColorTransparent(image,pm->color[0]);
10 reyssat 1227
}
1228
 
7675 bpr 1229
/* set interlace */
10 reyssat 1230
void obj_interlace(objparm *pm)
1231
{
12473 bpr 1232
  gdImageInterlace(image,1);
10 reyssat 1233
}
1234
 
7675 bpr 1235
/* set linewidth */
10 reyssat 1236
void obj_linewidth(objparm *pm)
1237
{
12473 bpr 1238
  if(pm->pd[0]<1 || pm->pd[0]>255) fly_error("bad_parms");
17574 bpr 1239
  width=pm->pd[0];
17612 bpr 1240
  if(tikz_file) fprintf(tikz_file,"\\pgfsetlinewidth{%fpt}\n",width*0.7);
10 reyssat 1241
}
1242
 
8414 bpr 1243
/* set crosshairsize */
1244
void obj_crosshairsize(objparm *pm)
1245
{
12473 bpr 1246
  if(pm->pd[0]<1 || pm->pd[0]>255) fly_error("bad_parms");
1247
  else width2=pm->pd[0];
8414 bpr 1248
}
1249
 
7675 bpr 1250
/* set x range */
10 reyssat 1251
void obj_xrange(objparm *pm)
1252
{
12473 bpr 1253
  double dd;
1254
  dd=pm->pd[1]-pm->pd[0];
1255
  xstart=pm->pd[0]; xscale=sizex/dd;
10 reyssat 1256
}
1257
 
7675 bpr 1258
/* set y range */
10 reyssat 1259
void obj_yrange(objparm *pm)
1260
{
12473 bpr 1261
  double dd;
1262
  dd=pm->pd[1]-pm->pd[0];
1263
  ystart=pm->pd[1]; yscale=-sizey/dd;
10 reyssat 1264
}
1265
 
7675 bpr 1266
/* set x et y range */
10 reyssat 1267
void obj_range(objparm *pm)
1268
{
12473 bpr 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;
10 reyssat 1273
}
1274
 
7675 bpr 1275
/* set t range */
10 reyssat 1276
void obj_trange(objparm *pm)
1277
{
12473 bpr 1278
  /*double dd;
1279
  dd=pm->pd[1]-pm->pd[0];*/
1280
  tstart=pm->pd[0]; tend=pm->pd[1];
1281
  tranged=1;
10 reyssat 1282
}
1283
 
7675 bpr 1284
/* set tstep (plotting step) */
10 reyssat 1285
void obj_tstep(objparm *pm)
1286
{
12473 bpr 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;
10 reyssat 1294
}
1295
 
7675 bpr 1296
/* set plotjump (plot jump break threashold) */
10 reyssat 1297
void obj_plotjump(objparm *pm)
1298
{
12473 bpr 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;
10 reyssat 1304
}
1305
 
7675 bpr 1306
/* plot a curve, either parametric or explicit */
6578 bpr 1307
void _obj_plot(objparm *pm,int dash)
10 reyssat 1308
{
12473 bpr 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];
10 reyssat 1314
 
12473 bpr 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);
7675 bpr 1329
      pp+=strlen(EV_T);
12473 bpr 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();
17933 bpr 1335
  if (tikz_file) fprintf(tikz_file,"\%\%begin plot\n\\draw\[%s]",
1336
    tikz_options(pm->color[0],-dash));
12473 bpr 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);
10 reyssat 1341
    }
17621 bpr 1342
    else {
1343
      t=tstart+i*v; eval_setval(varpos,t);
1344
      dc[0]=strevalue(p1); dc[1]=strevalue(p2);
10 reyssat 1345
    }
17621 bpr 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]);
18019 bpr 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)
18105 georgesk 1367
            myDashedLine(image,oc[0],oc[1],ic[0],ic[1],pm->color[0]);
18019 bpr 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)",
17932 bpr 1372
                oc[0],flip(oc[1]),ic[0],flip(ic[1]));
18019 bpr 1373
        }
17621 bpr 1374
      }
1375
    }
18019 bpr 1376
    memmove(oc,ic,sizeof(oc));
12473 bpr 1377
  }
17932 bpr 1378
  if (tikz_file) fprintf(tikz_file,";\n\%\%end plot\n");
12473 bpr 1379
  if(vimg_enable) vimg_plotend();
10 reyssat 1380
}
1381
 
6578 bpr 1382
void obj_plot(objparm *pm) { _obj_plot( pm,0) ;}
18352 bpr 1383
void obj_dplot(objparm *pm) { _obj_plot( pm,1) ;}
6578 bpr 1384
 
7675 bpr 1385
/* set levelcurve granularity */
10 reyssat 1386
void obj_levelstep(objparm *pm)
1387
{
12473 bpr 1388
  int dd;
1389
  dd=pm->pd[0];
1390
  if(dd<1) return;
1391
  if(dd>16) dd=16;
1392
  lstep=dd;
10 reyssat 1393
}
1394
 
7675 bpr 1395
/* level curve */
10 reyssat 1396
void obj_levelcurve(objparm *pm)
1397
{
12473 bpr 1398
  char fn[MAX_LINELEN+1],tc[MAX_LINELEN+1];
1399
  int n,i;
1400
  double d;
1401
  leveldata *ld;
7615 bpr 1402
 
12473 bpr 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;
10 reyssat 1418
    }
12473 bpr 1419
    ld->levelcnt=n-1;
1420
  }
1421
  levelcurve(ld);
17932 bpr 1422
  if (tikz_file)
1423
    fprintf(tikz_file,"\%\%begin levelcurve\n\\draw\[%s]"
18065 bpr 1424
      ,tikz_options(pm->color[0],1));
12473 bpr 1425
  for(i=0;i<ld->datacnt;i++) {
1426
    gdImageSetPixel(image,ld->xdata[i],ld->ydata[i],pm->color[0]);
17584 bpr 1427
    if (tikz_file)
1428
      fprintf(tikz_file,
17933 bpr 1429
        "(%i,%i)rectangle(%i,%i)",
1430
        ld->xdata[i],flip(ld->ydata[i]),ld->xdata[i]+1,flip(ld->ydata[i]+1));
12473 bpr 1431
  }
17932 bpr 1432
  if (tikz_file) fprintf(tikz_file,";\n\%\%end levelcurve\n");
12473 bpr 1433
  free(ld);
10 reyssat 1434
}
1435
 
7675 bpr 1436
/* set animation step */
10 reyssat 1437
void obj_animstep(objparm *pm)
1438
{
12473 bpr 1439
  animstep=pm->pd[0];
1440
  setvar("animstep",pm->pd[0]);
10 reyssat 1441
}
1442
 
7675 bpr 1443
/* Starts a line counting */
10 reyssat 1444
void obj_linecount(objparm *pm)
1445
{
12473 bpr 1446
  linecnt=pm->pd[0];
10 reyssat 1447
}
1448
 
7675 bpr 1449
/* marks end of execution */
10 reyssat 1450
void obj_end(objparm *pm)
1451
{
12473 bpr 1452
  fly_error("successful_end_of_execution");
10 reyssat 1453
}
1454
 
7675 bpr 1455
/* output */
10 reyssat 1456
void obj_output(objparm *pm)
1457
{
12473 bpr 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);
10 reyssat 1464
}
1465
 
7675 bpr 1466
/* vimgfile */
1024 bpr 1467
void obj_vimgfile(objparm *pm)
1468
{
12473 bpr 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();
1024 bpr 1473
}
1474
 
7675 bpr 1475
/* vimg enable/disable */
1024 bpr 1476
void obj_vimg(objparm *pm)
1477
{
12473 bpr 1478
  vimg_enable=pm->pd[0];
1479
  if(vimg_enable>0 && vimg_ready==0) vimg_init();
1024 bpr 1480
}
1481
 
7675 bpr 1482
/* Set affine transformation */
10 reyssat 1483
void obj_affine(objparm *pm)
1484
{
12473 bpr 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;
10 reyssat 1489
}
1490
 
7675 bpr 1491
/* Set affine transformation */
10 reyssat 1492
void obj_rotation(objparm *pm)
1493
{
12473 bpr 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;
10 reyssat 1499
}
1500
 
8419 bpr 1501
/* Set linear transformation */
10 reyssat 1502
void obj_linear(objparm *pm)
1503
{
12473 bpr 1504
  int i;
1505
  for(i=0;i<4;i++) matrix[i]=pm->pd[i];
1506
  transform=1;
10 reyssat 1507
}
1508
 
8419 bpr 1509
/* Set translation transformation */
10 reyssat 1510
void obj_translation(objparm *pm)
1511
{
12473 bpr 1512
  transx=pm->pd[0]; transy=pm->pd[1];
10 reyssat 1513
}
1514
 
8419 bpr 1515
/* kill affine transformation */
10 reyssat 1516
void obj_killaffine(objparm *pm)
1517
{
12473 bpr 1518
  matrix[0]=matrix[3]=1;
1519
  matrix[1]=matrix[2]=transx=transy=0;
1520
  transform=0;
10 reyssat 1521
}
1522
 
8419 bpr 1523
/* kill affine transformation */
10 reyssat 1524
void obj_killlinear(objparm *pm)
1525
{
12473 bpr 1526
  matrix[0]=matrix[3]=1;
1527
  matrix[1]=matrix[2]=0;
1528
  transform=0;
10 reyssat 1529
}
1530
 
8419 bpr 1531
/* kill affine transformation */
10 reyssat 1532
void obj_killtranslation(objparm *pm)
1533
{
12473 bpr 1534
  transx=transy=0;
10 reyssat 1535
}
1536
 
1537
/***** Les modifs de Jean-Christophe Leger Fev 2006 *****/
1538
 
7615 bpr 1539
/* arguments: un numero de matrice entre 1 et JC_NB_MATRICES, une liste de 4 nombres reels pour la matrice */
10 reyssat 1540
void obj_setmatrix(objparm *pm)
1541
{
1542
  int nummatrix = (int) (pm->pd[0]);
1543
  if((nummatrix < 1) ||(nummatrix>JC_NB_MATRICES)) {
8195 bpr 1544
    fly_error("bad_matrix_number");
10 reyssat 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
 
7615 bpr 1553
/* arguments: un numero de matrice entre 1 et JC_NB_MATRICES */
10 reyssat 1554
void obj_resetmatrix(objparm *pm)
1555
{
1556
  int nummatrix = (int) (pm->pd[0]);
1557
  if((nummatrix < 1) ||(nummatrix>JC_NB_MATRICES)) {
8195 bpr 1558
    fly_error("bad_matrix_number");
10 reyssat 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
}
7615 bpr 1567
/* arguments: un numero de vecteur entre 1 et JC_NB_MATRICES, une liste de 2 nombres reels pour le vecteur */
10 reyssat 1568
void obj_setvector(objparm *pm)
1569
{
1570
  int nummatrix = (int) (pm->pd[0]);
1571
  if((nummatrix < 1) ||(nummatrix>JC_NB_MATRICES)) {
8195 bpr 1572
    fly_error("bad_vector_number");
10 reyssat 1573
    return;
1574
  }
1575
  vecteurs_pavage[nummatrix][0] = pm->pd[1];
1576
  vecteurs_pavage[nummatrix][1] = pm->pd[2];
1577
}
1578
 
7615 bpr 1579
/* arguments: un numero de matrice entre 1 et JC_NB_MATRICES */
10 reyssat 1580
void obj_resetvector(objparm *pm)
1581
{
1582
  int nummatrix = (int) (pm->pd[0]);
1583
  if((nummatrix < 1) ||(nummatrix>JC_NB_MATRICES)) {
8195 bpr 1584
    fly_error("bad_vector_number");
10 reyssat 1585
    return;
1586
  }
1587
  vecteurs_pavage[nummatrix][0] = 0;
1588
  vecteurs_pavage[nummatrix][1] = 0;
1589
}
1590
 
7615 bpr 1591
/* arguments: un numero de vecteur entre 1 et JC_NB_MATRICES, une liste de 6 nombres reels pour la matrice et le vecteur */
10 reyssat 1592
void obj_settransform(objparm *pm)
1593
{
1594
  int nummatrix = (int) (pm->pd[0]);
1595
  if((nummatrix < 1) ||(nummatrix>JC_NB_MATRICES)) {
8195 bpr 1596
    fly_error("bad_vector_number");
10 reyssat 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
 
7615 bpr 1608
/* arguments: un numero de matrice entre 1 et JC_NB_MATRICES */
10 reyssat 1609
void obj_resettransform(objparm *pm)
1610
{
1611
  int nummatrix = (int) (pm->pd[0]);
1612
  if((nummatrix < 1) ||(nummatrix>JC_NB_MATRICES)) {
8195 bpr 1613
    fly_error("bad_vector_number");
10 reyssat 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
 
7615 bpr 1624
/* arguments: une liste de 6 nombres reels pour les coordonnees de l'origine et des vecteurs de base */
10 reyssat 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
{
12473 bpr 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 ? */
7615 bpr 1660
 
12473 bpr 1661
  for (i=0; (i<gdMaxColors); i++) {
1662
    colorMap[i] = (-1);
1663
  }
10 reyssat 1664
 
12473 bpr 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
10 reyssat 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
     */
12473 bpr 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);
10 reyssat 1679
 
1680
 
12473 bpr 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
  }
10 reyssat 1689
 
12473 bpr 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++;
10 reyssat 1697
    }
12473 bpr 1698
  }
1699
  if(t<=0){
1700
    for(i=0;i<JC_NB_MATRICES;i++) {
1701
      nummatrices[i] = i+1;
10 reyssat 1702
    }
12473 bpr 1703
    comptmat=JC_NB_MATRICES;
1704
  }
10 reyssat 1705
 
7615 bpr 1706
 
12473 bpr 1707
  imax = gdImageSX(insimg);
1708
  jmax = gdImageSY(insimg);
10 reyssat 1709
 
12473 bpr 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],
7675 bpr 1731
                             insimg->blue[c]);
12473 bpr 1732
             /* If we're out of colors, go for the closest color */
1733
            if (nc == (-1)) {
1734
              nc = gdImageColorClosest(image,
7675 bpr 1735
                              insimg->red[c], insimg->green[c],
1736
                              insimg->blue[c]);
12473 bpr 1737
            }
1738
          }
1739
          colorMap[c] = nc;
1740
        }
10 reyssat 1741
 
12473 bpr 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++){
7675 bpr 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]);
12473 bpr 1751
        }
10 reyssat 1752
      }
1753
    }
12473 bpr 1754
  }
1755
  gdImageDestroy(insimg);
10 reyssat 1756
}
1757
 
1758
/**** Fin modifs JC Fev 06 ******/
1759
 
7675 bpr 1760
/* get color from value */
10 reyssat 1761
int calc_color(char *p, struct objtab *o)
1762
{
12473 bpr 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) {
10 reyssat 1771
 
12473 bpr 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]);
10 reyssat 1784
}
1785
 
7675 bpr 1786
/* Routine to parse parameters */
10 reyssat 1787
int parse_parms(char *p,objparm *pm,struct objtab *o)
1788
{
12473 bpr 1789
  char buf[MAX_LINELEN+1];
1790
  char *p1, *p2;
1791
  int j,t,c,c1,c2;
7615 bpr 1792
 
12473 bpr 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;}
10 reyssat 1813
    }
12473 bpr 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;
18131 bpr 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;
18159 bpr 1829
       if(noreset==0) dashed=0;
18131 bpr 1830
    }
1831
    if(filled) {
1832
      if(pm->fill==-1) pm->fill=2;
1833
      if(pm->fill==0) pm->fill=1;
18159 bpr 1834
      if(noreset==0) filled=0;
18131 bpr 1835
    }
1836
  }
12473 bpr 1837
  ovlstrcpy(pm->str,p); return 0;
10 reyssat 1838
}
1839
 
8068 bpr 1840
/* Create the struct objparm pm corresponding to the objparm p
1841
 * Execute a command. Returns 0 if OK.
1842
 */
10 reyssat 1843
int obj_main(char *p)
1844
{
12473 bpr 1845
  int i;
1846
  char *pp,*name,*pt;
1847
  char tbuf2[MAX_LINELEN+1];
1848
  struct objparm pm;
10 reyssat 1849
 
12473 bpr 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;
10 reyssat 1878
}
18352 bpr 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];
18447 bpr 1888
  if (dy*dy*1e4 <= nx*nx+ny*ny){
18352 bpr 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]);
18353 bpr 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
   }
18352 bpr 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];
18358 bpr 1940
  if (tikz_file) fprintf(tikz_file, "\\draw[%s]",tikz_options(pm->color[0],0));
18352 bpr 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
 
18432 bpr 1970
/* polygon */
1971
void obj_hyppoly(objparm *pm)
1972
{
1973
  double rx,ry,data[1000],res[7];
1974
  int i,*qq=pm->p;
1975
  int cnt=(pm->pcnt)/2;
1976
  scale(pm->pd,pm->p,cnt);
1977
  for(i=0; i<pm->pcnt+2; i++) data[i]=pm->pd[i%pm->pcnt];
1978
  if (tikz_file) fprintf(tikz_file, "\\draw[%s]",tikz_options(pm->color[0],0));
1979
  for(i=0; i<cnt; i++){
1980
    if(hypgeodaux(data+2*i,res)){
1981
      scale2(res[2],res[2],&rx,&ry);
1982
      scale(res,qq,1);
1983
      myGdImageArc(image,qq[0],qq[1],rx,ry,res[3]/DEG,res[4]/DEG,pm->color[0]);
1984
      if(tikz_file)
1985
        fprintf(tikz_file, "(%i,%i) arc (%f:%f:%i and %i)",
1986
          (int)rint(qq[0]+rx*cos(res[3])),
1987
          flip((int)rint(qq[1]+ry*sin(res[3]))),
1988
          res[3]/DEG,res[4]/DEG,(int)rint(rx),-(int)rint(ry));
1989
    }
1990
    else {
1991
      scale(data+2*i,qq,2);
1992
      gdImageLine(image,qq[0],qq[1],qq[2],qq[3],pm->color[0]);
1993
      if(tikz_file) fprintf(tikz_file,"(%i,%i)--(%i,%i)",
1994
            qq[0],flip(qq[1]),qq[2],flip(qq[3]));
1995
    }
1996
  }
1997
  if(tikz_file) fprintf(tikz_file,";\n");
1998
  if(pm->fill){
1999
    data[0]=res[5];
2000
    data[1]=res[6];
2001
    hypgeodaux(data,res);
2002
    scale(res+5,qq,1);
2003
    //patchgdImageFill(image,qq[0],qq[1],pm->color[0]);
2004
    patchgdImageFillToBorder(image,qq[0],qq[1],pm->color[0],pm->color[0]);
2005
    if(tikz_file) tikz_fill(qq[0],qq[1],1,1,0,pm->color[0],grid);
2006
  }
2007
}
18352 bpr 2008
/* complete hyperbolic geodesic through the two first points, then the following two points, etc */
2009
 
2010
void obj_hyplines(objparm *pm)
2011
{
2012
  double rx,ry,res[7],*pd = pm->pd;
2013
  int i;
2014
  if (tikz_file) fprintf(tikz_file, "\\draw[%s]",tikz_options(pm->color[0],0));
2015
  for (i=0; i < pm->pcnt; i+=4,pd+=4)
2016
    if (hypgeodaux(pd,res)) {
2017
      double alpha=atan(1/res[2]), beta = atan2(res[1],res[0]);
2018
      scale(res,pm->p,1);
2019
      scale2(res[2],res[2],&rx,&ry);
2020
      myGdImageArc(image,pm->p[0],pm->p[1],rx,ry,180+(beta-alpha)/DEG,180+(beta+alpha)/DEG,pm->color[0]);
2021
      if (tikz_file) fprintf(tikz_file, "(%i,%i) arc (%f:%f:%i and %i);\n",
2022
        (int)rint(pm->p[0]-rx*cos(beta-alpha)), flip((int)rint(pm->p[1]-ry*sin(beta-alpha))),
18356 bpr 2023
          180+(beta-alpha)/DEG,180+(beta+alpha)/DEG, (int)rint(rx),-(int)rint(ry));
18352 bpr 2024
    }
2025
    else {
2026
      double gamma;
2027
      if (pd[1]*pd[1]+pd[0]*pd[0] > pd[2]*pd[2]+pd[3]*pd[3])
2028
        gamma = atan2(pd[1],pd[0]);
2029
      else
2030
        gamma = atan2(pd[3],pd[2]);
2031
        res[0]=cos(gamma);
2032
        res[1]=sin(gamma);
2033
        res[2]=-cos(gamma);
2034
        res[3]=-sin(gamma);
2035
        scale(res,pm->p,2);
2036
        gdImageLine(image,pm->p[0],pm->p[1],pm->p[2],pm->p[3],pm->color[0]);
2037
        if(tikz_file) fprintf(tikz_file, "(%i,%i)--(%i,%i)",
2038
          pm->p[0],flip(pm->p[1]),pm->p[2],flip(pm->p[3]));
2039
    }
2040
    if (tikz_file) fprintf(tikz_file, ";\n");
2041
}