77 |
aurelien |
1 |
<?php
|
|
|
2 |
require_once '../jpgraph.php';
|
|
|
3 |
require_once '../jpgraph_canvas.php';
|
|
|
4 |
require_once '../jpgraph_canvtools.php';
|
|
|
5 |
/*
|
|
|
6 |
* To change this template, choose Tools | Templates
|
|
|
7 |
* and open the template in the editor.
|
|
|
8 |
*/
|
|
|
9 |
|
|
|
10 |
/**
|
|
|
11 |
* Description of test_findpolygon
|
|
|
12 |
*
|
|
|
13 |
* @author ljp
|
|
|
14 |
*/
|
|
|
15 |
class Findpolygon {
|
|
|
16 |
private $nbrContours=-1;
|
|
|
17 |
public $contourCoord=array();
|
|
|
18 |
private $scale = array(0,6,0,8);
|
|
|
19 |
|
|
|
20 |
function flattenEdges($p) {
|
|
|
21 |
$fp=array();
|
|
|
22 |
for ($i = 0 ; $i < count($p) ; $i++) {
|
|
|
23 |
$fp[] = $p[$i][0];
|
|
|
24 |
$fp[] = $p[$i][1];
|
|
|
25 |
}
|
|
|
26 |
return $fp;
|
|
|
27 |
}
|
|
|
28 |
|
|
|
29 |
function SetupTestData() {
|
|
|
30 |
// for($i=0; $i<count($this->contourCoord[0]); ++$i) {
|
|
|
31 |
// echo '('.$this->contourCoord[0][$i][0][0].','.$this->contourCoord[0][$i][0][1].') -> '.
|
|
|
32 |
// '('.$this->contourCoord[0][$i][1][0].','.$this->contourCoord[0][$i][1][1].")\n";
|
|
|
33 |
// }
|
|
|
34 |
//
|
|
|
35 |
|
|
|
36 |
$c=0;
|
|
|
37 |
$p[$c] = array(0.6,1, 1,0.5, 2,0.5, 3,0.5, 3.5,1, 3.5,2, 3,2.5, 2,2.5, 1,2.5, 0.5,2, 0.6,1);
|
|
|
38 |
$c++;
|
|
|
39 |
$p[$c] = array(6,0.5, 5.5,1, 5.5,2, 6,2.5);
|
|
|
40 |
|
|
|
41 |
$this->nbrContours = $c+1;
|
|
|
42 |
|
|
|
43 |
for ($c = 0 ; $c < count($p) ; $c++) {
|
|
|
44 |
$n=count($p[$c]);
|
|
|
45 |
|
|
|
46 |
$this->contourCoord[$c][0] = array(array($p[$c][0],$p[$c][1]),array($p[$c][2],$p[$c][3]));
|
|
|
47 |
$k=1;
|
|
|
48 |
for ($i = 0; $i < ($n-4)/2; $i++, $k++) {
|
|
|
49 |
$this->contourCoord[$c][$k] = array($this->contourCoord[$c][$k-1][1], array($p[$c][2*$k+2],$p[$c][2*$k+1+2]));
|
|
|
50 |
}
|
|
|
51 |
|
|
|
52 |
// Swap edges order at random
|
|
|
53 |
$n = count($this->contourCoord[$c]);
|
|
|
54 |
for($i=0; $i < floor($n/2); ++$i) {
|
|
|
55 |
$swap1 = rand(0,$n-1);
|
|
|
56 |
$t = $this->contourCoord[$c][$swap1];
|
|
|
57 |
while( $swap1 == ($swap2 = rand(0,$n-1)) )
|
|
|
58 |
;
|
|
|
59 |
$this->contourCoord[$c][$swap1] = $this->contourCoord[$c][$swap2];
|
|
|
60 |
$this->contourCoord[$c][$swap2] = $t;
|
|
|
61 |
}
|
|
|
62 |
|
|
|
63 |
// Swap vector direction on 1/3 of the edges
|
|
|
64 |
for ($i = 0 ; $i < floor(count($this->contourCoord[$c])/3) ; $i++) {
|
|
|
65 |
$e = rand(0, count($this->contourCoord[$c])-1);
|
|
|
66 |
$edge = $this->contourCoord[$c][$e];
|
|
|
67 |
$v1 = $edge[0]; $v2 = $edge[1];
|
|
|
68 |
$this->contourCoord[$c][$e][0] = $v2;
|
|
|
69 |
$this->contourCoord[$c][$e][1] = $v1;
|
|
|
70 |
}
|
|
|
71 |
}
|
|
|
72 |
|
|
|
73 |
$pp = array();
|
|
|
74 |
for($j=0; $j < count($p); ++$j ) {
|
|
|
75 |
for( $i=0; $i < count($p[$j])/2; ++$i ) {
|
|
|
76 |
$pp[$j][$i] = array($p[$j][2*$i],$p[$j][2*$i+1]);
|
|
|
77 |
}
|
|
|
78 |
}
|
|
|
79 |
return $pp;
|
|
|
80 |
}
|
|
|
81 |
|
|
|
82 |
function p_edges($v) {
|
|
|
83 |
for ($i = 0 ; $i < count($v) ; $i++) {
|
|
|
84 |
echo "(".$v[$i][0][0].",".$v[$i][0][1].") -> (".$v[$i][1][0].",".$v[$i][1][1].")\n";
|
|
|
85 |
}
|
|
|
86 |
echo "\n";
|
|
|
87 |
}
|
|
|
88 |
|
|
|
89 |
function CompareCyclic($a,$b,$forward=true) {
|
|
|
90 |
|
|
|
91 |
// We assume disjoint vertices and if last==first this just means
|
|
|
92 |
// that the polygon is closed. For this comparison it must be unique
|
|
|
93 |
// elements
|
|
|
94 |
if( $a[count($a)-1] == $a[0] ) {
|
|
|
95 |
array_pop($a);
|
|
|
96 |
}
|
|
|
97 |
if( $b[count($b)-1] == $b[0] ) {
|
|
|
98 |
array_pop($b);
|
|
|
99 |
}
|
|
|
100 |
|
|
|
101 |
$n1 = count($a); $n2 = count($b);
|
|
|
102 |
if( $n1 != $n2 )
|
|
|
103 |
return false;
|
|
|
104 |
|
|
|
105 |
$i=0;
|
|
|
106 |
while( ($i < $n2) && ($a[0] != $b[$i]) )
|
|
|
107 |
++$i;
|
|
|
108 |
|
|
|
109 |
if( $i >= $n2 )
|
|
|
110 |
return false;
|
|
|
111 |
|
|
|
112 |
$j=0;
|
|
|
113 |
if( $forward ) {
|
|
|
114 |
while( ($j < $n1) && ($a[$j] == $b[$i]) ) {
|
|
|
115 |
$i = ($i + 1) % $n2;
|
|
|
116 |
++$j;
|
|
|
117 |
}
|
|
|
118 |
}
|
|
|
119 |
else {
|
|
|
120 |
while( ($j < $n1) && ($a[$j] == $b[$i]) ) {
|
|
|
121 |
--$i;
|
|
|
122 |
if( $i < 0 ) {
|
|
|
123 |
$i = $n2-1;
|
|
|
124 |
}
|
|
|
125 |
++$j;
|
|
|
126 |
}
|
|
|
127 |
}
|
|
|
128 |
return $j >= $n1;
|
|
|
129 |
}
|
|
|
130 |
|
|
|
131 |
function dbg($s) {
|
|
|
132 |
// echo $s."\n";
|
|
|
133 |
}
|
|
|
134 |
|
|
|
135 |
function IsVerticeOnBorder($x1,$y1) {
|
|
|
136 |
// Check if the vertice lies on any of the four border
|
|
|
137 |
if( $x1==$this->scale[0] || $x1==$this->scale[1] ) {
|
|
|
138 |
return true;
|
|
|
139 |
}
|
|
|
140 |
if( $y1==$this->scale[2] || $y1==$this->scale[3] ) {
|
|
|
141 |
return true;
|
|
|
142 |
}
|
|
|
143 |
return false;
|
|
|
144 |
}
|
|
|
145 |
|
|
|
146 |
function FindPolygons($debug=false) {
|
|
|
147 |
|
|
|
148 |
$pol = 0;
|
|
|
149 |
for ($c = 0; $c < $this->nbrContours; $c++) {
|
|
|
150 |
|
|
|
151 |
$this->dbg("\n** Searching polygon chain $c ... ");
|
|
|
152 |
$this->dbg("------------------------------------------\n");
|
|
|
153 |
|
|
|
154 |
$edges = $this->contourCoord[$c];
|
|
|
155 |
while( count($edges) > 0 ) {
|
|
|
156 |
|
|
|
157 |
$edge = array_shift($edges);
|
|
|
158 |
list($x1,$y1) = $edge[0];
|
|
|
159 |
list($x2,$y2) = $edge[1];
|
|
|
160 |
$polygons[$pol]=array(
|
|
|
161 |
array($x1,$y1),array($x2,$y2)
|
|
|
162 |
);
|
|
|
163 |
|
|
|
164 |
$this->dbg("Searching on second vertice.");
|
|
|
165 |
|
|
|
166 |
$found=false;
|
|
|
167 |
if( ! $this->IsVerticeOnBorder($x2,$y2) ) {
|
|
|
168 |
do {
|
|
|
169 |
|
|
|
170 |
$this->dbg(" --Searching on edge: ($x1,$y1)->($x2,$y2)");
|
|
|
171 |
|
|
|
172 |
$found=false;
|
|
|
173 |
$nn = count($edges);
|
|
|
174 |
for( $i=0; $i < $nn && !$found; ++$i ) {
|
|
|
175 |
$edge = $edges[$i];
|
|
|
176 |
if( $found = ($x2==$edge[0][0] && $y2==$edge[0][1]) ) {
|
|
|
177 |
$polygons[$pol][] = array($edge[1][0],$edge[1][1]);
|
|
|
178 |
$x1 = $x2; $y1 = $y2;
|
|
|
179 |
$x2 = $edge[1][0]; $y2 = $edge[1][1];
|
|
|
180 |
}
|
|
|
181 |
elseif( $found = ($x2==$edge[1][0] && $y2==$edge[1][1]) ) {
|
|
|
182 |
$polygons[$pol][] = array($edge[0][0],$edge[0][1]);
|
|
|
183 |
$x1 = $x2; $y1 = $y2;
|
|
|
184 |
$x2 = $edge[0][0]; $y2 = $edge[0][1];
|
|
|
185 |
}
|
|
|
186 |
if( $found ) {
|
|
|
187 |
$this->dbg(" --Found next edge: [i=$i], (%,%) -> ($x2,$y2)");
|
|
|
188 |
unset($edges[$i]);
|
|
|
189 |
$edges = array_values($edges);
|
|
|
190 |
}
|
|
|
191 |
}
|
|
|
192 |
|
|
|
193 |
} while( $found );
|
|
|
194 |
}
|
|
|
195 |
|
|
|
196 |
if( !$found && count($edges)>0 ) {
|
|
|
197 |
$this->dbg("Searching on first vertice.");
|
|
|
198 |
list($x1,$y1) = $polygons[$pol][0];
|
|
|
199 |
list($x2,$y2) = $polygons[$pol][1];
|
|
|
200 |
|
|
|
201 |
if( ! $this->IsVerticeOnBorder($x1,$y1) ) {
|
|
|
202 |
do {
|
|
|
203 |
|
|
|
204 |
$this->dbg(" --Searching on edge: ($x1,$y1)->($x2,$y2)");
|
|
|
205 |
|
|
|
206 |
$found=false;
|
|
|
207 |
$nn = count($edges);
|
|
|
208 |
for( $i=0; $i < $nn && !$found; ++$i ) {
|
|
|
209 |
$edge = $edges[$i];
|
|
|
210 |
if( $found = ($x1==$edge[0][0] && $y1==$edge[0][1]) ) {
|
|
|
211 |
array_unshift($polygons[$pol],array($edge[1][0],$edge[1][1]));
|
|
|
212 |
$x2 = $x1; $y2 = $y1;
|
|
|
213 |
$x1 = $edge[1][0]; $y1 = $edge[1][1];
|
|
|
214 |
}
|
|
|
215 |
elseif( $found = ($x1==$edge[1][0] && $y1==$edge[1][1]) ) {
|
|
|
216 |
array_unshift($polygons[$pol],array($edge[0][0],$edge[0][1]));
|
|
|
217 |
$x2 = $x1; $y2 = $y1;
|
|
|
218 |
$x1 = $edge[0][0]; $y1 = $edge[0][1];
|
|
|
219 |
}
|
|
|
220 |
if( $found ) {
|
|
|
221 |
$this->dbg(" --Found next edge: [i=$i], ($x1,$y1) -> (%,%)");
|
|
|
222 |
unset($edges[$i]);
|
|
|
223 |
$edges = array_values($edges);
|
|
|
224 |
}
|
|
|
225 |
}
|
|
|
226 |
|
|
|
227 |
} while( $found );
|
|
|
228 |
}
|
|
|
229 |
|
|
|
230 |
}
|
|
|
231 |
|
|
|
232 |
$pol++;
|
|
|
233 |
}
|
|
|
234 |
}
|
|
|
235 |
|
|
|
236 |
return $polygons;
|
|
|
237 |
}
|
|
|
238 |
|
|
|
239 |
}
|
|
|
240 |
define('HORIZ_EDGE',0);
|
|
|
241 |
define('VERT_EDGE',1);
|
|
|
242 |
|
|
|
243 |
class FillGridRect {
|
|
|
244 |
private $edges,$dataPoints,$colors,$isoBars;
|
|
|
245 |
private $invert=false;
|
|
|
246 |
|
|
|
247 |
function __construct(&$edges,&$dataPoints,$isoBars,$colors) {
|
|
|
248 |
$this->edges = $edges;
|
|
|
249 |
$this->dataPoints = $dataPoints;
|
|
|
250 |
$this->colors = $colors;
|
|
|
251 |
$this->isoBars = $isoBars;
|
|
|
252 |
}
|
|
|
253 |
|
|
|
254 |
function GetIsobarColor($val) {
|
|
|
255 |
for ($i = 0 ; $i < count($this->isoBars) ; $i++) {
|
|
|
256 |
if( $val <= $this->isoBars[$i] ) {
|
|
|
257 |
return $this->colors[$i];
|
|
|
258 |
}
|
|
|
259 |
}
|
|
|
260 |
return $this->colors[$i]; // The color for all values above the highest isobar
|
|
|
261 |
}
|
|
|
262 |
|
|
|
263 |
function GetIsobarVal($a,$b) {
|
|
|
264 |
// Get the isobar that is between the values a and b
|
|
|
265 |
// If there are more isobars then return the one with lowest index
|
|
|
266 |
if( $b < $a ) {
|
|
|
267 |
$t=$a; $a=$b; $b=$t;
|
|
|
268 |
}
|
|
|
269 |
$i = 0 ;
|
|
|
270 |
$n = count($this->isoBars);
|
|
|
271 |
while( $i < $n && $this->isoBars[$i] < $a ) {
|
|
|
272 |
++$i;
|
|
|
273 |
}
|
|
|
274 |
if( $i >= $n )
|
|
|
275 |
die("Internal error. Cannot find isobar values for ($a,$b)");
|
|
|
276 |
return $this->isoBars[$i];
|
|
|
277 |
}
|
|
|
278 |
|
|
|
279 |
function getCrossingCoord($aRow,$aCol,$aEdgeDir,$aIsobarVal) {
|
|
|
280 |
// In order to avoid numerical problem when two vertices are very close
|
|
|
281 |
// we have to check and avoid dividing by close to zero denumerator.
|
|
|
282 |
if( $aEdgeDir == HORIZ_EDGE ) {
|
|
|
283 |
$d = abs($this->dataPoints[$aRow][$aCol] - $this->dataPoints[$aRow][$aCol+1]);
|
|
|
284 |
if( $d > 0.001 ) {
|
|
|
285 |
$xcoord = $aCol + abs($aIsobarVal - $this->dataPoints[$aRow][$aCol]) / $d;
|
|
|
286 |
}
|
|
|
287 |
else {
|
|
|
288 |
$xcoord = $aCol;
|
|
|
289 |
}
|
|
|
290 |
$ycoord = $aRow;
|
|
|
291 |
}
|
|
|
292 |
else {
|
|
|
293 |
$d = abs($this->dataPoints[$aRow][$aCol] - $this->dataPoints[$aRow+1][$aCol]);
|
|
|
294 |
if( $d > 0.001 ) {
|
|
|
295 |
$ycoord = $aRow + abs($aIsobarVal - $this->dataPoints[$aRow][$aCol]) / $d;
|
|
|
296 |
}
|
|
|
297 |
else {
|
|
|
298 |
$ycoord = $aRow;
|
|
|
299 |
}
|
|
|
300 |
$xcoord = $aCol;
|
|
|
301 |
}
|
|
|
302 |
if( $this->invert ) {
|
|
|
303 |
$ycoord = $this->nbrRows-1 - $ycoord;
|
|
|
304 |
}
|
|
|
305 |
return array($xcoord,$ycoord);
|
|
|
306 |
}
|
|
|
307 |
|
|
|
308 |
function Fill(ContCanvas $canvas) {
|
|
|
309 |
|
|
|
310 |
$nx_vertices = count($this->dataPoints[0]);
|
|
|
311 |
$ny_vertices = count($this->dataPoints);
|
|
|
312 |
|
|
|
313 |
// Loop through all squares in the grid
|
|
|
314 |
for($col=0; $col < $nx_vertices-1; ++$col) {
|
|
|
315 |
for($row=0; $row < $ny_vertices-1; ++$row) {
|
|
|
316 |
|
|
|
317 |
$n = 0;$quad_edges=array();
|
|
|
318 |
if ( $this->edges[VERT_EDGE][$row][$col] ) $quad_edges[$n++] = array($row, $col, VERT_EDGE);
|
|
|
319 |
if ( $this->edges[VERT_EDGE][$row][$col+1] ) $quad_edges[$n++] = array($row, $col+1,VERT_EDGE);
|
|
|
320 |
if ( $this->edges[HORIZ_EDGE][$row][$col] ) $quad_edges[$n++] = array($row, $col, HORIZ_EDGE);
|
|
|
321 |
if ( $this->edges[HORIZ_EDGE][$row+1][$col] ) $quad_edges[$n++] = array($row+1,$col, HORIZ_EDGE);
|
|
|
322 |
|
|
|
323 |
if( $n == 0 ) {
|
|
|
324 |
// Easy, fill the entire quadrant with one color since we have no crossings
|
|
|
325 |
// Select the top left datapoint as representing this quadrant
|
|
|
326 |
// color for this quadrant
|
|
|
327 |
$color = $this->GetIsobarColor($this->dataPoints[$row][$col]);
|
|
|
328 |
$polygon = array($col,$row,$col,$row+1,$col+1,$row+1,$col+1,$row,$col,$row);
|
|
|
329 |
$canvas->FilledPolygon($polygon,$color);
|
|
|
330 |
|
|
|
331 |
} elseif( $n==2 ) {
|
|
|
332 |
|
|
|
333 |
// There is one isobar edge crossing this quadrant. In order to fill we need to
|
|
|
334 |
// find out the orientation of the two areas this edge is separating in order to
|
|
|
335 |
// construct the two polygons that define the two areas to be filled
|
|
|
336 |
// There are six possible variants
|
|
|
337 |
// 0) North-South
|
|
|
338 |
// 1) West-East
|
|
|
339 |
// 2) West-North
|
|
|
340 |
// 3) East-North
|
|
|
341 |
// 4) West-South
|
|
|
342 |
// 5) East-South
|
|
|
343 |
$type=-1;
|
|
|
344 |
if( $this->edges[HORIZ_EDGE][$row][$col] ) {
|
|
|
345 |
if( $this->edges[HORIZ_EDGE][$row+1][$col] ) $type=0; // North-South
|
|
|
346 |
elseif( $this->edges[VERT_EDGE][$row][$col] ) $type=2;
|
|
|
347 |
elseif( $this->edges[VERT_EDGE][$row][$col+1] ) $type=3;
|
|
|
348 |
}
|
|
|
349 |
elseif( $this->edges[HORIZ_EDGE][$row+1][$col] ) {
|
|
|
350 |
if( $this->edges[VERT_EDGE][$row][$col] ) $type=4;
|
|
|
351 |
elseif( $this->edges[VERT_EDGE][$row][$col+1] ) $type=5;
|
|
|
352 |
}
|
|
|
353 |
else {
|
|
|
354 |
$type=1;
|
|
|
355 |
}
|
|
|
356 |
if( $type==-1 ) {
|
|
|
357 |
die('Internal error: n=2 but no edges in the quadrant was find to determine type.');
|
|
|
358 |
}
|
|
|
359 |
|
|
|
360 |
switch( $type ) {
|
|
|
361 |
case 0: //North-South
|
|
|
362 |
|
|
|
363 |
// North vertice
|
|
|
364 |
$v1 = $this->dataPoints[$row][$col];
|
|
|
365 |
$v2 = $this->dataPoints[$row][$col+1];
|
|
|
366 |
$isobarValue = $this->GetIsobarVal($v1, $v2);
|
|
|
367 |
list($x1,$y1) = $this->getCrossingCoord($row, $col,HORIZ_EDGE, $isobarValue);
|
|
|
368 |
|
|
|
369 |
// South vertice
|
|
|
370 |
$v1 = $this->dataPoints[$row+1][$col];
|
|
|
371 |
$v2 = $this->dataPoints[$row+1][$col+1];
|
|
|
372 |
$isobarValue = $this->GetIsobarVal($v1, $v2);
|
|
|
373 |
list($x2,$y2) = $this->getCrossingCoord($row+1, $col,HORIZ_EDGE, $isobarValue);
|
|
|
374 |
|
|
|
375 |
$polygon = array($col,$row,$x1,$y1,$x2,$y2,$col,$row+1,$col,$row);
|
|
|
376 |
$canvas->FilledPolygon($polygon,$this->GetIsobarColor($v1));
|
|
|
377 |
|
|
|
378 |
$polygon = array($col+1,$row,$x1,$y1,$x2,$y2,$col+1,$row+1,$col+1,$row);
|
|
|
379 |
$canvas->FilledPolygon($polygon,$this->GetIsobarColor($v2));
|
|
|
380 |
|
|
|
381 |
break;
|
|
|
382 |
|
|
|
383 |
case 1: // West-East
|
|
|
384 |
|
|
|
385 |
// West vertice
|
|
|
386 |
$v1 = $this->dataPoints[$row][$col];
|
|
|
387 |
$v2 = $this->dataPoints[$row+1][$col];
|
|
|
388 |
$isobarValue = $this->GetIsobarVal($v1, $v2);
|
|
|
389 |
list($x1,$y1) = $this->getCrossingCoord($row, $col,VERT_EDGE, $isobarValue);
|
|
|
390 |
|
|
|
391 |
// East vertice
|
|
|
392 |
$v1 = $this->dataPoints[$row][$col+1];
|
|
|
393 |
$v2 = $this->dataPoints[$row+1][$col+1];
|
|
|
394 |
$isobarValue = $this->GetIsobarVal($v1, $v2);
|
|
|
395 |
list($x2,$y2) = $this->getCrossingCoord($row, $col+1,VERT_EDGE, $isobarValue);
|
|
|
396 |
|
|
|
397 |
$polygon = array($col,$row,$x1,$y1,$x2,$y2,$col+1,$row,$col,$row);
|
|
|
398 |
$canvas->FilledPolygon($polygon,$this->GetIsobarColor($v1));
|
|
|
399 |
|
|
|
400 |
$polygon = array($col,$row+1,$x1,$y1,$x2,$y2,$col+1,$row+1,$col,$row+1);
|
|
|
401 |
$canvas->FilledPolygon($polygon,$this->GetIsobarColor($v2));
|
|
|
402 |
break;
|
|
|
403 |
|
|
|
404 |
case 2: // West-North
|
|
|
405 |
|
|
|
406 |
// West vertice
|
|
|
407 |
$v1 = $this->dataPoints[$row][$col];
|
|
|
408 |
$v2 = $this->dataPoints[$row+1][$col];
|
|
|
409 |
$isobarValue = $this->GetIsobarVal($v1, $v2);
|
|
|
410 |
list($x1,$y1) = $this->getCrossingCoord($row, $col,VERT_EDGE, $isobarValue);
|
|
|
411 |
|
|
|
412 |
// North vertice
|
|
|
413 |
$v1 = $this->dataPoints[$row][$col];
|
|
|
414 |
$v2 = $this->dataPoints[$row][$col+1];
|
|
|
415 |
$isobarValue = $this->GetIsobarVal($v1, $v2);
|
|
|
416 |
list($x2,$y2) = $this->getCrossingCoord($row, $col,HORIZ_EDGE, $isobarValue);
|
|
|
417 |
|
|
|
418 |
$polygon = array($col,$row,$x1,$y1,$x2,$y2,$col,$row);
|
|
|
419 |
$canvas->FilledPolygon($polygon,$this->GetIsobarColor($v1));
|
|
|
420 |
|
|
|
421 |
$polygon = array($x1,$y1,$x2,$y2,$col+1,$row,$col+1,$row+1,$col,$row+1,$x1,$y1);
|
|
|
422 |
$canvas->FilledPolygon($polygon,$this->GetIsobarColor($v2));
|
|
|
423 |
|
|
|
424 |
break;
|
|
|
425 |
|
|
|
426 |
case 3: // East-North
|
|
|
427 |
|
|
|
428 |
// if( $row==3 && $col==1 && $n==2 ) {
|
|
|
429 |
// echo " ** East-North<br>";
|
|
|
430 |
// }
|
|
|
431 |
|
|
|
432 |
|
|
|
433 |
// East vertice
|
|
|
434 |
$v1 = $this->dataPoints[$row][$col+1];
|
|
|
435 |
$v2 = $this->dataPoints[$row+1][$col+1];
|
|
|
436 |
$isobarValue = $this->GetIsobarVal($v1, $v2);
|
|
|
437 |
list($x1,$y1) = $this->getCrossingCoord($row, $col+1,VERT_EDGE, $isobarValue);
|
|
|
438 |
//
|
|
|
439 |
// if( $row==3 && $col==1 && $n==2 ) {
|
|
|
440 |
// echo " ** E_val($v1,$v2), isobar=$isobarValue<br>";
|
|
|
441 |
// echo " ** E($x1,$y1)<br>";
|
|
|
442 |
// }
|
|
|
443 |
|
|
|
444 |
|
|
|
445 |
// North vertice
|
|
|
446 |
$v1 = $this->dataPoints[$row][$col];
|
|
|
447 |
$v2 = $this->dataPoints[$row][$col+1];
|
|
|
448 |
$isobarValue = $this->GetIsobarVal($v1, $v2);
|
|
|
449 |
list($x2,$y2) = $this->getCrossingCoord($row, $col,HORIZ_EDGE, $isobarValue);
|
|
|
450 |
|
|
|
451 |
// if( $row==3 && $col==1 && $n==2 ) {
|
|
|
452 |
// echo " ** N_val($v1,$v2), isobar=$isobarValue<br>";
|
|
|
453 |
// echo " ** N($x2,$y2)<br>";
|
|
|
454 |
// }
|
|
|
455 |
// if( $row==3 && $col==1 && $n==2 )
|
|
|
456 |
// $canvas->Line($x1,$y1,$x2,$y2,'blue');
|
|
|
457 |
|
|
|
458 |
$polygon = array($x1,$y1,$x2,$y2,$col+1,$row,$x1,$y1);
|
|
|
459 |
$canvas->FilledPolygon($polygon,$this->GetIsobarColor($v2));
|
|
|
460 |
|
|
|
461 |
$polygon = array($col,$row,$x2,$y2,$x1,$y1,$col+1,$row+1,$col,$row+1,$col,$row);
|
|
|
462 |
$canvas->FilledPolygon($polygon,$this->GetIsobarColor($v1));
|
|
|
463 |
|
|
|
464 |
break;
|
|
|
465 |
|
|
|
466 |
case 4: // West-South
|
|
|
467 |
|
|
|
468 |
// West vertice
|
|
|
469 |
$v1 = $this->dataPoints[$row][$col];
|
|
|
470 |
$v2 = $this->dataPoints[$row+1][$col];
|
|
|
471 |
$isobarValue = $this->GetIsobarVal($v1, $v2);
|
|
|
472 |
list($x1,$y1) = $this->getCrossingCoord($row, $col,VERT_EDGE, $isobarValue);
|
|
|
473 |
|
|
|
474 |
// South vertice
|
|
|
475 |
$v1 = $this->dataPoints[$row+1][$col];
|
|
|
476 |
$v2 = $this->dataPoints[$row+1][$col+1];
|
|
|
477 |
$isobarValue = $this->GetIsobarVal($v1, $v2);
|
|
|
478 |
list($x2,$y2) = $this->getCrossingCoord($row+1, $col,HORIZ_EDGE, $isobarValue);
|
|
|
479 |
|
|
|
480 |
$polygon = array($col,$row+1,$x1,$y1,$x2,$y2,$col,$row+1);
|
|
|
481 |
$canvas->FilledPolygon($polygon,$this->GetIsobarColor($v1));
|
|
|
482 |
|
|
|
483 |
$polygon = array($x1,$y1,$x2,$y2,$col+1,$row+1,$col+1,$row,$col,$row,$x1,$y1);
|
|
|
484 |
$canvas->FilledPolygon($polygon,$this->GetIsobarColor($v2));
|
|
|
485 |
|
|
|
486 |
break;
|
|
|
487 |
|
|
|
488 |
case 5: // East-South
|
|
|
489 |
|
|
|
490 |
//
|
|
|
491 |
// if( $row==1 && $col==1 && $n==2 ) {
|
|
|
492 |
// echo " ** Sout-East<br>";
|
|
|
493 |
// }
|
|
|
494 |
|
|
|
495 |
// East vertice
|
|
|
496 |
$v1 = $this->dataPoints[$row][$col+1];
|
|
|
497 |
$v2 = $this->dataPoints[$row+1][$col+1];
|
|
|
498 |
$isobarValue = $this->GetIsobarVal($v1, $v2);
|
|
|
499 |
list($x1,$y1) = $this->getCrossingCoord($row, $col+1,VERT_EDGE, $isobarValue);
|
|
|
500 |
|
|
|
501 |
// if( $row==1 && $col==1 && $n==2 ) {
|
|
|
502 |
// echo " ** E_val($v1,$v2), isobar=$isobarValue<br>";
|
|
|
503 |
// echo " ** E($x1,$y1)<br>";
|
|
|
504 |
// }
|
|
|
505 |
|
|
|
506 |
// South vertice
|
|
|
507 |
$v1 = $this->dataPoints[$row+1][$col];
|
|
|
508 |
$v2 = $this->dataPoints[$row+1][$col+1];
|
|
|
509 |
$isobarValue = $this->GetIsobarVal($v1, $v2);
|
|
|
510 |
list($x2,$y2) = $this->getCrossingCoord($row+1, $col,HORIZ_EDGE, $isobarValue);
|
|
|
511 |
|
|
|
512 |
// if( $row==1 && $col==1 && $n==2 ) {
|
|
|
513 |
// echo " ** S_val($v1,$v2), isobar=$isobarValue<br>";
|
|
|
514 |
// echo " ** S($x2,$y2)<br>";
|
|
|
515 |
// }
|
|
|
516 |
|
|
|
517 |
$polygon = array($col+1,$row+1,$x1,$y1,$x2,$y2,$col+1,$row+1);
|
|
|
518 |
$canvas->FilledPolygon($polygon,$this->GetIsobarColor($v2));
|
|
|
519 |
|
|
|
520 |
$polygon = array($x1,$y1,$x2,$y2,$col,$row+1,$col,$row,$col+1,$row,$x1,$y1);
|
|
|
521 |
$canvas->FilledPolygon($polygon,$this->GetIsobarColor($v1));
|
|
|
522 |
|
|
|
523 |
break;
|
|
|
524 |
|
|
|
525 |
}
|
|
|
526 |
|
|
|
527 |
}
|
|
|
528 |
|
|
|
529 |
}
|
|
|
530 |
}
|
|
|
531 |
|
|
|
532 |
}
|
|
|
533 |
}
|
|
|
534 |
|
|
|
535 |
|
|
|
536 |
class ContCanvas {
|
|
|
537 |
public $g;
|
|
|
538 |
public $shape,$scale;
|
|
|
539 |
function __construct($xmax=6,$ymax=6,$width=400,$height=400) {
|
|
|
540 |
|
|
|
541 |
$this->g = new CanvasGraph($width,$height);
|
|
|
542 |
$this->scale = new CanvasScale($this->g, 0, $xmax, 0, $ymax);
|
|
|
543 |
$this->shape = new Shape($this->g, $this->scale);
|
|
|
544 |
|
|
|
545 |
//$this->g->SetFrame(true);
|
|
|
546 |
$this->g->SetMargin(5,5,5,5);
|
|
|
547 |
$this->g->SetMarginColor('white@1');
|
|
|
548 |
$this->g->InitFrame();
|
|
|
549 |
|
|
|
550 |
|
|
|
551 |
$this->shape->SetColor('gray');
|
|
|
552 |
for( $col=1; $col<$xmax; ++$col ) {
|
|
|
553 |
$this->shape->Line($col, 0, $col, $ymax);
|
|
|
554 |
}
|
|
|
555 |
for( $row=1; $row<$ymax; ++$row ) {
|
|
|
556 |
$this->shape->Line(0, $row, $xmax, $row);
|
|
|
557 |
}
|
|
|
558 |
}
|
|
|
559 |
|
|
|
560 |
function SetDatapoints($datapoints) {
|
|
|
561 |
$ny=count($datapoints);
|
|
|
562 |
$nx=count($datapoints[0]);
|
|
|
563 |
$t = new Text();
|
|
|
564 |
$t->SetFont(FF_ARIAL,FS_NORMAL,8);
|
|
|
565 |
for( $x=0; $x < $nx; ++$x ) {
|
|
|
566 |
for( $y=0; $y < $ny; ++$y ) {
|
|
|
567 |
list($x1,$y1) = $this->scale->Translate($x,$y);
|
|
|
568 |
|
|
|
569 |
if( $datapoints[$y][$x] > 0 )
|
|
|
570 |
$t->SetColor('blue');
|
|
|
571 |
else
|
|
|
572 |
$t->SetColor('black');
|
|
|
573 |
$t->SetFont(FF_ARIAL,FS_BOLD,8);
|
|
|
574 |
$t->Set($datapoints[$y][$x]);
|
|
|
575 |
$t->Stroke($this->g->img,$x1,$y1);
|
|
|
576 |
|
|
|
577 |
$t->SetColor('gray');
|
|
|
578 |
$t->SetFont(FF_ARIAL,FS_NORMAL,8);
|
|
|
579 |
$t->Set("($y,$x)");
|
|
|
580 |
$t->Stroke($this->g->img,$x1+10,$y1);
|
|
|
581 |
|
|
|
582 |
}
|
|
|
583 |
}
|
|
|
584 |
}
|
|
|
585 |
|
|
|
586 |
function DrawLinePolygons($p,$color='red') {
|
|
|
587 |
$this->shape->SetColor($color);
|
|
|
588 |
for ($i = 0 ; $i < count($p) ; $i++) {
|
|
|
589 |
$x1 = $p[$i][0][0]; $y1 = $p[$i][0][1];
|
|
|
590 |
for ($j = 1 ; $j < count($p[$i]) ; $j++) {
|
|
|
591 |
$x2=$p[$i][$j][0]; $y2 = $p[$i][$j][1];
|
|
|
592 |
$this->shape->Line($x1, $y1, $x2, $y2);
|
|
|
593 |
$x1=$x2; $y1=$y2;
|
|
|
594 |
}
|
|
|
595 |
}
|
|
|
596 |
}
|
|
|
597 |
|
|
|
598 |
function Line($x1,$y1,$x2,$y2,$color='red') {
|
|
|
599 |
$this->shape->SetColor($color);
|
|
|
600 |
$this->shape->Line($x1, $y1, $x2, $y2);
|
|
|
601 |
}
|
|
|
602 |
function Polygon($p,$color='blue') {
|
|
|
603 |
$this->shape->SetColor($color);
|
|
|
604 |
$this->shape->Polygon($p);
|
|
|
605 |
}
|
|
|
606 |
|
|
|
607 |
function FilledPolygon($p,$color='lightblue') {
|
|
|
608 |
$this->shape->SetColor($color);
|
|
|
609 |
$this->shape->FilledPolygon($p);
|
|
|
610 |
}
|
|
|
611 |
|
|
|
612 |
function Point($x,$y,$color) {
|
|
|
613 |
list($x1,$y1) = $this->scale->Translate($x, $y);
|
|
|
614 |
$this->shape->SetColor($color);
|
|
|
615 |
$this->g->img->Point($x1,$y1);
|
|
|
616 |
}
|
|
|
617 |
|
|
|
618 |
function Stroke() {
|
|
|
619 |
$this->g->Stroke();
|
|
|
620 |
}
|
|
|
621 |
|
|
|
622 |
}
|
|
|
623 |
|
|
|
624 |
|
|
|
625 |
class PixelFill {
|
|
|
626 |
|
|
|
627 |
private $edges,$dataPoints,$colors,$isoBars;
|
|
|
628 |
|
|
|
629 |
function __construct(&$edges,&$dataPoints,$isoBars,$colors) {
|
|
|
630 |
$this->edges = $edges;
|
|
|
631 |
$this->dataPoints = $dataPoints;
|
|
|
632 |
$this->colors = $colors;
|
|
|
633 |
$this->isoBars = $isoBars;
|
|
|
634 |
}
|
|
|
635 |
|
|
|
636 |
function GetIsobarColor($val) {
|
|
|
637 |
for ($i = 0 ; $i < count($this->isoBars) ; $i++) {
|
|
|
638 |
if( $val <= $this->isoBars[$i] ) {
|
|
|
639 |
return $this->colors[$i];
|
|
|
640 |
}
|
|
|
641 |
}
|
|
|
642 |
return $this->colors[$i]; // The color for all values above the highest isobar
|
|
|
643 |
}
|
|
|
644 |
|
|
|
645 |
function Fill(ContCanvas $canvas) {
|
|
|
646 |
|
|
|
647 |
$nx_vertices = count($this->dataPoints[0]);
|
|
|
648 |
$ny_vertices = count($this->dataPoints);
|
|
|
649 |
|
|
|
650 |
// Loop through all squares in the grid
|
|
|
651 |
for($col=0; $col < $nx_vertices-1; ++$col) {
|
|
|
652 |
for($row=0; $row < $ny_vertices-1; ++$row) {
|
|
|
653 |
|
|
|
654 |
$v=array(
|
|
|
655 |
$this->dataPoints[$row][$col],
|
|
|
656 |
$this->dataPoints[$row][$col+1],
|
|
|
657 |
$this->dataPoints[$row+1][$col+1],
|
|
|
658 |
$this->dataPoints[$row+1][$col],
|
|
|
659 |
);
|
|
|
660 |
|
|
|
661 |
list($x1,$y1) = $canvas->scale->Translate($col, $row);
|
|
|
662 |
list($x2,$y2) = $canvas->scale->Translate($col+1, $row+1);
|
|
|
663 |
|
|
|
664 |
for( $x=$x1; $x < $x2; ++$x ) {
|
|
|
665 |
for( $y=$y1; $y < $y2; ++$y ) {
|
|
|
666 |
|
|
|
667 |
$v1 = $v[0] + ($v[1]-$v[0])*($x-$x1)/($x2-$x1);
|
|
|
668 |
$v2 = $v[3] + ($v[2]-$v[3])*($x-$x1)/($x2-$x1);
|
|
|
669 |
$val = $v1 + ($v2-$v1)*($y-$y1)/($y2-$y1);
|
|
|
670 |
|
|
|
671 |
if( $row==2 && $col==2 ) {
|
|
|
672 |
//echo " ($val ($x,$y)) (".$v[0].",".$v[1].",".$v[2].",".$v[3].")<br>";
|
|
|
673 |
}
|
|
|
674 |
$color = $this->GetIsobarColor($val);
|
|
|
675 |
$canvas->g->img->SetColor($color);
|
|
|
676 |
$canvas->g->img->Point($x, $y);
|
|
|
677 |
}
|
|
|
678 |
}
|
|
|
679 |
}
|
|
|
680 |
}
|
|
|
681 |
|
|
|
682 |
}
|
|
|
683 |
|
|
|
684 |
}
|
|
|
685 |
|
|
|
686 |
$edges=array(array(),array(),array());
|
|
|
687 |
$datapoints=array();
|
|
|
688 |
for($col=0; $col<6; $col++) {
|
|
|
689 |
for($row=0; $row<6; $row++) {
|
|
|
690 |
$datapoints[$row][$col]=0;
|
|
|
691 |
$edges[VERT_EDGE][$row][$col] = false;
|
|
|
692 |
$edges[HORIZ_EDGE][$row][$col] = false;
|
|
|
693 |
}
|
|
|
694 |
}
|
|
|
695 |
|
|
|
696 |
$datapoints[1][2] = 2;
|
|
|
697 |
$datapoints[2][1] = 1;
|
|
|
698 |
$datapoints[2][2] = 7;
|
|
|
699 |
$datapoints[2][3] = 2;
|
|
|
700 |
$datapoints[3][1] = 2;
|
|
|
701 |
$datapoints[3][2] = 17;
|
|
|
702 |
$datapoints[3][3] = 4;
|
|
|
703 |
$datapoints[4][2] = 3;
|
|
|
704 |
|
|
|
705 |
$datapoints[1][4] = 12;
|
|
|
706 |
|
|
|
707 |
$edges[VERT_EDGE][1][2] = true;
|
|
|
708 |
$edges[VERT_EDGE][3][2] = true;
|
|
|
709 |
|
|
|
710 |
$edges[HORIZ_EDGE][2][1] = true;
|
|
|
711 |
$edges[HORIZ_EDGE][2][2] = true;
|
|
|
712 |
$edges[HORIZ_EDGE][3][1] = true;
|
|
|
713 |
$edges[HORIZ_EDGE][3][2] = true;
|
|
|
714 |
|
|
|
715 |
|
|
|
716 |
|
|
|
717 |
$isobars = array(5,10,15);
|
|
|
718 |
$colors = array('lightgray','lightblue','lightred','red');
|
|
|
719 |
|
|
|
720 |
$engine = new PixelFill($edges, $datapoints, $isobars, $colors);
|
|
|
721 |
$canvas = new ContCanvas();
|
|
|
722 |
$engine->Fill($canvas);
|
|
|
723 |
$canvas->SetDatapoints($datapoints);
|
|
|
724 |
$canvas->Stroke();
|
|
|
725 |
die();
|
|
|
726 |
|
|
|
727 |
|
|
|
728 |
//$tst = new Findpolygon();
|
|
|
729 |
//$p1 = $tst->SetupTestData();
|
|
|
730 |
//
|
|
|
731 |
//$canvas = new ContCanvas();
|
|
|
732 |
//for ($i = 0 ; $i < count($tst->contourCoord); $i++) {
|
|
|
733 |
// $canvas->DrawLinePolygons($tst->contourCoord[$i]);
|
|
|
734 |
//}
|
|
|
735 |
//
|
|
|
736 |
//$p2 = $tst->FindPolygons();
|
|
|
737 |
//for ($i = 0 ; $i < count($p2) ; $i++) {
|
|
|
738 |
// $canvas->FilledPolygon($tst->flattenEdges($p2[$i]));
|
|
|
739 |
//}
|
|
|
740 |
//
|
|
|
741 |
//for ($i = 0 ; $i < count($p2) ; $i++) {
|
|
|
742 |
// $canvas->Polygon($tst->flattenEdges($p2[$i]));
|
|
|
743 |
//}
|
|
|
744 |
//
|
|
|
745 |
//$canvas->Stroke();
|
|
|
746 |
//die();
|
|
|
747 |
|
|
|
748 |
|
|
|
749 |
//for( $trial = 0; $trial < 1; ++$trial ) {
|
|
|
750 |
// echo "\nTest $trial:\n";
|
|
|
751 |
// echo "========================================\n";
|
|
|
752 |
// $tst = new Findpolygon();
|
|
|
753 |
// $p1 = $tst->SetupTestData();
|
|
|
754 |
//
|
|
|
755 |
// // for ($i = 0 ; $i < count($p1) ; $i++) {
|
|
|
756 |
// // echo "Test polygon $i:\n";
|
|
|
757 |
// // echo "---------------------\n";
|
|
|
758 |
// // $tst->p_edges($tst->contourCoord[$i]);
|
|
|
759 |
// // echo "\n";
|
|
|
760 |
// // }
|
|
|
761 |
// //
|
|
|
762 |
// $p2 = $tst->FindPolygons();
|
|
|
763 |
// $npol = count($p2);
|
|
|
764 |
// //echo "\n** Found $npol separate polygon chains.\n\n";
|
|
|
765 |
//
|
|
|
766 |
// for( $i=0; $i<$npol; ++$i ) {
|
|
|
767 |
//
|
|
|
768 |
// $res_forward = $tst->CompareCyclic($p1[$i], $p2[$i],true);
|
|
|
769 |
// $res_backward = $tst->CompareCyclic($p1[$i], $p2[$i],false);
|
|
|
770 |
// if( $res_backward || $res_forward ) {
|
|
|
771 |
// // if( $res_forward )
|
|
|
772 |
// // echo "Forward matches!\n";
|
|
|
773 |
// // else
|
|
|
774 |
// // echo "Backward matches!\n";
|
|
|
775 |
// }
|
|
|
776 |
// else {
|
|
|
777 |
// echo "********** NO MATCH!!.\n\n";
|
|
|
778 |
// echo "\nBefore find:\n";
|
|
|
779 |
// for ($j = 0 ; $j < count($p1[$i]) ; $j++) {
|
|
|
780 |
// echo "(".$p1[$i][$j][0].','.$p1[$i][$j][1]."), ";
|
|
|
781 |
// }
|
|
|
782 |
// echo "\n";
|
|
|
783 |
//
|
|
|
784 |
// echo "\nAfter find:\n";
|
|
|
785 |
// for ($j = 0 ; $j < count($p2[$i]) ; $j++) {
|
|
|
786 |
// echo "(".$p2[$i][$j][0].','.$p2[$i][$j][1]."), ";
|
|
|
787 |
// }
|
|
|
788 |
// echo "\n";
|
|
|
789 |
// }
|
|
|
790 |
//
|
|
|
791 |
// }
|
|
|
792 |
//}
|
|
|
793 |
//
|
|
|
794 |
//echo "\n\nAll tests ready.\n\n";
|
|
|
795 |
//
|
|
|
796 |
|
|
|
797 |
|
|
|
798 |
?>
|