Subversion Repositories Applications.annuaire

Rev

Rev 77 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
77 aurelien 1
<?php
2
require_once '../jpgraph.php';
3
require_once '../jpgraph_canvas.php';
4
require_once '../jpgraph_canvtools.php';
5
 
6
 
7
class ContCanvas {
8
    public $g;
9
    public $shape,$scale;
10
    function __construct($xmax=5,$ymax=5,$width=350,$height=350) {
11
 
12
        $this->g = new CanvasGraph($width,$height);
13
        $this->scale = new CanvasScale($this->g, 0, $xmax, 0, $ymax);
14
        $this->shape = new Shape($this->g, $this->scale);
15
 
16
        //$this->g->SetFrame(true);
17
        $this->g->SetMargin(2,2,2,2);
18
        $this->g->SetMarginColor('white@1');
19
        $this->g->InitFrame();
20
    }
21
 
22
    function StrokeGrid() {
23
        list($xmin,$xmax,$ymin,$ymax) = $this->scale->Get();
24
        $this->shape->SetColor('gray');
25
        for( $col=1; $col<$xmax; ++$col ) {
26
            $this->shape->Line($col, 0, $col, $ymax);
27
        }
28
        for( $row=1; $row<$ymax; ++$row ) {
29
            $this->shape->Line(0, $row, $xmax, $row);
30
        }
31
    }
32
 
33
    function SetDatapoints($datapoints) {
34
        $ny=count($datapoints);
35
        $nx=count($datapoints[0]);
36
        $t = new Text();
37
        $t->SetFont(FF_ARIAL,FS_NORMAL,8);
38
        for( $x=0; $x < $nx; ++$x ) {
39
            for( $y=0; $y < $ny; ++$y ) {
40
                list($x1,$y1) = $this->scale->Translate($x,$y);
41
 
42
                if( $datapoints[$y][$x] > 0 )
43
                    $t->SetColor('blue');
44
                else
45
                    $t->SetColor('black');
46
                $t->SetFont(FF_ARIAL,FS_BOLD,8);
47
                $t->Set($datapoints[$y][$x]);
48
                $t->Stroke($this->g->img,$x1,$y1);
49
 
50
                $t->SetColor('gray');
51
                $t->SetFont(FF_ARIAL,FS_NORMAL,8);
52
                $t->Set("($y,$x)");
53
                $t->Stroke($this->g->img,$x1+10,$y1);
54
 
55
            }
56
        }
57
    }
58
 
59
    function DrawLinePolygons($p,$color='red') {
60
        $this->shape->SetColor($color);
61
        for ($i = 0 ; $i < count($p) ; $i++) {
62
            $x1 = $p[$i][0][0]; $y1 = $p[$i][0][1];
63
            for ($j = 1 ; $j < count($p[$i]) ; $j++) {
64
                $x2=$p[$i][$j][0]; $y2 = $p[$i][$j][1];
65
                $this->shape->Line($x1, $y1, $x2, $y2);
66
                $x1=$x2; $y1=$y2;
67
            }
68
        }
69
    }
70
 
71
    function Line($x1,$y1,$x2,$y2,$color='red') {
72
        $this->shape->SetColor($color);
73
        $this->shape->Line($x1, $y1, $x2, $y2);
74
    }
75
    function Polygon($p,$color='blue') {
76
        $this->shape->SetColor($color);
77
        $this->shape->Polygon($p);
78
    }
79
 
80
    function FilledPolygon($p,$color='lightblue') {
81
        $this->shape->SetColor($color);
82
        $this->shape->FilledPolygon($p);
83
    }
84
 
85
    function Point($x,$y,$color) {
86
        list($x1,$y1) = $this->scale->Translate($x, $y);
87
        $this->shape->SetColor($color);
88
        $this->g->img->Point($x1,$y1);
89
    }
90
 
91
    function Stroke() {
92
        $this->g->Stroke();
93
    }
94
 
95
}
96
 
97
// Calculate the area for a simple polygon. This will not work for
98
// non-simple polygons, i.e. self crossing.
99
function polygonArea($aX, $aY) {
100
    $n = count($aX);
101
    $area = 0 ;
102
    $j = 0 ;
103
    for ($i=0; $i < $n; $i++) {
104
        $j++;
105
        if ( $j == $n) {
106
            $j=0;
107
        }
108
        $area += ($aX[i]+$aX[j])*($aY[i]-$aY[j]);
109
    }
110
    return area*.5;
111
}
112
 
113
class SingleTestTriangle {
114
    const contval=5;
115
    static $maxdepth=2;
116
    static $cnt=0;
117
    static $t;
118
    public $g;
119
    public $shape,$scale;
120
    public $cont = array(2,4,5);
121
    public $contcolors = array('yellow','purple','seagreen','green','lightblue','blue','teal','orange','red','darkred','brown');
122
    public $dofill=false;
123
    public $showtriangulation=false,$triangulation_color="lightgray";
124
    public $showannotation=false;
125
    public $contlinecolor='black',$showcontlines=true;
126
    private $labels = array(), $showlabels=false;
127
    private $labelColor='black',$labelFF=FF_ARIAL,$labelFS=FS_BOLD,$labelFSize=9;
128
 
129
    function __construct($width,$height,$nx,$ny) {
130
        $xmax=$nx+0.1;$ymax=$ny+0.1;
131
        $this->g = new CanvasGraph($width,$height);
132
        $this->scale = new CanvasScale($this->g, -0.1, $xmax, -0.1, $ymax);
133
        $this->shape = new Shape($this->g, $this->scale);
134
 
135
        //$this->g->SetFrame(true);
136
        $this->g->SetMargin(2,2,2,2);
137
        $this->g->SetMarginColor('white@1');
138
        //$this->g->InitFrame();
139
 
140
        self::$t = new Text();
141
        self::$t->SetColor('black');
142
        self::$t->SetFont(FF_ARIAL,FS_BOLD,9);
143
        self::$t->SetAlign('center','center');
144
    }
145
 
146
    function getPlotSize() {
147
        return array($this->g->img->width,$this->g->img->height);
148
    }
149
 
150
    function SetContours($c) {
151
        $this->cont = $c;
152
    }
153
 
154
    function ShowLabels($aFlg=true) {
155
        $this->showlabels = $aFlg;
156
    }
157
 
158
    function ShowLines($aFlg=true) {
159
        $this->showcontlines=$aFlg;
160
    }
161
 
162
    function SetFilled($f=true) {
163
        $this->dofill = $f;
164
    }
165
 
166
    function ShowTriangulation($f=true) {
167
        $this->showtriangulation = $f;
168
    }
169
 
170
    function Stroke() {
171
        $this->g->Stroke();
172
    }
173
 
174
    function FillPolygon($color,&$p) {
175
        self::$cnt++;
176
        if( $this->dofill ) {
177
            $this->shape->SetColor($color);
178
            $this->shape->FilledPolygon($p);
179
        }
180
        if( $this->showtriangulation ) {
181
            $this->shape->SetColor($this->triangulation_color);
182
            $this->shape->Polygon($p);
183
        }
184
    }
185
 
186
    function GetNextHigherContourIdx($val) {
187
        for( $i=0; $i < count($this->cont); ++$i ) {
188
            if( $val < $this->cont[$i] ) return $i;
189
        }
190
        return count($this->cont);
191
    }
192
 
193
    function GetContVal($v1) {
194
        for( $i=0; $i < count($this->cont); ++$i ) {
195
            if( $this->cont[$i] > $v1 ) {
196
                return $this->cont[$i];
197
            }
198
        }
199
        die('No contour value is larger or equal than : '.$v1);
200
    }
201
 
202
    function GetColor($v) {
203
        return $this->contcolors[$this->GetNextHigherContourIdx($v)];
204
    }
205
 
206
    function storeAnnotation($x1,$y1,$v1,$angle) {
207
        $this->labels[$this->GetNextHigherContourIdx($v1)][] = array($x1,$y1,$v1,$angle);
208
    }
209
 
210
    function labelProx($x1,$y1,$v1) {
211
 
212
        list($w,$h) = $this->getPlotSize();
213
 
214
 
215
        if( $x1 < 20 || $x1 > $w-20 )
216
            return true;
217
 
218
        if( $y1 < 20 || $y1 > $h-20 )
219
            return true;
220
 
221
        if( !isset ($this->labels[$this->GetNextHigherContourIdx($v1)]) ) {
222
            return false;
223
        }
224
        $p = $this->labels[$this->GetNextHigherContourIdx($v1)];
225
        $n = count($p);
226
        $d = 999999;
227
        for ($i = 0 ; $i < $n ; $i++) {
228
            $xp = $p[$i][0];
229
            $yp = $p[$i][1];
230
            $d = min($d, ($x1-$xp)*($x1-$xp) + ($y1-$yp)*($y1-$yp));
231
        }
232
 
233
        $limit = $w*$h/9;
234
        $limit = max(min($limit,20000),3500);
235
        if( $d < $limit ) return true;
236
        else return false;
237
    }
238
 
239
    function putLabel($x1,$y1,$x2,$y2,$v1) {
240
 
241
        $angle = 0;
242
        if( $x2 - $x1 != 0 ) {
243
            $grad = ($y2-$y1)/($x2-$x1);
244
            $angle = -(atan($grad) * 180/M_PI);
245
            self::$t->SetAngle($angle);
246
        }
247
 
248
        $x = $this->scale->TranslateX($x1);
249
        $y = $this->scale->TranslateY($y1);
250
        if( !$this->labelProx($x, $y, $v1) ) {
251
            $this->storeAnnotation($x, $y, $v1, $angle);
252
        }
253
    }
254
 
255
    function strokeLabels() {
256
        $t = new Text();
257
        $t->SetColor($this->labelColor);
258
        $t->SetFont($this->labelFF,$this->labelFS,$this->labelFSize);
259
        $t->SetAlign('center','center');
260
 
261
        foreach ($this->labels as $cont_idx => $pos) {
262
            if( $cont_idx >= 10 ) return;
263
            foreach ($pos as $idx => $coord) {
264
                $t->Set( sprintf("%.1f",$coord[2]) );
265
                $t->SetAngle($coord[3]);
266
                $t->Stroke($this->g->img,$coord[0],$coord[1]);
267
            }
268
        }
269
    }
270
 
271
    function annotate($x1,$y1,$x2,$y2,$x1p,$y1p,$v1,$v2,$v1p) {
272
        if( !$this->showannotation ) return;
273
        /*
274
        $this->g->img->SetColor('green');
275
        $this->g->img->FilledCircle($this->scale->TranslateX($x1),$this->scale->TranslateY($y1), 4);
276
        $this->g->img->FilledCircle($this->scale->TranslateX($x2),$this->scale->TranslateY($y2), 4);
277
 
278
        $this->g->img->SetColor('red');
279
        $this->g->img->FilledCircle($this->scale->TranslateX($x1p),$this->scale->TranslateY($y1p), 4);
280
*/
281
        //self::$t->Set(sprintf("%.1f",$v1,$this->VC($v1)));
282
        //self::$t->Stroke($this->g->img,$this->scale->TranslateX($x1),$this->scale->TranslateY($y1));
283
        //self::$t->Set(sprintf("%.1f",$v2,$this->VC($v2)));
284
        //self::$t->Stroke($this->g->img,$this->scale->TranslateX($x2),$this->scale->TranslateY($y2));
285
 
286
        $x = $this->scale->TranslateX($x1p);
287
        $y = $this->scale->TranslateY($y1p);
288
        if( !$this->labelProx($x, $y, $v1p) ) {
289
            $this->storeAnnotation($x, $y, $v1p);
290
            self::$t->Set(sprintf("%.1f",$v1p,$this->VC($v1p)));
291
            self::$t->Stroke($this->g->img,$x,$y);
292
        }
293
    }
294
 
295
    function Pertubate(&$v1,&$v2,&$v3,&$v4) {
296
        $pert = 0.9999;
297
        $n = count($this->cont);
298
        for($i=0; $i < $n; ++$i) {
299
            if( $v1==$this->cont[$i] ) {
300
                $v1 *= $pert;
301
                break;
302
            }
303
        }
304
        for($i=0; $i < $n; ++$i) {
305
            if( $v2==$this->cont[$i] ) {
306
                $v2 *= $pert;
307
                break;
308
            }
309
        }
310
        for($i=0; $i < $n; ++$i) {
311
            if( $v3==$this->cont[$i] ) {
312
                $v3 *= $pert;
313
                break;
314
            }
315
        }
316
        for($i=0; $i < $n; ++$i) {
317
            if( $v4==$this->cont[$i] ) {
318
                $v4 *= $pert;
319
                break;
320
            }
321
        }
322
    }
323
 
324
    function interp2($x1,$y1,$x2,$y2,$v1,$v2) {
325
        $cv = $this->GetContVal(min($v1,$v2));
326
        $alpha = ($v1-$cv)/($v1-$v2);
327
        $x1p = $x1*(1-$alpha) + $x2*$alpha;
328
        $y1p = $y1*(1-$alpha) + $y2*$alpha;
329
        $v1p = $v1 + $alpha*($v2-$v1);
330
        return array($x1p,$y1p,$v1p);
331
    }
332
 
333
    function RectFill($v1,$v2,$v3,$v4,$x1,$y1,$x2,$y2,$x3,$y3,$x4,$y4,$depth) {
334
         if( $depth >= self::$maxdepth ) {
335
            // Abort and just appoximate the color of this area
336
            // with the average of the three values
337
            $color = $this->GetColor(($v1+$v2+$v3+$v4)/4);
338
            $p = array($x1, $y1, $x2, $y2, $x3, $y3, $x4, $y4, $x1, $y1);
339
            $this->FillPolygon($color,$p) ;
340
        }
341
        else {
342
 
343
            $this->Pertubate($v1,$v2,$v3,$v4);
344
 
345
            $fcnt = 0 ;
346
            $vv1 = $this->GetNextHigherContourIdx($v1);
347
            $vv2 = $this->GetNextHigherContourIdx($v2);
348
            $vv3 = $this->GetNextHigherContourIdx($v3);
349
            $vv4 = $this->GetNextHigherContourIdx($v4);
350
            $eps = 0.0001;
351
 
352
           if( $vv1 == $vv2 && $vv2 == $vv3 && $vv3 == $vv4 ) {
353
                $color = $this->GetColor($v1);
354
                $p = array($x1, $y1, $x2, $y2, $x3, $y3, $x4, $y4, $x1, $y1);
355
                $this->FillPolygon($color,$p) ;
356
            }
357
            else {
358
 
359
                $dv1 = abs($vv1-$vv2);
360
                $dv2 = abs($vv2-$vv3);
361
                $dv3 = abs($vv3-$vv4);
362
                $dv4 = abs($vv1-$vv4);
363
 
364
                if( $dv1 == 1 ) {
365
                    list($x1p,$y1p,$v1p) = $this->interp2($x1,$y1,$x2,$y2,$v1,$v2);
366
                    $fcnt++;
367
                }
368
 
369
                if( $dv2 == 1 ) {
370
                    list($x2p,$y2p,$v2p) = $this->interp2($x2,$y2,$x3,$y3,$v2,$v3);
371
                    $fcnt++;
372
                }
373
 
374
                if( $dv3 == 1 ) {
375
                    list($x3p,$y3p,$v3p) = $this->interp2($x3,$y3,$x4,$y4,$v3,$v4);
376
                    $fcnt++;
377
                }
378
 
379
                if( $dv4 == 1 ) {
380
                    list($x4p,$y4p,$v4p) = $this->interp2($x4,$y4,$x1,$y1,$v4,$v1);
381
                    $fcnt++;
382
                }
383
 
384
                $totdv = $dv1 + $dv2 + $dv3 + $dv4 ;
385
 
386
                if( ($fcnt == 2 && $totdv==2) || ($fcnt == 4 && $totdv==4) ) {
387
 
388
                    if( $fcnt == 2 && $totdv==2 ) {
389
 
390
                        if( $dv1 == 1 && $dv2 == 1) {
391
                            $color1 = $this->GetColor($v2);
392
                            $p1 = array($x1p,$y1p,$x2,$y2,$x2p,$y2p,$x1p,$y1p);
393
                            $color2 = $this->GetColor($v4);
394
                            $p2 = array($x1,$y1,$x1p,$y1p,$x2p,$y2p,$x3,$y3,$x4,$y4,$x1,$y1);
395
 
396
                            $color = $this->GetColor($v1p);
397
                            $p = array($x1p,$y1p,$x2p,$y2p);
398
                            $v = $v1p;
399
                        }
400
                        elseif( $dv1 == 1 && $dv3 == 1 ) {
401
                            $color1 = $this->GetColor($v2);
402
                            $p1 = array($x1p,$y1p,$x2,$y2,$x3,$y3,$x3p,$y3p,$x1p,$y1p);
403
                            $color2 = $this->GetColor($v4);
404
                            $p2 = array($x1,$y1,$x1p,$y1p,$x3p,$y3p,$x4,$y4,$x1,$y1);
405
 
406
                            $color = $this->GetColor($v1p);
407
                            $p = array($x1p,$y1p,$x3p,$y3p);
408
                            $v = $v1p;
409
                        }
410
                        elseif( $dv1 == 1 && $dv4 == 1 ) {
411
                            $color1 = $this->GetColor($v1);
412
                            $p1 = array($x1,$y1,$x1p,$y1p,$x4p,$y4p,$x1,$y1);
413
                            $color2 = $this->GetColor($v3);
414
                            $p2 = array($x1p,$y1p,$x2,$y2,$x3,$y3,$x4,$y4,$x4p,$y4p,$x1p,$y1p);
415
 
416
                            $color = $this->GetColor($v1p);
417
                            $p = array($x1p,$y1p,$x4p,$y4p);
418
                            $v = $v1p;
419
                        }
420
                        elseif( $dv2 == 1 && $dv4 == 1 ) {
421
                            $color1 = $this->GetColor($v1);
422
                            $p1 = array($x1,$y1,$x2,$y2,$x2p,$y2p,$x4p,$y4p,$x1,$y1);
423
                            $color2 = $this->GetColor($v3);
424
                            $p2 = array($x4p,$y4p,$x2p,$y2p,$x3,$y3,$x4,$y4,$x4p,$y4p);
425
 
426
                            $color = $this->GetColor($v2p);
427
                            $p = array($x2p,$y2p,$x4p,$y4p);
428
                            $v = $v2p;
429
                        }
430
                        elseif( $dv2 == 1 && $dv3 == 1 ) {
431
                            $color1 = $this->GetColor($v1);
432
                            $p1 = array($x1,$y1,$x2,$y2,$x2p,$y2p,$x3p,$y3p,$x4,$y4,$x1,$y1);
433
                            $color2 = $this->GetColor($v3);
434
                            $p2 = array($x2p,$y2p,$x3,$y3,$x3p,$y3p,$x2p,$y2p);
435
 
436
                            $color = $this->GetColor($v2p);
437
                            $p = array($x2p,$y2p,$x3p,$y3p);
438
                            $v = $v2p;
439
                        }
440
                        elseif( $dv3 == 1 && $dv4 == 1 ) {
441
                            $color1 = $this->GetColor($v1);
442
                            $p1 = array($x1,$y1,$x2,$y2,$x3,$y3,$x3p,$y3p,$x4p,$y4p,$x1,$y1);
443
                            $color2 = $this->GetColor($v4);
444
                            $p2 = array($x4p,$y4p,$x3p,$y3p,$x4,$y4,$x4p,$y4p);
445
 
446
                            $color = $this->GetColor($v4p);
447
                            $p = array($x4p,$y4p,$x3p,$y3p);
448
                            $v = $v4p;
449
                        }
450
 
451
                        $this->FillPolygon($color1,$p1);
452
                        $this->FillPolygon($color2,$p2);
453
 
454
                        if( $this->showcontlines ) {
455
                            if( $this->dofill ) {
456
                                $this->shape->SetColor($this->contlinecolor);
457
                            }
458
                            else {
459
                                $this->shape->SetColor($color);
460
                            }
461
                            $this->shape->Line($p[0],$p[1],$p[2],$p[3]);
462
                        }
463
                        if( $this->showlabels ) {
464
                            $this->putLabel( ($p[0]+$p[2])/2, ($p[1]+$p[3])/2, $p[2],$p[3] , $v);
465
                        }
466
                    }
467
                    elseif( $fcnt == 4 && $totdv==4 ) {
468
                        $vc = ($v1+$v2+$v3+$v4)/4;
469
 
470
                        if( $v1p == $v2p && $v2p == $v3p && $v3p == $v4p ) {
471
                            // Four edge crossings (saddle point) of the same contour
472
                            // so we first need to
473
                            // find out how the saddle is crossing "/" or "\"
474
 
475
                            if( $this->GetNextHigherContourIdx($vc) == $this->GetNextHigherContourIdx($v1) ) {
476
                                // "\"
477
                                $color1 = $this->GetColor($v1);
478
                                $p1 = array($x1,$y1,$x1p,$y1p,$x4p,$y4p,$x1,$y1);
479
 
480
                                $color2 = $this->GetColor($v2);
481
                                $p2 = array($x1p,$y1p,$x2,$y2,$x2p,$y2p,$x3p,$y3p,$x4,$y4,$x4p,$y4p,$x1p,$y1p);
482
 
483
                                $color3 = $color1;
484
                                $p3 = array($x2p,$y2p,$x3,$y3,$x3p,$y3p,$x2p,$y2p);
485
 
486
                                $colorl1 = $this->GetColor($v1p);
487
                                $pl1 = array($x1p,$y1p,$x4p,$y4p);
488
                                $colorl2 = $this->GetColor($v2p);
489
                                $pl2 = array($x2p,$y2p,$x3p,$y3p);
490
                                $vl1 = $v1p; $vl2 = $v2p;
491
 
492
                            }
493
                            else {
494
                                // "/"
495
                                $color1 = $this->GetColor($v2);
496
                                $p1 = array($x1p,$y1p,$x2,$y2,$x2p,$y2p,$x1p,$y1p);
497
 
498
                                $color2 = $this->GetColor($v3);
499
                                $p2 = array($x1p,$y1p,$x2p,$y2p,$x3,$y3,$x3p,$y3p,$x4p,$y4p,$x1,$y1,$x1p,$y1p);
500
 
501
                                $color3 = $color1;
502
                                $p3 = array($x4p,$y4p,$x3p,$y3p,$x4,$y4,$x4p,$y4p);
503
 
504
                                $colorl1 = $this->GetColor($v1p);
505
                                $pl1 = array($x1p,$y1p,$x2p,$y2p);
506
                                $colorl2 = $this->GetColor($v4p);
507
                                $pl2 = array($x4p,$y4p,$x3p,$y3p);
508
                                $vl1 = $v1p; $vl2 = $v4p;
509
                            }
510
                        }
511
                        else {
512
                            // There are two different contours crossing so we need to find
513
                            // out which belongs to which
514
                            if( $v1p == $v2p ) {
515
                                // "/"
516
                                $color1 = $this->GetColor($v2);
517
                                $p1 = array($x1p,$y1p,$x2,$y2,$x2p,$y2p,$x1p,$y1p);
518
 
519
                                $color2 = $this->GetColor($v3);
520
                                $p2 = array($x1p,$y1p,$x2p,$y2p,$x3,$y3,$x3p,$y3p,$x4p,$y4p,$x1,$y1,$x1p,$y1p);
521
 
522
                                $color3 = $this->GetColor($v4);
523
                                $p3 = array($x4p,$y4p,$x3p,$y3p,$x4,$y4,$x4p,$y4p);
524
 
525
                                $colorl1 = $this->GetColor($v1p);
526
                                $pl1 = array($x1p,$y1p,$x2p,$y2p);
527
                                $colorl2 = $this->GetColor($v4p);
528
                                $pl2 = array($x4p,$y4p,$x3p,$y3p);
529
                                $vl1 = $v1p; $vl2 = $v4p;
530
                            }
531
                            else { //( $v1p == $v4p )
532
                                // "\"
533
                                $color1 = $this->GetColor($v1);
534
                                $p1 = array($x1,$y1,$x1p,$y1p,$x4p,$y4p,$x1,$y1);
535
 
536
                                $color2 = $this->GetColor($v2);
537
                                $p2 = array($x1p,$y1p,$x2,$y2,$x2p,$y2p,$x3p,$y3p,$x4,$y4,$x4p,$y4p,$x1p,$y1p);
538
 
539
                                $color3 = $this->GetColor($v3);
540
                                $p3 = array($x2p,$y2p,$x3,$y3,$x3p,$y3p,$x2p,$y2p);
541
 
542
                                $colorl1 = $this->GetColor($v1p);
543
                                $pl1 = array($x1p,$y1p,$x4p,$y4p);
544
                                $colorl2 = $this->GetColor($v2p);
545
                                $pl2 = array($x2p,$y2p,$x3p,$y3p);
546
                                $vl1 = $v1p; $vl2 = $v2p;
547
                            }
548
                        }
549
                        $this->FillPolygon($color1,$p1);
550
                        $this->FillPolygon($color2,$p2);
551
                        $this->FillPolygon($color3,$p3);
552
 
553
                        if( $this->showcontlines ) {
554
                            if( $this->dofill ) {
555
                                $this->shape->SetColor($this->contlinecolor);
556
                                $this->shape->Line($pl1[0],$pl1[1],$pl1[2],$pl1[3]);
557
                                $this->shape->Line($pl2[0],$pl2[1],$pl2[2],$pl2[3]);
558
                            }
559
                            else {
560
                                $this->shape->SetColor($colorl1);
561
                                $this->shape->Line($pl1[0],$pl1[1],$pl1[2],$pl1[3]);
562
                                $this->shape->SetColor($colorl2);
563
                                $this->shape->Line($pl2[0],$pl2[1],$pl2[2],$pl2[3]);
564
                            }
565
                        }
566
                        if( $this->showlabels ) {
567
                            $this->putLabel( ($pl1[0]+$pl1[2])/2, ($pl1[1]+$pl1[3])/2, $pl1[2], $pl1[3], $vl1);
568
                            $this->putLabel( ($pl2[0]+$pl2[2])/2, ($pl2[1]+$pl2[3])/2, $pl2[2], $pl2[3],$vl2);
569
                        }
570
                    }
571
                }
572
                else {
573
                    $vc = ($v1+$v2+$v3+$v4)/4;
574
                    $xc = ($x1+$x4)/2;
575
                    $yc = ($y1+$y2)/2;
576
 
577
                    // Top left
578
                    $this->RectFill(($v1+$v2)/2, $v2, ($v2+$v3)/2, $vc,
579
                                    $x1,$yc, $x2,$y2, $xc,$y2, $xc,$yc, $depth+1);
580
                    // Top right
581
                    $this->RectFill($vc, ($v2+$v3)/2, $v3, ($v3+$v4)/2,
582
                                    $xc,$yc, $xc,$y2, $x3,$y3, $x3,$yc, $depth+1);
583
 
584
                    // Bottom left
585
                    $this->RectFill($v1, ($v1+$v2)/2, $vc, ($v1+$v4)/2,
586
                                    $x1,$y1, $x1,$yc, $xc,$yc, $xc,$y4, $depth+1);
587
 
588
                    // Bottom right
589
                    $this->RectFill(($v1+$v4)/2, $vc, ($v3+$v4)/2, $v4,
590
                                    $xc,$y1, $xc,$yc, $x3,$yc, $x4,$y4, $depth+1);
591
 
592
                }
593
            }
594
        }
595
    }
596
 
597
    function TriFill($v1,$v2,$v3,$x1,$y1,$x2,$y2,$x3,$y3,$depth) {
598
        if( $depth >= self::$maxdepth ) {
599
            // Abort and just appoximate the color of this area
600
            // with the average of the three values
601
            $color = $this->GetColor(($v1+$v2+$v3)/3);
602
            $p = array($x1, $y1, $x2, $y2, $x3, $y3, $x1, $y1);
603
            $this->FillPolygon($color,$p) ;
604
        }
605
        else {
606
            // In order to avoid some real unpleasentness in case a vertice is exactly
607
            // the same value as a contour we pertuberate them so that we do not end up
608
            // in udefined situation. This will only affect the calculations and not the
609
            // visual appearance
610
 
611
            $dummy=0;
612
            $this->Pertubate($v1,$v2,$v3,$dummy);
613
 
614
            $fcnt = 0 ;
615
            $vv1 = $this->GetNextHigherContourIdx($v1);
616
            $vv2 = $this->GetNextHigherContourIdx($v2);
617
            $vv3 = $this->GetNextHigherContourIdx($v3);
618
            $eps = 0.0001;
619
 
620
            if( $vv1 == $vv2 && $vv2 == $vv3 ) {
621
                $color = $this->GetColor($v1);
622
                $p = array($x1, $y1, $x2, $y2, $x3, $y3, $x1, $y1);
623
                $this->FillPolygon($color,$p) ;
624
            }
625
            else {
626
                $dv1 = abs($vv1-$vv2);
627
                $dv2 = abs($vv2-$vv3);
628
                $dv3 = abs($vv1-$vv3);
629
 
630
                if( $dv1 == 1 ) {
631
                    list($x1p,$y1p,$v1p) = $this->interp2($x1,$y1,$x2,$y2,$v1,$v2);
632
                    $fcnt++;
633
                }
634
                else {
635
                    $x1p = ($x1+$x2)/2;
636
                    $y1p = ($y1+$y2)/2;
637
                    $v1p = ($v1+$v2)/2;
638
                }
639
 
640
                if( $dv2 == 1 ) {
641
                    list($x2p,$y2p,$v2p) = $this->interp2($x2,$y2,$x3,$y3,$v2,$v3);
642
                    $fcnt++;
643
                }
644
                else {
645
                    $x2p = ($x2+$x3)/2;
646
                    $y2p = ($y2+$y3)/2;
647
                    $v2p = ($v2+$v3)/2;
648
                }
649
 
650
                if( $dv3 == 1 ) {
651
                    list($x3p,$y3p,$v3p) = $this->interp2($x3,$y3,$x1,$y1,$v3,$v1);
652
                    $fcnt++;
653
                }
654
                else {
655
                    $x3p = ($x3+$x1)/2;
656
                    $y3p = ($y3+$y1)/2;
657
                    $v3p = ($v3+$v1)/2;
658
                }
659
 
660
                if( $fcnt == 2 &&
661
                    ((abs($v1p-$v2p) < $eps && $dv1 ==1 && $dv2==1 ) ||
662
                    (abs($v1p-$v3p) < $eps && $dv1 ==1 && $dv3==1 ) ||
663
                    (abs($v2p-$v3p) < $eps && $dv2 ==1 && $dv3==1 )) ) {
664
 
665
                    // This means that the contour line crosses exactly two sides
666
                    // and that the values of each vertice is such that only this
667
                    // contour line will cross this section.
668
                    // We can now be smart. The cotour line will simply divide the
669
                    // area in two polygons that we can fill and then return. There is no
670
                    // need to recurse.
671
 
672
                    // First find out which two sides the contour is crossing
673
                    if( abs($v1p-$v2p) < $eps ) {
674
                        $p4 = array($x1,$y1,$x1p,$y1p,$x2p,$y2p,$x3,$y3,$x1,$y1);
675
                        $color4 = $this->GetColor($v1);
676
 
677
                        $p3 = array($x1p,$y1p,$x2,$y2,$x2p,$y2p,$x1p,$y1p);
678
                        $color3 = $this->GetColor($v2);
679
 
680
                        $p = array($x1p,$y1p,$x2p,$y2p);
681
                        $color = $this->GetColor($v1p);
682
                        $v = $v1p;
683
                    }
684
                    elseif( abs($v1p-$v3p) < $eps ) {
685
                        $p4 = array($x1p,$y1p,$x2,$y2,$x3,$y3,$x3p,$y3p,$x1p,$y1p);
686
                        $color4 = $this->GetColor($v2);
687
 
688
                        $p3 = array($x1,$y1,$x1p,$y1p,$x3p,$y3p,$x1,$y1);
689
                        $color3 = $this->GetColor($v1);
690
 
691
                        $p = array($x1p,$y1p,$x3p,$y3p);
692
                        $color = $this->GetColor($v1p);
693
                        $v = $v1p;
694
                    }
695
                    else {
696
                        $p4 = array($x1,$y1,$x2,$y2,$x2p,$y2p,$x3p,$y3p,$x1,$y1);
697
                        $color4 = $this->GetColor($v2);
698
 
699
                        $p3 = array($x3p,$y3p,$x2p,$y2p,$x3,$y3,$x3p,$y3p);
700
                        $color3 = $this->GetColor($v3);
701
 
702
                        $p = array($x3p,$y3p,$x2p,$y2p);
703
                        $color = $this->GetColor($v3p);
704
                        $v = $v3p;
705
                    }
706
                    $this->FillPolygon($color4,$p4);
707
                    $this->FillPolygon($color3,$p3);
708
 
709
                    if( $this->showcontlines ) {
710
                        if( $this->dofill ) {
711
                            $this->shape->SetColor($this->contlinecolor);
712
                        }
713
                        else {
714
                            $this->shape->SetColor($color);
715
                        }
716
                        $this->shape->Line($p[0],$p[1],$p[2],$p[3]);
717
                    }
718
                    if( $this->showlabels ) {
719
                        $this->putLabel( ($p[0]+$p[2])/2, ($p[1]+$p[3])/2, $p[2], $p[3], $v);
720
                    }
721
                }
722
                else {
723
                    $this->TriFill($v1, $v1p, $v3p, $x1, $y1, $x1p, $y1p, $x3p, $y3p, $depth+1);
724
                    $this->TriFill($v1p, $v2, $v2p, $x1p, $y1p, $x2, $y2, $x2p, $y2p, $depth+1);
725
                    $this->TriFill($v3p, $v1p, $v2p, $x3p, $y3p, $x1p, $y1p, $x2p, $y2p, $depth+1);
726
                    $this->TriFill($v3p, $v2p, $v3, $x3p, $y3p, $x2p, $y2p, $x3, $y3, $depth+1);
727
                }
728
            }
729
        }
730
    }
731
 
732
    function Fill($v1,$v2,$v3,$maxdepth) {
733
        $x1=0; $y1=1;
734
        $x2=1; $y2=0;
735
        $x3=1; $y3=1;
736
        self::$maxdepth = $maxdepth;
737
        $this->TriFill($v1, $v2, $v3, $x1, $y1, $x2, $y2, $x3, $y3, 0);
738
    }
739
 
740
    function Fillmesh($meshdata,$maxdepth,$method='tri') {
741
        $nx = count($meshdata[0]);
742
        $ny = count($meshdata);
743
        self::$maxdepth = $maxdepth;
744
        for( $x=0; $x < $nx-1; ++$x ) {
745
            for( $y=0; $y < $ny-1; ++$y ) {
746
                $v1 = $meshdata[$y][$x];
747
                $v2 = $meshdata[$y][$x+1];
748
                $v3 = $meshdata[$y+1][$x+1];
749
                $v4 = $meshdata[$y+1][$x];
750
 
751
                if( $method == 'tri' ) {
752
                    // Fill upper and lower triangle
753
                    $this->TriFill($v4, $v1, $v2, $x, $y+1, $x, $y, $x+1, $y, 0);
754
                    $this->TriFill($v4, $v2, $v3, $x, $y+1, $x+1, $y, $x+1, $y+1, 0);
755
                }
756
                else {
757
                    $this->RectFill($v4, $v1, $v2, $v3, $x, $y+1, $x, $y, $x+1, $y, $x+1, $y+1, 0);
758
                }
759
            }
760
        }
761
        if( $this->showlabels ) {
762
            $this->strokeLabels();
763
        }
764
    }
765
}
766
 
767
$meshdata = array(
768
    array (12,12,10,10),
769
    array (10,10,8,14),
770
    array (7,7,13,17),
771
    array (4,5,8,12),
772
    array (10,8,7,8));
773
 
774
$tt = new SingleTestTriangle(400,400,count($meshdata[0])-1,count($meshdata)-1);
775
$tt->SetContours(array(4.7, 6.0, 7.2, 8.6, 9.9, 11.2, 12.5, 13.8, 15.1, 16.4));
776
$tt->SetFilled(true);
777
 
778
//$tt->ShowTriangulation(true);
779
$tt->ShowLines(true);
780
 
781
//$tt->ShowLabels(true);
782
$tt->Fillmesh($meshdata, 8, 'rect');
783
 
784
//$tt->Fill(4.0,3.0,7.0, 4);
785
//$tt->Fill(7,4,1,5);
786
//$tt->Fill(1,7,4,5);
787
 
788
$tt->Stroke();
789
 
790
?>