~ubuntu-branches/ubuntu/hardy/libterralib/hardy

« back to all changes in this revision

Viewing changes to src/terralib/kernel/TeGraph.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Daniel T Chen
  • Date: 2005-11-25 22:32:59 UTC
  • Revision ID: james.westby@ubuntu.com-20051125223259-3zubal8ux4ki4fjg
Tags: upstream-3.0.3b2
ImportĀ upstreamĀ versionĀ 3.0.3b2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/************************************************************************************
 
2
TerraLib - a library for developing GIS applications.
 
3
Copyright ļæ½ 2001-2004 INPE and Tecgraf/PUC-Rio.
 
4
 
 
5
This code is part of the TerraLib library.
 
6
This library is free software; you can redistribute it and/or
 
7
modify it under the terms of the GNU Lesser General Public
 
8
License as published by the Free Software Foundation; either
 
9
version 2.1 of the License, or (at your option) any later version.
 
10
 
 
11
You should have received a copy of the GNU Lesser General Public
 
12
License along with this library.
 
13
 
 
14
The authors reassure the license terms regarding the warranties.
 
15
They specifically disclaim any warranties, including, but not limited to,
 
16
the implied warranties of merchantability and fitness for a particular purpose.
 
17
The library provided hereunder is on an "as is" basis, and the authors have no
 
18
obligation to provide maintenance, support, updates, enhancements, or modifications.
 
19
In no event shall INPE and Tecgraf / PUC-Rio be held liable to any party for direct,
 
20
indirect, special, incidental, or consequential damages arising out of the use
 
21
of this library and its documentation.
 
22
*************************************************************************************/
 
23
 
 
24
#ifdef WIN32
 
25
#pragma warning ( disable: 4786 )
 
26
#endif
 
27
 
 
28
#include <TeGraph.h>
 
29
#include <TeDefines.h>
 
30
#include <math.h>
 
31
#include <float.h>
 
32
#include <malloc.h>
 
33
#include <stdio.h>
 
34
#include <errno.h>
 
35
 
 
36
int set_font(int font_number);
 
37
 
 
38
#define PEN_UP       0     /* pen status up */
 
39
#define PEN_DOWN     1     /* pen status down */
 
40
 
 
41
#define ERR        -1     /* return value when error occurs */
 
42
 
 
43
#define HORIZ_FONT 0
 
44
#define VERT_FONT  1
 
45
#define FONT_MAX   2
 
46
 
 
47
/*** symbol shapes ***/
 
48
 
 
49
#define RECTANGLE    0     /* tells PlotSymbol to draw a rectangle */
 
50
#define TRIANGLE     1     /* tells PlotSymbol to draw a triangle */
 
51
#define DIAMOND      2     /* tells PlotSymbol to draw a diamond */
 
52
#define SCROSS       3     /* tells PlotSymbol to draw an upright cross */
 
53
#define XCROSS       4     /* tells PlotSymbol to draw an angled cross */
 
54
 
 
55
/*** constants ***/
 
56
 
 
57
#define CURVES_LIMIT   10     /* maximum number of curves */
 
58
#define MAIN_ROW       3     /* row number for main menu */
 
59
#define MAIN_COL       2     /* column number for main menu */
 
60
#define MAX_ITEMS     13     /* maximum number of items in a menu window */
 
61
#define MASK_SIZE     50     /* length of file mask string */
 
62
#define ASCII          0     /* Ascii output format */
 
63
#define BINARY         1     /* Binary output format */
 
64
#define LOAD           0     /* load options */
 
65
#define SAVE           1     /* save options */
 
66
#define SET            2     /* set options */
 
67
#define DIGITS         6     /* number of digits to display for regression
 
68
                               coefficients and mean square error */
 
69
int  Font_width[FONT_MAX]  = {9,9};
 
70
int  Font_height[FONT_MAX] = {15,15};
 
71
int  Font_direct[FONT_MAX] = {HORIZ_FONT,VERT_FONT};
 
72
 
 
73
 
 
74
char Symbol[CURVES_MAX] = {        /* symbols identifying each curve */
 
75
                RECTANGLE,  /* symbol for curve 1 is a rectangle */
 
76
                SCROSS,     /* symbol for curve 4 is an upright cross */
 
77
                TRIANGLE,   /* symbol for curve 2 is a triangle */
 
78
                DIAMOND,    /* symbol for curve 3 is a diamond */
 
79
                XCROSS,      /* symbol for curve 5 is an angled cross */
 
80
                RECTANGLE,  /* symbol for curve 6 is a rectangle */
 
81
                SCROSS,     /* symbol for curve 9 is an upright cross */
 
82
                TRIANGLE,   /* symbol for curve 7 is a triangle */
 
83
                DIAMOND,    /* symbol for curve 8 is a diamond */
 
84
                XCROSS      /* symbol for curve 10 is an angled cross */
 
85
};
 
86
 
 
87
unsigned Terms[] =
 
88
{                                  /* terms in each regression function */
 
89
        0,                         /* terms for polynomial set by regres_func */
 
90
        2,                         /* number of exponential terms */
 
91
        2,                         /* number of logarithmic terms */
 
92
        3                          /* number of sinusoidal terms */
 
93
};
 
94
 
 
95
 
 
96
struct {                        /* Options default settings */
 
97
        char symbols[5];
 
98
        char x_major[3];
 
99
        char x_minor[3];
 
100
        char y_major[3];
 
101
        char y_minor[3];
 
102
        char y_scale[2];
 
103
        char filemask[MASK_SIZE];
 
104
        char format[7];
 
105
        char digits[3];
 
106
        char linesize[4];
 
107
        char adapter[5];
 
108
        char printer[9];
 
109
        char printer_port[5];
 
110
        char print_file[13];
 
111
        char color[4];
 
112
        char snow[4];
 
113
        char sound[4];
 
114
        char grid[4];
 
115
        char legend[4];
 
116
} Options ;       /* curve legend turned on                        */
 
117
 
 
118
struct {
 
119
        unsigned char background;   /* color of outermost window */
 
120
        unsigned char pattern;      /* color of pattern in outermost window */
 
121
        unsigned char input;        /* color of input window */
 
122
        unsigned char input_prompt; /* color of input window prompt */
 
123
        unsigned char input_answer; /* color of input window answer */  
 
124
        unsigned char edit;         /* color of edit window */
 
125
        unsigned char edit_bar;     /* color of selection bar in edit window */
 
126
        unsigned char edit_answer;  /* color of answer bar in edit window */
 
127
        unsigned char edit_index;   /* color of index values in edit window */
 
128
        unsigned char edit_heading; /* color of X/Y values heading in edit window */
 
129
        unsigned char error;        /* color of error window */
 
130
        unsigned char help;         /* color of help message text */
 
131
} Text_color;
 
132
 
 
133
struct Grc{
 
134
    TeColor grid;                /* color of plot grid         */
 
135
    TeColor text;                /* color of plot text         */
 
136
    TeColor lines;               /* color of plot all lines    */
 
137
        TeColor symbol[CURVES_LIMIT];/* color of plot all lines    */
 
138
    TeColor curve[CURVES_LIMIT]; /* color of plot curves       */
 
139
} Graph_color;
 
140
 
 
141
int f_spoly(Real x, Real_Vector terms, unsigned n);
 
142
int f_sexp(Real x, Real_Vector terms, unsigned n);
 
143
int f_slog(Real x, Real_Vector terms, unsigned n);
 
144
int f_ssin(Real x, Real_Vector terms, unsigned n);
 
145
 
 
146
int (*Function[])(Real, Real_Vector, unsigned) =
 
147
{                                  /* array of functions used for regression  */
 
148
        f_spoly,                     /* calculate terms of polynomial equation  */
 
149
        f_sexp,                      /* calculate terms of exponential equation */
 
150
        f_slog,                      /* calculate terms of logarithmic equation */
 
151
        f_ssin                       /* calculate terms of sinusoidal equation  */
 
152
};
 
153
 
 
154
Real vmaxval(Real_Vector v, unsigned int n, int *imx)
 
155
{
 
156
        // subroutine determines the maximum  value (maxx) of the
 
157
        // vector v[] of length n and returns the index (imx) where
 
158
        // the maximum occurs; i.e. imx, where maxx = v[imx].
 
159
        // On exit, the maximum is returned as the value of vmaxval().
 
160
 
 
161
        unsigned int i;
 
162
        Real maxx;
 
163
 
 
164
        if (v == NULL)
 
165
                return (Real) 0.0;
 
166
 
 
167
        //find max value
 
168
 
 
169
        *imx=0;
 
170
        maxx=v[0];
 
171
        for(i=1; i<n ; i++)
 
172
        {
 
173
                if(v[i] > maxx)
 
174
                {
 
175
                        maxx=v[i];
 
176
                        *imx=i;
 
177
                }
 
178
        }
 
179
        return maxx;
 
180
}
 
181
 
 
182
//---------------------------------------------------------------------------------
 
183
Real
 
184
vminval(Real_Vector v, unsigned int n, int *imn)
 
185
{
 
186
// subroutine determines the minimum  value (minx) of the
 
187
// vector v[] of length n and returns the index (imn) where
 
188
// the minimum occurs; i.e. imn, where minx = v[imn].
 
189
// On exit, the minimum is returned as the value of vminval().
 
190
 
 
191
        unsigned int i;
 
192
        Real minx;
 
193
 
 
194
        if (v == NULL)
 
195
                return (Real) 0.0;
 
196
 
 
197
        //find min value
 
198
        *imn=0;
 
199
        minx=v[0];
 
200
        for(i=1; i<n ; i++)
 
201
        {
 
202
                if(v[i] < minx)
 
203
                {
 
204
                        minx=v[i];
 
205
                        *imn=i;
 
206
                }
 
207
        }
 
208
        return minx;
 
209
}
 
210
 
 
211
//---------------------------------------------------------------------------------
 
212
 
 
213
TeGraphPlot :: TeGraphPlot ()
 
214
{
 
215
        GPpointwidth = 7;
 
216
        GPpointheight = 7;
 
217
 
 
218
        label_field = 11;
 
219
        major_tick = 5;
 
220
        minor_tick = 2;
 
221
        x_major = 8;
 
222
        y_major = 10;
 
223
        x_minor = 8;
 
224
        y_minor = 5;
 
225
        Scale_curve = -1;
 
226
        Plot_curves = 0;
 
227
        Plot_symbols = 0;
 
228
        Curve_choice = 0;
 
229
        Plot_choice = 0;
 
230
        int i;
 
231
        for( i = 0; i < CURVES_MAX; i++ )
 
232
        {
 
233
                Plot_points[i] = 0;
 
234
                Plot_x[i] = 0;
 
235
                Plot_y[i] = 0;
 
236
        }
 
237
 
 
238
        Graph_color.grid.init(0,100,100);
 
239
        Graph_color.text.init(0,0,0);
 
240
        Graph_color.lines.init(30,30,30);// preto
 
241
 
 
242
        for( i = 0; i < CURVES_LIMIT; i++ )
 
243
        {
 
244
                Graph_color.symbol[i].init(0,255,0);
 
245
                Graph_color.curve[i].init(0,0,0);
 
246
        }
 
247
 
 
248
        sliceVector = 0;
 
249
}
 
250
 
 
251
//---------------------------------------------------------------------------------
 
252
 
 
253
TeGraphPlot :: ~TeGraphPlot ()
 
254
{
 
255
}
 
256
 
 
257
//---------------------------------------------------------------------------------
 
258
void
 
259
TeGraphPlot :: setColorGrid(int r, int g, int b)
 
260
{
 
261
        Graph_color.grid.init(r,g,b);
 
262
}
 
263
 
 
264
//---------------------------------------------------------------------------------
 
265
void    
 
266
TeGraphPlot :: setColorText(int r, int g, int b)
 
267
{
 
268
        Graph_color.text.init(r,g,b);
 
269
}
 
270
 
 
271
//---------------------------------------------------------------------------------
 
272
void    
 
273
TeGraphPlot :: setColorSymbol(int r, int g, int b, int curve, int w, int h)
 
274
{
 
275
        if (curve==-1)
 
276
                for (int i=0; i<CURVES_LIMIT; i++)
 
277
                        Graph_color.symbol[i].init(r,g,b);
 
278
        else
 
279
                Graph_color.symbol[curve].init(r,g,b);
 
280
 
 
281
        GPpointwidth = w;
 
282
        GPpointheight = h;
 
283
}
 
284
 
 
285
//---------------------------------------------------------------------------------
 
286
void
 
287
TeGraphPlot :: setColorLine(int r, int g, int b)
 
288
{
 
289
        Graph_color.lines.init(r,g,b);
 
290
}
 
291
 
 
292
//---------------------------------------------------------------------------------
 
293
int
 
294
TeGraphPlot :: setColorCurve(int c,int r, int g, int b)
 
295
{
 
296
        if( c < 0 || c > CURVES_LIMIT-1 )
 
297
        {
 
298
                return false;
 
299
        }
 
300
 
 
301
        Graph_color.curve[c].init(r,g,b);
 
302
 
 
303
        return true;
 
304
}
 
305
 
 
306
void
 
307
TeGraphPlot :: pieLegends ()
 
308
{
 
309
        if (sliceVector == 0)
 
310
                return;
 
311
        int     w;
 
312
        int     xt, yt, pw, ph;
 
313
        
 
314
        TeColor cor;
 
315
 
 
316
        cor.init (0, 0, 0);
 
317
    setTextColor(cor);
 
318
 
 
319
        w = min(width,height);
 
320
        pw = 2*label_height;
 
321
        ph = label_height;
 
322
 
 
323
        xt = xupleft + w + pw;
 
324
        yt = yupleft;
 
325
        int textext = 0;
 
326
        for (unsigned int i = 0; i < sliceVector->size (); i++)
 
327
        {
 
328
                TeLegendEntry slice = (*sliceVector)[i];
 
329
                TeVisual  vis;
 
330
                //vis = slice.visual (TePOLYGONS);
 
331
                TeColor cor = vis.color();
 
332
                
 
333
                setFillAreaColor (cor);
 
334
                string leg = slice.label ();
 
335
                int xw,xh;
 
336
                textExtent (leg, xw, xh);
 
337
                textext = max (textext, xw);
 
338
                fillRectangle (xt, yt-ph, pw, ph);
 
339
                drawText ((int)(xt+1.2*pw), yt-2, (char*)leg.c_str ());
 
340
                yt += (int)1.5*ph;
 
341
                if (yt > (yupleft + height))
 
342
                {
 
343
                        xt += (int)(textext+2.2*pw);
 
344
                        textext = 0;
 
345
                        yt = yupleft;
 
346
                }
 
347
        }
 
348
}
 
349
 
 
350
 
 
351
//---------------------------------------------------------------------------------
 
352
int
 
353
TeGraphPlot :: setGridSize(int s)
 
354
{
 
355
        if( s <= 0 )
 
356
        {
 
357
                return false;
 
358
        }
 
359
        x_major = s;
 
360
        y_major = s;
 
361
        x_minor = s;
 
362
        y_minor = s;
 
363
 
 
364
        return true;
 
365
}
 
366
 
 
367
//---------------------------------------------------------------------------------
 
368
int
 
369
TeGraphPlot :: setGridSize(int xma, int yma, int xmi, int ymi )
 
370
{
 
371
        if( xma <= 0 || yma <= 0 || xmi <= 0 || ymi <= 0 )
 
372
        {
 
373
                return false;
 
374
        }
 
375
 
 
376
        x_major = xma;
 
377
        y_major = yma;
 
378
        x_minor = xmi;
 
379
        y_minor = ymi;
 
380
 
 
381
        return true;
 
382
}
 
383
 
 
384
//---------------------------------------------------------------------------------
 
385
int
 
386
TeGraphPlot :: loadDirect (int format, int curv, int points, Labels& labels, Real_Vector x, Real_Vector y)
 
387
{
 
388
        if( curv < 0 || curv > CURVES_LIMIT-1 )
 
389
        {
 
390
                return false;
 
391
        }
 
392
 
 
393
        strcpy(Plot_label.title, labels.title);
 
394
        strcpy(Plot_label.xaxis, labels.xaxis);
 
395
        strcpy(Plot_label.yaxis, labels.yaxis);
 
396
        Input_format[curv] = format;
 
397
 
 
398
        strcpy(Plot_label.curve[curv], labels.curve[curv]);
 
399
 
 
400
        Plot_x[curv] = x;
 
401
        Plot_y[curv] = y;
 
402
        Plot_points[curv] = points;
 
403
 
 
404
        if (Plot_curves < (curv+1) )
 
405
        {
 
406
                Plot_curves= curv+1;
 
407
        }
 
408
 
 
409
        return true;
 
410
}
 
411
 
 
412
void TeGraphPlot::loadHistogram (TeLegendEntryVector& sv, const string& label)
 
413
{
 
414
// Manage number of curves, only one histogram is allowed per plotting
 
415
        Plot_curves = 1;
 
416
        int order = 0;
 
417
        Input_format[order] = BAR_BINARY;
 
418
        sliceVector = &sv;
 
419
// Allocate space for data arrays
 
420
        unsigned int size = sv.size ();
 
421
        if (Plot_x[order])
 
422
                delete [] Plot_x[order];
 
423
        if (Plot_y[order])
 
424
                delete [] Plot_y[order];
 
425
        Plot_x[order] = new double [size];
 
426
        Plot_y[order] = new double [size];
 
427
        Plot_points[order] = size;
 
428
 
 
429
// Transfer data from TeLegendEntrys to double array
 
430
        int vmax = 0;
 
431
        for (unsigned int i=0;i<size;i++)
 
432
        {
 
433
                TeLegendEntry sl = sv[i];
 
434
                Plot_y[order][i] = sl.count();
 
435
                Plot_x[order][i] = (double)i+1;
 
436
                vmax = max (vmax,sl.count() );
 
437
        }
 
438
 
 
439
// Fill in labels
 
440
        strcpy(Plot_label.title, label.c_str ());
 
441
        strcpy(Plot_label.xaxis, "Fatia");
 
442
        strcpy(Plot_label.yaxis, "Objetos");
 
443
        strcpy(Plot_label.curve[order],"");
 
444
        setScale(1., Real(size+1) , 0., Real (vmax));
 
445
        setGridSize(size, 8, 1, 1); 
 
446
}
 
447
 
 
448
void TeGraphPlot::loadPie (TeLegendEntryVector& sv, const string& label)
 
449
{
 
450
// Manage number of curves, only one pie is allowed per plotting
 
451
        Plot_curves = 1;
 
452
        int order = 0;
 
453
        Input_format[order] = PIE_BINARY;
 
454
        height = Y_pixels - yupleft;
 
455
        sliceVector = &sv;
 
456
// Allocate space for data arrays
 
457
        unsigned int size = sv.size ();
 
458
        if (Plot_x[order])
 
459
                delete [] Plot_x[order];
 
460
        if (Plot_y[order])
 
461
                delete [] Plot_y[order];
 
462
        Plot_x[order] = new double [size];
 
463
        Plot_y[order] = new double [size];
 
464
        Plot_points[order] = size;
 
465
 
 
466
// Transfer data from TeLegendEntrys to double array
 
467
        int vmax = 0;
 
468
        for (unsigned int i=0;i<size;i++)
 
469
        {
 
470
                TeLegendEntry sl = sv[i];
 
471
                Plot_y[order][i] = sl.count();
 
472
                Plot_x[order][i] = (double)i+1;
 
473
                vmax = max (vmax,sl.count() );
 
474
        }
 
475
 
 
476
// Fill in labels
 
477
        strcpy(Plot_label.title, label.c_str ());
 
478
//      strcpy(Label.curve[order],label.c_str ());
 
479
}
 
480
 
 
481
//---------------------------------------------------------------------------------
 
482
void
 
483
TeGraphPlot :: autoScale(void)
 
484
{
 
485
// scale axes for Scale_curve (for all curves if Scale_curve == -1) 
 
486
 
 
487
        int i, index;
 
488
        Real value;
 
489
 
 
490
        if (Scale_curve == -1)
 
491
                index = 0;
 
492
        else
 
493
                index = Scale_curve;
 
494
 
 
495
        xminval = vminval(Plot_x[index], Plot_points[index], &i);
 
496
        xmaxval = vmaxval(Plot_x[index], Plot_points[index], &i);
 
497
        yminval = vminval(Plot_y[index], Plot_points[index], &i);
 
498
        ymaxval = vmaxval(Plot_y[index], Plot_points[index], &i);
 
499
 
 
500
        if (Scale_curve == -1)
 
501
        {
 
502
                for (i = 1; i < Plot_curves; i++)
 
503
                {
 
504
                        value = vminval(Plot_x[i], Plot_points[i], &index);
 
505
                        if (value < xminval) xminval = value;
 
506
                        value = vmaxval(Plot_x[i], Plot_points[i], &index);
 
507
                        if (value > xmaxval) xmaxval = value;
 
508
                        value = vminval(Plot_y[i], Plot_points[i], &index);
 
509
                        if (value < yminval) yminval = value;
 
510
                        value = vmaxval(Plot_y[i], Plot_points[i], &index);
 
511
                        if (value > ymaxval) ymaxval = value;
 
512
                }
 
513
        }
 
514
 
 
515
        for (int curva=0; curva < Plot_curves; curva++)
 
516
        {
 
517
                if (Input_format[curva] == ROSE_BINARY)
 
518
                {
 
519
                        yminval = - ymaxval;
 
520
                        xmaxval = ymaxval;
 
521
                        xminval = yminval;
 
522
                }
 
523
        }
 
524
}
 
525
 
 
526
//---------------------------------------------------------------------------------
 
527
void
 
528
TeGraphPlot :: setScale(Real x1, Real x2 , Real y1, Real y2)
 
529
{
 
530
        xminval = x1;
 
531
        xmaxval = x2;
 
532
        yminval = y1;
 
533
        ymaxval = y2;
 
534
 
 
535
        for (int curva=0; curva < Plot_curves; curva++)
 
536
        {
 
537
                if (Input_format[curva] == ROSE_BINARY)
 
538
                {
 
539
                        yminval = - ymaxval;
 
540
                        xmaxval = ymaxval;
 
541
                        xminval = yminval;
 
542
                }
 
543
        }
 
544
}
 
545
 
 
546
int f_spoly(Real x, Real_Vector terms, unsigned n)
 
547
{
 
548
        unsigned i;
 
549
        terms[0] = 1.0;
 
550
        for (i = 1; i < n; i++) terms[i] = x * terms[i-1];
 
551
        return 0;
 
552
}
 
553
 
 
554
int f_sexp(Real x, Real_Vector terms, unsigned n)
 
555
{
 
556
    double value;
 
557
        value = exp(x);
 
558
    if (value != HUGE_VAL)
 
559
        {
 
560
        terms[0] = 1.0;
 
561
        terms[n-1] = (Real) value;
 
562
        return 0;
 
563
    }
 
564
        else
 
565
        {
 
566
       show_err("Value of exp(x) is too large");
 
567
       return -1;
 
568
    }
 
569
}
 
570
 
 
571
int f_slog(Real x, Real_Vector terms, unsigned n)
 
572
{
 
573
        if (x > 0.0) {
 
574
                terms[0] = 1.0;
 
575
                terms[n-1] = (Real) log(x);
 
576
                return 0;
 
577
        }
 
578
        else {
 
579
                show_err("Value of x is less than or equal to 0");
 
580
                return -1;
 
581
        }
 
582
}
 
583
 
 
584
int f_ssin(Real x, Real_Vector terms, unsigned n)
 
585
{
 
586
        errno = 0;
 
587
        terms[0] = 1.0;
 
588
    terms[1] = (Real) sin(x);
 
589
    terms[n-1] = (Real) cos(x);
 
590
    if (errno == 0)
 
591
                return 0;
 
592
    else
 
593
        {
 
594
        show_err("Loss of significance for cos(x) or sin(x)");
 
595
        return -1;
 
596
    }
 
597
}
 
598
 
 
599
void show_err (char* /* mess */)
 
600
{
 
601
//      printf("%s\n",mess);
 
602
}
 
603
 
 
604
//---------------------------------------------------------------------------------
 
605
 
 
606
int
 
607
TeGraphPlot :: kill(int order)
 
608
{
 
609
        int i, count = 0;
 
610
        if (Plot_curves == 0)
 
611
                return 0;
 
612
        for (i = 0; i < Plot_curves; i++)
 
613
        {
 
614
                if (Plot_x[i] == Plot_x[order]) count++;
 
615
        }
 
616
 
 
617
        if (count == 1)
 
618
        {
 
619
                delete [] Plot_x[order];
 
620
                Plot_x[order] = 0;
 
621
                delete [] Plot_y[order];
 
622
                Plot_y[order] = 0;
 
623
        }
 
624
 
 
625
        for (i = order; i < Plot_curves-1; i++)
 
626
        {
 
627
                Plot_x[i] = Plot_x[i+1];
 
628
                Plot_y[i] = Plot_y[i+1];
 
629
                Plot_points[i] = Plot_points[i+1];
 
630
                strcpy(Plot_label.curve[i], Plot_label.curve[i+1]);
 
631
        }
 
632
        Plot_curves--;
 
633
        return 1;
 
634
}
 
635
 
 
636
 
 
637
//---------------------------------------------------------------------------------
 
638
void
 
639
TeGraphPlot :: plotCurves ( int with_simbols)
 
640
{
 
641
        short   drawplot = true;
 
642
        Plot_symbols = with_simbols; // numero de pontos
 
643
                                     // que devem ser plotados
 
644
                                     // sobre a curva
 
645
        for (int curva=0; curva < Plot_curves; curva++)
 
646
        {
 
647
                if (Plot_y[curva] == NULL)
 
648
                        continue;
 
649
 
 
650
                if (Input_format[curva] == PIE_BINARY && sliceVector)
 
651
                {
 
652
                        plot_title(Plot_label.title);
 
653
 
 
654
                        int             i, w, h, xc, yc, points;
 
655
                        
 
656
                        w = min(width,height);
 
657
                        h = min(width,height);
 
658
                        xc = xupleft ;
 
659
                        yc = yupleft ;
 
660
                        points =  Plot_points[0];
 
661
 
 
662
                        Real_Vector y = Plot_y[0];
 
663
 
 
664
                        Real    count = 0., value;
 
665
 
 
666
                        for (i = 0; i < points; i++) count += y[i];
 
667
                        if (count == 0.) break;
 
668
 
 
669
                        double  a1 = 0.,
 
670
                                        a2 = 0.;
 
671
 
 
672
                        for (i = 0; i < points; i++)
 
673
                        {
 
674
                                TeLegendEntry slice = (*sliceVector)[i];
 
675
                                TeColor cor = slice.visual(TePOLYGONS).color ();
 
676
                                value = y[i]/count;
 
677
                                a2 += value*360.;
 
678
                                setFillAreaColor(cor);
 
679
                                fillArc (xc, yc, w, h, a1, a2, cor);
 
680
                                if (value > .08)
 
681
                                {
 
682
                                        char percent[16];
 
683
                                        sprintf (percent,"     %.1f",value*100);
 
684
                                        drawText (xc+w/2, yc+h/2, percent, (a1+a2)/2);
 
685
                                }
 
686
                                a1 = a2;
 
687
                        }
 
688
                        pieLegends ();
 
689
                }
 
690
                else if (Input_format[curva] == BAR_BINARY)
 
691
                {
 
692
                //-------------------------------------
 
693
                //           ^
 
694
                //           |      .....
 
695
                //           |      :   :....
 
696
                //           |      :   :   :
 
697
                //           |  ....:   :   :    
 
698
                //           |  :   :   :   :
 
699
                //           |__:_|_:_|_:_|_:____
 
700
                //                1   2   3 
 
701
                //------------------------------------- 
 
702
                        int             x1, y1, x2, y2;
 
703
                        int i;
 
704
 
 
705
//                      Real_Vector     x = Plot_x[0],
 
706
//                                      y = Plot_y[0];
 
707
                        Real_Vector     x = Plot_x[curva],
 
708
                                        y = Plot_y[curva];
 
709
 
 
710
//                      setLineAttributes(Graph_color.curve[0],0,0);
 
711
//                      setFillAreaColor(Graph_color.curve[0]);
 
712
                        setLineAttributes(Graph_color.curve[curva],TeLnTypeContinuous,0);
 
713
                        setFillAreaColor(Graph_color.curve[curva]);
 
714
 
 
715
                        for (i = 0; i < Plot_points[curva]; i++)
 
716
                        {
 
717
                                // Juan begin
 
718
                                if(sliceVector)
 
719
                                {
 
720
                                        TeLegendEntry slice = (*sliceVector)[i];
 
721
                                        TeColor cor = slice.visual(TePOLYGONS).color ();
 
722
                                        setFillAreaColor (cor);
 
723
                                }
 
724
                                // Juan end
 
725
//                              if (i < Plot_points[0]-1)
 
726
                                if (i < Plot_points[curva]-1)
 
727
                                {
 
728
                                        value_to_pixel(x[i], y[i], &x1, &y1);
 
729
                                        value_to_pixel(x[i+1], y[i+1], &x2, &y2);
 
730
                                        if ((height + yupleft - y1 - 1) > 0)
 
731
                                                fillRectangle(x1-(x2-x1)/2, y1, (x2-x1-2), height + yupleft - y1);
 
732
                                }
 
733
                                else
 
734
                                {
 
735
                                        value_to_pixel(x[i-1], y[i-1], &x1, &y1);
 
736
                                        value_to_pixel(x[i], y[i], &x2, &y2);
 
737
                                        if ((height + yupleft - y2 - 1) > 0)
 
738
                                                fillRectangle(x2-(x2-x1)/2, y2, (x2-x1-2), height + yupleft - y2);
 
739
                                }
 
740
                        }
 
741
                        draw_plot();
 
742
                }
 
743
                else if (Input_format[curva] == ROSE_BINARY)
 
744
                {
 
745
                        int i;
 
746
                        int x1, y1, x2, y2;
 
747
                        int     a1 = 0,
 
748
                                a2 = 0;
 
749
                        Real_Vector     x = Plot_x[0],
 
750
                                        y = Plot_y[0];
 
751
 
 
752
                        plot_title(Plot_label.title);
 
753
 
 
754
                        for (i = 0; i < Plot_points[0]-1; i++)
 
755
                        {
 
756
                                if (y[i]==0.)continue;
 
757
                                a1 = (int)(x[i] * 64.);
 
758
                                a2 = (int)(x[i+1] * 64.) - a1;
 
759
                        value_to_pixel(-y[i], y[i], &x1, &y1);
 
760
                        value_to_pixel(y[i], -y[i], &x2, &y2);
 
761
                                fillArc (x1, y1, x2-x1, y2-y1, a1, a2, Graph_color.curve[0]);
 
762
                        }
 
763
 
 
764
                        Real step = ymaxval / (Real)major_tick;
 
765
 
 
766
                        for(Real j = step ; j <= ymaxval; j += step )
 
767
                        { 
 
768
                                value_to_pixel(-j, j, &x1, &y1);
 
769
                                value_to_pixel(j, -j, &x2, &y2);
 
770
 
 
771
                                setLineAttributes(Graph_color.lines,TeLnTypeContinuous,0);
 
772
                                drawArc(x1, y1, x2-x1, y2-y1,0,360*64,Graph_color.lines);
 
773
                        }
 
774
                }
 
775
                else if (Input_format[curva] == POINT_BINARY)
 
776
                {
 
777
                        draw_plot();
 
778
                
 
779
                        for (int j = 0; j < Plot_points[curva]; j ++)
 
780
                                plotSymbol(Plot_x[curva][j], Plot_y[curva][j], Symbol[curva], GPpointwidth, GPpointheight, curva);
 
781
 
 
782
                }
 
783
                else
 
784
                {
 
785
                        if (drawplot)
 
786
                        {
 
787
                                draw_plot();
 
788
                                drawplot = false;
 
789
                        }
 
790
 
 
791
                        plot_curve(Plot_x[curva], Plot_y[curva], Plot_points[curva],Graph_color.curve[curva]);
 
792
                
 
793
                        if (Plot_symbols)
 
794
                                draw_symbols(curva);
 
795
                }
 
796
        }
 
797
}
 
798
 
 
799
//---------------------------------------------------------------------------------
 
800
void
 
801
TeGraphPlot :: draw_plot(void)
 
802
{
 
803
 
 
804
        plot_axes();
 
805
        plot_ticks();
 
806
//      if (Options.grid[1] == 'n')
 
807
        plot_grid();
 
808
        plot_labels();
 
809
        plot_title(Plot_label.title);
 
810
        plot_xaxis(Plot_label.xaxis);
 
811
        plot_yaxis(Plot_label.yaxis);
 
812
//      if (Options.legend[1] == 'n') 
 
813
        drawLegend();
 
814
}
 
815
 
 
816
//---------------------------------------------------------------------------------
 
817
void
 
818
TeGraphPlot :: setSymbolType(int type, int curve)
 
819
{
 
820
        if(curve>=0 && curve<=9)
 
821
                Symbol[curve] = type;
 
822
}
 
823
 
 
824
//---------------------------------------------------------------------------------
 
825
void
 
826
TeGraphPlot :: draw_symbols(int curve)
 
827
{
 
828
        int i, step;
 
829
 
 
830
        if (Plot_symbols > 0)
 
831
        {
 
832
                step = Plot_points[curve]/Plot_symbols;
 
833
                if (Plot_points[curve] % Plot_symbols != 0) step++;
 
834
                for (i = 0; i < Plot_points[curve]; i += step)
 
835
                        plotSymbol(Plot_x[curve][i], Plot_y[curve][i], Symbol[curve], GPpointwidth, GPpointheight, curve);
 
836
        }
 
837
}
 
838
 
 
839
//---------------------------------------------------------------------------------
 
840
void
 
841
TeGraphPlot :: plot_axes(void)
 
842
{
 
843
    setLineAttributes(Graph_color.lines,TeLnTypeContinuous,0);
 
844
    drawRectangle(xupleft, yupleft, width, height);
 
845
}
 
846
 
 
847
//---------------------------------------------------------------------------------
 
848
void
 
849
TeGraphPlot :: plot_ticks(void)
 
850
{
 
851
    int i, x, y, maximum, delta, length;
 
852
    int x_ticks = x_major * x_minor;
 
853
    int y_ticks = y_major * y_minor;
 
854
 
 
855
    setLineAttributes(Graph_color.lines,TeLnTypeContinuous,0);
 
856
        delta = (width-1)/x_ticks;
 
857
        x = xupleft + delta;
 
858
        y = yupleft + 1;
 
859
        maximum = yupleft + height ;
 
860
        for (i = 1; i < x_ticks; i++)
 
861
        {
 
862
                if (i % x_minor == 0)
 
863
                        length = major_tick;
 
864
                else
 
865
                        length = minor_tick;
 
866
                drawLine(x, y, x, y + length );
 
867
                drawLine(x, maximum, x, maximum - length );
 
868
                x += delta;
 
869
        }
 
870
        delta = (height-1)/y_ticks;
 
871
        x = xupleft + 1;
 
872
        y = yupleft + delta;
 
873
        maximum = xupleft + width -1;
 
874
        for (i = 1; i < y_ticks; i++)
 
875
        {
 
876
                if (i % y_minor == 0)
 
877
                        length = major_tick;
 
878
                else
 
879
                        length = minor_tick;
 
880
                drawLine(x, y, x + length , y);
 
881
                drawLine(maximum, y, maximum - length , y);
 
882
                y += delta;
 
883
        }
 
884
}
 
885
 
 
886
void TeGraphPlot :: plot_grid(void)
 
887
{
 
888
        int i, x, y, maximum, delta;
 
889
        setLineAttributes(Graph_color.grid,TeLnTypeDotted,0);
 
890
        delta = (width-1)/x_major;
 
891
        x = xupleft + delta;
 
892
        maximum = yupleft + height - major_tick - 1;
 
893
        for (i = 1; i < x_major; i++)
 
894
                {
 
895
                y = yupleft + 1 + major_tick;
 
896
 //             while (y < maximum) {
 
897
 //                     drawPoint (x,y);
 
898
 //                     y += 3;
 
899
 //             }
 
900
                        drawLine(x,y,x,maximum);
 
901
                x += delta;
 
902
        }
 
903
        delta = (height-1)/y_major;
 
904
        y = yupleft + delta;
 
905
        maximum = xupleft + width - major_tick - 1;
 
906
        for (i = 1; i < y_major; i++)
 
907
                {
 
908
                x = xupleft + 1 + major_tick;
 
909
 //             while (x < maximum) {
 
910
 //                     drawPoint (x,y);
 
911
 //                     x += 4;
 
912
 //         }
 
913
                        drawLine(x,y,maximum,y);
 
914
                y += delta;
 
915
        }
 
916
}
 
917
 
 
918
int TeGraphPlot :: plot_labels(void)
 
919
{
 
920
    char text[80];
 
921
    int i, x, y, delta, font;
 
922
    int digits_left, digits_right, exp_flag;
 
923
    Real value, values[3];
 
924
        setTextColor(Graph_color.text);
 
925
 
 
926
        if ((font = set_font(label_font)) == ERR) return ERR;
 
927
        values[0] = yminval;
 
928
        values[1] = ymaxval;
 
929
        values[2] = (values[1] - values[0]) / y_major;
 
930
        label_format(values, 3, label_field, &digits_left, &digits_right, &exp_flag);
 
931
        delta = (height-1) / y_major;
 
932
        y = yupleft - label_height/2;
 
933
        value = ymaxval;
 
934
        Yaxis_x = xupleft;
 
935
        for (i = 0; i <= y_major; i++)
 
936
        {
 
937
                ftos(value, digits_left, digits_right, exp_flag, text);
 
938
                x = xupleft - strlen(text) * label_width - label_width/2;
 
939
                if (x < Yaxis_x)
 
940
                        Yaxis_x = x;
 
941
                drawText (x, y+8, text);
 
942
                y += delta;
 
943
                value -= values[2];
 
944
        }
 
945
        if (yaxis_direct == HORIZ_FONT)
 
946
        {
 
947
                Yaxis_x -= (yaxis_width + label_width/2);
 
948
                if (Yaxis_x < 0)
 
949
                        Yaxis_x = 0;
 
950
        }
 
951
        else
 
952
        {
 
953
                Yaxis_x -= label_width/2;
 
954
                if (Yaxis_x < yaxis_height)
 
955
                        Yaxis_x = yaxis_height;
 
956
        }
 
957
        values[0] = xminval;
 
958
        values[1] = xmaxval;
 
959
        values[2] = (values[1] - values[0]) / x_major;
 
960
        label_format(values, 3, label_field, &digits_left, &digits_right, &exp_flag);
 
961
        delta = (width-1) / x_major;
 
962
        y = yupleft + height + label_height/2;
 
963
        value = xminval;
 
964
        for (i = 0; i <= x_major; i++)
 
965
        {
 
966
                ftos(value, digits_left, digits_right, exp_flag, text);
 
967
                x = xupleft + i*delta - strlen(text) * label_width / 2;
 
968
                drawText (x, y+8, text);
 
969
                value += values[2];
 
970
                if (x_major > 5)
 
971
                {
 
972
                        i++;
 
973
                        value += values[2];
 
974
                // if (x_major > 9) {
 
975
                //      i++;
 
976
                //      value += values[2];
 
977
                //}
 
978
                }
 
979
        }
 
980
//      set_font(font);
 
981
        return 0;
 
982
}
 
983
 
 
984
int TeGraphPlot :: plot_title(char *text)
 
985
{
 
986
        int font;
 
987
        if ((font = set_font(title_font)) == ERR) return ERR;
 
988
        plot_horiz(text, Title_y, title_width);
 
989
        set_font(font);
 
990
        return 0;
 
991
}
 
992
 
 
993
int TeGraphPlot :: plot_xaxis(char *text)
 
994
{
 
995
        int font;
 
996
        if ((font = set_font(xaxis_font)) == ERR) return ERR;
 
997
        plot_horiz(text, Xaxis_y, xaxis_width);
 
998
        set_font(font);
 
999
        return 0;
 
1000
}
 
1001
 
 
1002
void
 
1003
TeGraphPlot :: plot_horiz(char *text, int y, int w)
 
1004
{
 
1005
        int x, l, count;
 
1006
        char *ptr = NULL,buffer[80];
 
1007
        setTextColor(Graph_color.text);
 
1008
        l = strlen(text);
 
1009
        x = xupleft + (width-1)/2 - (l * w)/2;
 
1010
        ptr = text;
 
1011
        if (x < xupleft) {
 
1012
                count = (xupleft - x)/w;
 
1013
                buffer[79] = '\0';
 
1014
                strncpy(buffer, text, sizeof(buffer)-1);
 
1015
                buffer[l-count-1] = '\0';
 
1016
                x = xupleft;
 
1017
                ptr = buffer;
 
1018
        }
 
1019
        drawText (x, y, ptr);
 
1020
        ptr = NULL;
 
1021
}
 
1022
 
 
1023
int
 
1024
TeGraphPlot :: plot_yaxis(char *text)
 
1025
{
 
1026
    int y, length, count, font, delta;
 
1027
        char *ptr= NULL, buffer[80], c[2];
 
1028
    setTextColor(Graph_color.text);
 
1029
        c[0] = '*'; c[1] = '\0';
 
1030
        if ((font = set_font(yaxis_font)) == ERR) return ERR;
 
1031
        length = strlen(text);
 
1032
        ptr = text;
 
1033
        delta = yaxis_direct == HORIZ_FONT ?
 
1034
            yaxis_height : yaxis_width;
 
1035
        y = (Title_y + Xaxis_y + delta * length) / 2;
 
1036
        buffer[79] = '\0';
 
1037
        strncpy(buffer, text, sizeof(buffer)-1);
 
1038
        if (y < Title_y)
 
1039
        {
 
1040
                count = 2 * (Title_y - y)/delta;
 
1041
                buffer[length-count-1] = '\0';
 
1042
                y = Title_y;
 
1043
                ptr = buffer;
 
1044
        }
 
1045
        if (yaxis_direct == HORIZ_FONT)
 
1046
        {
 
1047
                while (*ptr != '\0')
 
1048
                {
 
1049
                        c[0] = *ptr++;
 
1050
                        drawText (Yaxis_x, y, c);
 
1051
                        y += yaxis_height;
 
1052
                }
 
1053
        }
 
1054
        else
 
1055
                drawText (Yaxis_x, y, ptr, 90.);
 
1056
        ptr = NULL;
 
1057
 
 
1058
        set_font(font);
 
1059
        return 0;
 
1060
}
 
1061
 
 
1062
int
 
1063
TeGraphPlot :: plot_curve(Real_Vector x, Real_Vector y,int points,TeColor c)
 
1064
{
 
1065
        int i, count;
 
1066
 
 
1067
        setLineAttributes(c,TeLnTypeContinuous,0);
 
1068
 
 
1069
        count = plot_point(x[0], y[0], PEN_UP);
 
1070
        for (i = 1; i < points; i++)
 
1071
                count += plot_point(x[i], y[i], PEN_DOWN);
 
1072
        return count;
 
1073
}
 
1074
 
 
1075
int
 
1076
TeGraphPlot :: plot_point(Real x_value, Real y_value, int pen_status)
 
1077
{
 
1078
    static Real last_x_value, last_y_value;
 
1079
    static int last_x, last_y;
 
1080
    Real temp_x, temp_y;
 
1081
    int x, y, status=0;
 
1082
 
 
1083
    if (point_visible(x_value, y_value))
 
1084
        {
 
1085
        value_to_pixel(x_value, y_value, &x, &y);
 
1086
        if (pen_status == PEN_UP)
 
1087
                {
 
1088
//                              SET_PIXEL(x, y, color);
 
1089
                }
 
1090
        else
 
1091
                {
 
1092
            if (!point_visible(last_x_value, last_y_value))
 
1093
                        {
 
1094
                                temp_x = last_x_value;
 
1095
                                temp_y = last_y_value;
 
1096
                find_intersect(x_value, y_value, &temp_x, &temp_y);
 
1097
                value_to_pixel(temp_x, temp_y, &last_x, &last_y);
 
1098
            }
 
1099
                        if (last_y_value < TeMAXFLOAT)
 
1100
                drawLine(last_x, last_y, x, y);
 
1101
        }
 
1102
        last_x = x;
 
1103
        last_y = y;
 
1104
        status = 1;
 
1105
    }
 
1106
    else if (pen_status == PEN_DOWN && point_visible(last_x_value, last_y_value))
 
1107
        {
 
1108
                if (y_value < TeMAXFLOAT)
 
1109
                {
 
1110
                        temp_x = x_value;
 
1111
                        temp_y = y_value;
 
1112
                        find_intersect(last_x_value, last_y_value, &temp_x, &temp_y);
 
1113
                        value_to_pixel(temp_x, temp_y, &x, &y);
 
1114
                        drawLine(last_x, last_y, x, y);
 
1115
                }
 
1116
        status = 0;
 
1117
    }
 
1118
    last_x_value = x_value;
 
1119
        last_y_value = y_value;
 
1120
    return status;
 
1121
}
 
1122
 
 
1123
int
 
1124
TeGraphPlot :: plotSymbol(Real x_value, Real y_value, int symbol, int w, int h, int curve)
 
1125
{
 
1126
    int x, y;
 
1127
 
 
1128
        setLineAttributes(Graph_color.symbol[curve],TeLnTypeContinuous,1);
 
1129
 
 
1130
        if (point_visible(x_value, y_value))
 
1131
        {
 
1132
                value_to_pixel(x_value, y_value, &x, &y);
 
1133
                draw_symbol(x, y, symbol, w, h);
 
1134
                return true;
 
1135
    }
 
1136
    return false;
 
1137
}
 
1138
 
 
1139
void
 
1140
TeGraphPlot :: draw_symbol(int x, int y,int symbol, int w, int h)
 
1141
{
 
1142
        if( symbol < 0 )                //V2.0-01 GAIA
 
1143
        {
 
1144
                symbol = 0;
 
1145
        }
 
1146
        symbol = symbol % 5;            //V2.0-01 GAIA
 
1147
 
 
1148
        switch (symbol)
 
1149
        {
 
1150
        case RECTANGLE:
 
1151
                drawLine(x - w/2, y - h/2, x + w/2, y - h/2);
 
1152
                drawLine(x + w/2, y - h/2, x + w/2, y + h/2);
 
1153
                drawLine(x + w/2, y + h/2, x - w/2, y + h/2);
 
1154
                drawLine(x - w/2, y + h/2, x - w/2, y - h/2);
 
1155
                break;
 
1156
 
 
1157
        case TRIANGLE:
 
1158
                drawLine(x - w/2, y + h/2, x, y - h/2);
 
1159
                drawLine(x, y - h/2, x + w/2, y + h/2);
 
1160
                drawLine(x + w/2, y + h/2, x - w/2, y + h/2);
 
1161
                break;
 
1162
 
 
1163
        case DIAMOND:
 
1164
                drawLine(x - w/2, y, x, y - h/2);
 
1165
                drawLine(x, y - h/2, x + w/2, y);
 
1166
                drawLine(x + w/2, y, x, y + h/2);
 
1167
                drawLine(x, y + h/2, x - w/2, y);
 
1168
                break;
 
1169
 
 
1170
        case SCROSS:
 
1171
                drawLine(x - w/2, y, x + w/2, y);
 
1172
                drawLine(x, y - h/2, x, y + h/2);
 
1173
                break;
 
1174
 
 
1175
        case XCROSS:
 
1176
                drawLine(x - w/2, y + h/2, x + w/2, y - h/2);
 
1177
                drawLine(x - w/2, y - h/2, x + w/2, y + h/2);
 
1178
                break;
 
1179
        }
 
1180
}
 
1181
 
 
1182
 
 
1183
void TeGraphPlot :: draw_symbol_legend(int x, int y, int symbol, int w, int h)
 
1184
{
 
1185
        if( symbol < 0 )                //V2.0-01 GAIA
 
1186
        {
 
1187
                symbol = 0;
 
1188
        }
 
1189
        symbol = symbol % 5;            //V2.0-01 GAIA
 
1190
 
 
1191
        switch (symbol)
 
1192
        {
 
1193
        case RECTANGLE:
 
1194
                drawLine(x - w/2, y - h/2, x + w/2, y - h/2);
 
1195
                drawLine(x + w/2, y - h/2, x + w/2, y + h/2);
 
1196
                drawLine(x + w/2, y + h/2, x - w/2, y + h/2);
 
1197
                drawLine(x - w/2, y + h/2, x - w/2, y - h/2);
 
1198
                break;
 
1199
 
 
1200
        case TRIANGLE:
 
1201
                drawLine(x - w/2, y + h/2, x, y - h/2);
 
1202
                drawLine(x, y - h/2, x + w/2, y + h/2);
 
1203
                drawLine(x + w/2, y + h/2, x - w/2, y + h/2);
 
1204
                break;
 
1205
 
 
1206
        case DIAMOND:
 
1207
                drawLine(x - w/2, y, x, y - h/2);
 
1208
                drawLine(x, y - h/2, x + w/2, y);
 
1209
                drawLine(x + w/2, y, x, y + h/2);
 
1210
                drawLine(x, y + h/2, x - w/2, y);
 
1211
                break;
 
1212
 
 
1213
        case SCROSS:
 
1214
                drawLine(x - w/2, y, x + w/2, y);
 
1215
                drawLine(x, y - h/2, x, y + h/2);
 
1216
                break;
 
1217
 
 
1218
        case XCROSS:
 
1219
                drawLine(x - w/2, y + h/2, x + w/2, y - h/2);
 
1220
                drawLine(x - w/2, y - h/2, x + w/2, y + h/2);
 
1221
                break;
 
1222
    }
 
1223
}
 
1224
 
 
1225
 
 
1226
void
 
1227
TeGraphPlot :: label_format(Real values[], int nvalues, int field_width,
 
1228
                   int *digits_left, int *digits_right, int *exp_flag)
 
1229
{
 
1230
        char *str = NULL;
 
1231
        int i, j, digits, decimal, sign, trail0;
 
1232
 
 
1233
        digits = sizeof(Real) == sizeof(float) ? FLT_DIG : DBL_DIG;
 
1234
        *digits_left = *digits_right = *exp_flag = 0;
 
1235
        for (i = 0; i < nvalues && *exp_flag == 0; i++) {
 
1236
                str = ecvt(values[i], digits, &decimal, &sign);
 
1237
                if (decimal > *digits_left) *digits_left = decimal;
 
1238
                for (j = digits-1, trail0 = 0; j >= 0 && str[j] == '0'; j--) trail0++;
 
1239
                j = digits - trail0 - decimal;
 
1240
                if (j > *digits_right) *digits_right = j;
 
1241
                if (*digits_right + *digits_left > field_width-2) {
 
1242
                        *exp_flag = 1;
 
1243
                        *digits_left = 1;
 
1244
                        *digits_right = field_width - 8;
 
1245
                        if (*digits_right < 0) *digits_right = 0;
 
1246
                }
 
1247
        }
 
1248
         str = NULL;
 
1249
}
 
1250
 
 
1251
void TeGraphPlot :: find_intersect(Real x_in, Real y_in,
 
1252
                    Real *x_out, Real *y_out)
 
1253
{
 
1254
        // find values for x_out and y_out that intersect the
 
1255
        // plot boundary on a line between the points (x_in, y_in) inside
 
1256
        // the plot region and (x_out, y_out) outside the plot region
 
1257
 
 
1258
        Real m, b, temp_x, temp_y;
 
1259
        m = (y_in - *y_out) / (x_in - *x_out);
 
1260
        b = y_in - m * x_in;
 
1261
        if (*x_out >= xminval && *x_out <= xmaxval) {
 
1262
                // only y_out is outside plot region
 
1263
                if (*y_out < yminval) *y_out = yminval;
 
1264
                else if (*y_out > ymaxval) *y_out = ymaxval;
 
1265
                *x_out = (*y_out - b) / m;
 
1266
        }
 
1267
        else
 
1268
        { 
 
1269
                if (*y_out >= yminval && *y_out <= ymaxval) {
 
1270
                        // only x_out is outside plot region
 
1271
                        if (*x_out < xminval) *x_out = xminval;
 
1272
                        else if (*x_out > xmaxval) *x_out = xmaxval;
 
1273
                        *y_out = *x_out * m + b;
 
1274
                }
 
1275
                else
 
1276
                {
 
1277
                        // both x_out and y_out are outside plot region
 
1278
                        if (*y_out < yminval) temp_y = yminval;
 
1279
                        else temp_y = ymaxval;
 
1280
                        temp_x = (temp_y - b) / m;
 
1281
                        if (temp_x >= xminval && temp_x <= xmaxval) {
 
1282
                                *y_out = temp_y;
 
1283
                                *x_out = temp_x;
 
1284
                        }
 
1285
                        else {
 
1286
                                if (*x_out < xminval) *x_out = xminval;
 
1287
                                else *x_out = xmaxval;
 
1288
                                *y_out = *x_out * m + b;
 
1289
                        }
 
1290
                }
 
1291
        }
 
1292
}
 
1293
 
 
1294
//----------------------------------------------------------------------------------------
 
1295
void
 
1296
TeGraphPlot :: drawLegend(void)
 
1297
{
 
1298
    int x, y, h, delta;
 
1299
    int i, count = 0;
 
1300
 
 
1301
        for (i = 0; i < Plot_curves; i++)
 
1302
        {
 
1303
        if (Plot_label.curve[i][0] != '\0') count++;
 
1304
        }
 
1305
 
 
1306
    if (count != 0)
 
1307
        {
 
1308
        h = Font_height[0] + 4;
 
1309
                if (count > 3) h += Font_height[0] + 3;
 
1310
        y = getHeight() - h;
 
1311
 
 
1312
                setLineAttributes(Graph_color.lines,TeLnTypeContinuous,0);
 
1313
                drawRectangle(0, y, width - 1, h);
 
1314
 
 
1315
        x = Font_width[0] + 1;
 
1316
                delta = Font_height[0] + 3;
 
1317
        for (count = 0, i = 0; i < Plot_curves; i++)
 
1318
        {
 
1319
            if (Plot_label.curve[i][0] != '\0' && count < 7)
 
1320
                        {
 
1321
                if (count++ == 3) {
 
1322
                    x = Font_width[0] + 1;
 
1323
                    y += Font_height[0] + 6;
 
1324
                }
 
1325
 
 
1326
                                setLineAttributes(Graph_color.curve[i],TeLnTypeContinuous,0);
 
1327
                                draw_symbol_legend(x, y + delta - Font_height[0]/2, Symbol[i], 7, 7);
 
1328
                x += Font_width[0];
 
1329
 
 
1330
                                setTextColor(Graph_color.text);
 
1331
                drawText (x, y + delta - Font_height[0], Plot_label.curve[i]);
 
1332
                                x += 10 * Font_width[0];
 
1333
            }
 
1334
        }
 
1335
        }
 
1336
}
 
1337
 
 
1338
//----------------------------------------------------------------------------------------
 
1339
void
 
1340
TeGraphPlot :: value_to_pixel(Real x_value, Real y_value, int *x_pixel, int *y_pixel)
 
1341
{
 
1342
        *x_pixel = (int) ((x_value - xminval) /
 
1343
               (xmaxval - xminval) * (width - 1) + xupleft);
 
1344
        *y_pixel = (int) ((ymaxval - y_value) /
 
1345
               (ymaxval - yminval) * (height - 1) + yupleft);
 
1346
}
 
1347
 
 
1348
//----------------------------------------------------------------------------------------
 
1349
void
 
1350
TeGraphPlot :: pixelToValue(int x_pixel, int y_pixel, Real& x_value, Real& y_value)
 
1351
{
 
1352
        x_value = (Real) (x_pixel - xupleft) / (width-1) *
 
1353
               (xmaxval - xminval) + xminval;
 
1354
        y_value = (Real) (yupleft - y_pixel) / (height-1) *
 
1355
               (ymaxval - yminval) + ymaxval;
 
1356
}
 
1357
 
 
1358
//----------------------------------------------------------------------------------------
 
1359
int
 
1360
TeGraphPlot :: point_visible(Real x, Real y)
 
1361
{
 
1362
        if (x >= xminval && x <= xmaxval &&
 
1363
        y >= yminval && y <= ymaxval) return 1;
 
1364
        else return 0;
 
1365
}
 
1366
 
 
1367
//FAMI0395 int set_font(int font_number)
 
1368
int set_font(int)
 
1369
{
 
1370
/*
 
1371
        static int font;
 
1372
        int previous_font;
 
1373
        // set font to specified font number
 
1374
        if (_set_font(font_number) == 0) {
 
1375
                previous_font = font;
 
1376
                font = font_number;
 
1377
                return previous_font;
 
1378
        }
 
1379
        else {
 
1380
                video_err = FONT_ERR;
 
1381
                return ERR;
 
1382
        } */
 
1383
 
 
1384
        return (int)true;       //FAMI0394
 
1385
}
 
1386
 
 
1387
void ftos(Real f, int digits_left, int digits_right, int exp_flag, char *s)
 
1388
{
 
1389
        int i = 0, decimal, sign;
 
1390
        char *str = NULL;
 
1391
        str = ecvt(f, digits_left+digits_right, &decimal, &sign);
 
1392
        if (sign) *s++ = '-';
 
1393
        if (exp_flag)
 
1394
        {       /* exponential format */
 
1395
                for (i=0; i < digits_left; i++) *s++ = *str++;
 
1396
                *s++ = '.';
 
1397
                for (i=0; i < digits_right; i++) *s++ = *str++;
 
1398
                *s++ = 'e';
 
1399
                decimal -= digits_left;
 
1400
                if (decimal < 0)
 
1401
                {
 
1402
                        *s++ = '-';
 
1403
                        decimal = -decimal;
 
1404
                }
 
1405
                else *s++ = '+';
 
1406
                s += 3;
 
1407
                *s-- = '\0';
 
1408
                for (i = 0; i < 3; i++)
 
1409
                {
 
1410
                        *s-- = (char) (decimal % 10 + (int) '0');
 
1411
                        decimal /= 10;
 
1412
                }
 
1413
        }
 
1414
        else
 
1415
        {          /* fixed point format */
 
1416
                if (decimal <= 0)
 
1417
                {
 
1418
                        if (digits_left > 0) *s++ = '0';
 
1419
                        if (digits_right > 0) *s++ = '.';
 
1420
                        for (i = decimal; i != 0 && digits_right > 0 ; i++)
 
1421
                        {
 
1422
                                *s++ = '0';
 
1423
                                digits_right--;
 
1424
                        }
 
1425
                        for (i = 0; i < digits_right; i++) *s++ = *str++;
 
1426
                }
 
1427
                else
 
1428
                {
 
1429
                        for (i = 0; i < decimal; i++) *s++ = *str++;
 
1430
                        if (digits_right > 0)
 
1431
                        {
 
1432
                                *s++ = '.';
 
1433
                                for (i = 0; i < digits_right; i++) *s++ = *str++;
 
1434
                        }
 
1435
                }
 
1436
                *s = '\0';
 
1437
        }
 
1438
        str = NULL;
 
1439
}
 
1440
 
 
1441
//----------------------------------------------------------------------------------------
 
1442
 
 
1443
void
 
1444
TeGraphPlot :: plotInit(int pixels_above, int pixels_below, int pixels_left, int pixels_right)
 
1445
{
 
1446
// initialize Plot variables, reserving the number of pixels specified by
 
1447
// pixels_above, pixels_below, pixels_left, and pixels_right for use by
 
1448
// the program
 
1449
 
 
1450
        int delta, x_ticks, y_ticks;
 
1451
 
 
1452
// initialize variables appropriately for current plotting device
 
1453
        X_pixels = getWidth ();
 
1454
        Y_pixels = getHeight ();
 
1455
 
 
1456
    get_plot_fonts();
 
1457
    get_font_size(title_font, &title_width, &title_height);
 
1458
    get_font_size(label_font, &label_width, &label_height);
 
1459
    get_font_size(xaxis_font, &xaxis_width, &xaxis_height);
 
1460
    yaxis_direct = get_font_size(yaxis_font, &yaxis_width, &yaxis_height);
 
1461
    delta = yaxis_direct == HORIZ_FONT ? yaxis_width : yaxis_height;
 
1462
        xupleft = pixels_left + delta + (label_field + 1) * label_width;
 
1463
    delta = title_height + label_height/2;
 
1464
    yupleft = pixels_above + delta + title_height;
 
1465
    Title_y = yupleft - delta;
 
1466
 
 
1467
// make width of Plot region a multiple of x_ticks
 
1468
        x_ticks = x_major * x_minor;
 
1469
        width = (X_pixels - pixels_right - xupleft -
 
1470
                label_field * label_width / 2 + 1)
 
1471
                /x_ticks*x_ticks + 1;
 
1472
 
 
1473
// make height of Plot region a multiple of y_ticks
 
1474
        y_ticks = y_major * y_minor;
 
1475
        delta = 2 * label_height + 1;
 
1476
        height = (Y_pixels - pixels_below - yupleft - delta -
 
1477
                xaxis_height)/y_ticks*y_ticks + 1;
 
1478
 
 
1479
// define y coordinate for x axis title
 
1480
        Xaxis_y = yupleft + height - 1 + delta;
 
1481
}
 
1482
 
 
1483
//----------------------------------------------------------------------------------------
 
1484
void
 
1485
TeGraphPlot :: get_plot_fonts()
 
1486
{
 
1487
        title_font = 0;
 
1488
        label_font = 0;
 
1489
        xaxis_font = 0;
 
1490
        yaxis_font = 1;
 
1491
}
 
1492
 
 
1493
//----------------------------------------------------------------------------------------
 
1494
int
 
1495
TeGraphPlot :: get_font_size(int font_number, int *w, int *h)
 
1496
{
 
1497
// return width, height, and direction of specified font number
 
1498
        if (font_number < FONT_MAX)
 
1499
        {
 
1500
                *w = Font_width[font_number];
 
1501
                *h = Font_height[font_number];
 
1502
                return Font_direct[font_number];
 
1503
        }
 
1504
        else
 
1505
        {
 
1506
                return ERR;
 
1507
        }
 
1508
}
 
1509
 
 
1510
//----------------------------------------------------------------------------------------
 
1511
void
 
1512
TeGraphPlot :: reset()
 
1513
{
 
1514
        for (int i=0; i<Plot_curves; i++)
 
1515
        {
 
1516
                Plot_x[i] = NULL;
 
1517
                Plot_y[i] = NULL;
 
1518
        }
 
1519
 
 
1520
        Plot_curves = 0;  
 
1521
        Plot_symbols = 0;  
 
1522
        Curve_choice = 0;         
 
1523
        Plot_choice = 0;
 
1524
}
 
1525
 
 
1526
void TeGraphPlot::drawMoranText (double dx1, double dy1, double dx2, double dy2)
 
1527
{
 
1528
        int     w, h, x, y;
 
1529
        int x1, y1, x2, y2, mx, my;
 
1530
 
 
1531
        setTextColor(Graph_color.text);
 
1532
 
 
1533
        value_to_pixel(dx1, dy1, &x1, &y1);
 
1534
        value_to_pixel(dx2, dy2, &x2, &y2);
 
1535
        value_to_pixel(0., 0., &mx, &my);
 
1536
 
 
1537
        string tx = "AA";
 
1538
        textExtent(tx, w, h);
 
1539
        x = (int)(mx + (double)(x2-mx)/2. - (double)w/2.);
 
1540
        y = (int)(my - (double)(my-y2)/2. + (double)h/2.);
 
1541
        drawText(x, y, (char*)tx.c_str());
 
1542
 
 
1543
        tx = "AB"; 
 
1544
        textExtent(tx, w, h);
 
1545
        x = (int)(mx - (double)(mx-x1)/2. - (double)w/2.);
 
1546
        drawText(x, y, (char*)tx.c_str());
 
1547
 
 
1548
        tx = "BB"; 
 
1549
        textExtent(tx, w, h);
 
1550
        y = (int)(my + (double)(y1-my)/2. + (double)h/2.);
 
1551
        drawText(x, y, (char*)tx.c_str());
 
1552
 
 
1553
        tx = "BA"; 
 
1554
        textExtent(tx, w, h);
 
1555
        x = (int)(mx + (double)(x2-mx)/2. - (double)w/2.);
 
1556
        drawText(x, y, (char*)tx.c_str());
 
1557
}