Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 1
<?php
2
/*
3
pSpring - class to draw spring graphs
4
 
5
Version     : 2.1.4
6
Made by     : Jean-Damien POGOLOTTI
7
Last Update : 19/01/2014
8
 
9
This file can be distributed under the license you can find at :
10
 
11
http://www.pchart.net/license
12
 
13
You can find the whole class documentation on the pChart web site.
14
*/
15
define("NODE_TYPE_FREE", 690001);
16
define("NODE_TYPE_CENTRAL", 690002);
17
define("NODE_SHAPE_CIRCLE", 690011);
18
define("NODE_SHAPE_TRIANGLE", 690012);
19
define("NODE_SHAPE_SQUARE", 690013);
20
define("ALGORITHM_RANDOM", 690021);
21
define("ALGORITHM_WEIGHTED", 690022);
22
define("ALGORITHM_CIRCULAR", 690023);
23
define("ALGORITHM_CENTRAL", 690024);
24
define("LABEL_CLASSIC", 690031);
25
define("LABEL_LIGHT", 690032);
26
 
27
/* pSpring class definition */
28
class pSpring
29
{
30
	var $History;
31
	var $pChartObject;
32
	var $Data;
33
	var $Links;
34
	var $X1;
35
	var $Y1;
36
	var $X2;
37
	var $Y2;
38
	var $AutoComputeFreeZone;
39
	var $Labels;
40
 
41
	/* Class creator */
42
	function __construct()
43
	{
44
		/* Initialise data arrays */
45
		$this->Data = [];
46
		$this->Links = [];
47
		/* Set nodes defaults */
48
		$this->Default = ["R" => 255, "G" => 255, "B" => 255, "Alpha" => 100, "BorderR" => 0, "BorderG" => 0, "BorderB" => 0, "BorderAlpha" => 100, "Surrounding" => NULL, "BackgroundR" => 255, "BackgroundG" => 255, "BackgroundB" => 255, "BackgroundAlpha" => 0, "Force" => 1, "NodeType" => NODE_TYPE_FREE, "Size" => 5, "Shape" => NODE_SHAPE_CIRCLE, "FreeZone" => 40, "LinkR" => 0, "LinkG" => 0, "LinkB" => 0, "LinkAlpha" => 0];
49
		$this->Labels = ["Type" => LABEL_CLASSIC, "R" => 0, "G" => 0, "B" => 0, "Alpha" => 100];
50
		$this->AutoComputeFreeZone = FALSE;
51
	}
52
 
53
	/* Set default links options */
54
	function setLinkDefaults(array $Settings = [])
55
	{
56
		#$vars = ["R", "G", "B", "Alpha"];
57
		foreach ($Settings as $key => $value){
58
			$this->Default["Link".$key] = $value;
59
		}
60
	}
61
 
62
	/* Set default links options */
63
	function setLabelsSettings(array $Settings = [])
64
	{
65
		#$vars = ["Type", "R", "G", "B", "Alpha"];
66
		foreach ($Settings as $key => $value){
67
			$this->Labels[$key] = $value;
68
		}
69
	}
70
 
71
	/* Auto compute the FreeZone size based on the number of connections */
72
	function autoFreeZone()
73
	{
74
		/* Check connections reciprocity */
75
		foreach($this->Data as $Key => $Settings) {
76
			$this->Data[$Key]["FreeZone"] = (isset($Settings["Connections"])) ? count($Settings["Connections"]) * 10 + 20 : 20;
77
		}
78
	}
79
 
80
	/* Set link properties */
81
	function linkProperties($FromNode, $ToNode, array $Settings)
82
	{
83
		if (!isset($this->Data[$FromNode])) {
84
			return 0;
85
		}
86
 
87
		if (!isset($this->Data[$ToNode])) {
88
			return 0;
89
		}
90
 
91
		$R = 0;
92
		$G = 0;
93
		$B = 0;
94
		$Alpha = 100;
95
		$Name = NULL;
96
		$Ticks = NULL;
97
 
98
		extract($Settings);
99
 
100
		$this->Links[$FromNode][$ToNode]["R"] = $R;
101
		$this->Links[$ToNode][$FromNode]["R"] = $R;
102
		$this->Links[$FromNode][$ToNode]["G"] = $G;
103
		$this->Links[$ToNode][$FromNode]["G"] = $G;
104
		$this->Links[$FromNode][$ToNode]["B"] = $B;
105
		$this->Links[$ToNode][$FromNode]["B"] = $B;
106
		$this->Links[$FromNode][$ToNode]["Alpha"] = $Alpha;
107
		$this->Links[$ToNode][$FromNode]["Alpha"] = $Alpha;
108
		$this->Links[$FromNode][$ToNode]["Name"] = $Name;
109
		$this->Links[$ToNode][$FromNode]["Name"] = $Name;
110
		$this->Links[$FromNode][$ToNode]["Ticks"] = $Ticks;
111
		$this->Links[$ToNode][$FromNode]["Ticks"] = $Ticks;
112
	}
113
 
114
	function setNodeDefaults(array $Settings = [])
115
	{
116
		#$vars = ["R", "G", "B", "Alpha", "BorderR", "BorderG", "BorderB", "BorderAlpha", "Surrounding", "BackgroundR", "BackgroundG", "BackgroundB", "BackgroundAlpha", "NodeType", "Size", "Shape", "FreeZone"];
117
		foreach ($Settings as $key => $value){
118
			$this->Default[$key] = $value;
119
		}
120
	}
121
 
122
	/* Add a node */
123
	function addNode($NodeID, array $Settings = [])
124
	{
125
		/* if the node already exists, ignore */
126
		if (isset($this->Data[$NodeID])) {
127
			return 0;
128
		}
129
 
130
		$Name = "Node " . $NodeID;
131
		$Connections = NULL;
132
		$R = $this->Default["R"];
133
		$G = $this->Default["G"];
134
		$B = $this->Default["B"];
135
		$Alpha = $this->Default["Alpha"];
136
		$BorderR = $this->Default["BorderR"];
137
		$BorderG = $this->Default["BorderG"];
138
		$BorderB = $this->Default["BorderB"];
139
		$BorderAlpha = $this->Default["BorderAlpha"];
140
		$Surrounding = $this->Default["Surrounding"];
141
		$BackgroundR = $this->Default["BackgroundR"];
142
		$BackgroundG = $this->Default["BackgroundG"];
143
		$BackgroundB = $this->Default["BackgroundB"];
144
		$BackgroundAlpha = $this->Default["BackgroundAlpha"];
145
		$Force = $this->Default["Force"];
146
		$NodeType = $this->Default["NodeType"];
147
		$Size = $this->Default["Size"];
148
		$Shape = $this->Default["Shape"];
149
		$FreeZone = $this->Default["FreeZone"];
150
 
151
		/* Override defaults */
152
		extract($Settings);
153
 
154
		if ($Surrounding != NULL) {
155
			$BorderR = $R + $Surrounding;
156
			$BorderG = $G + $Surrounding;
157
			$BorderB = $B + $Surrounding;
158
		}
159
 
160
		$this->Data[$NodeID] = [
161
			"R" => $R,
162
			"G" => $G,
163
			"B" => $B,
164
			"Alpha" => $Alpha,
165
			"BorderR" => $BorderR,
166
			"BorderG" => $BorderG,
167
			"BorderB" => $BorderB,
168
			"BorderAlpha" => $BorderAlpha,
169
			"BackgroundR" => $BackgroundR,
170
			"BackgroundG" => $BackgroundG,
171
			"BackgroundB" => $BackgroundB,
172
			"BackgroundAlpha" => $BackgroundAlpha,
173
			"Name" => $Name,
174
			"Force" => $Force,
175
			"Type" => $NodeType,
176
			"Size" => $Size,
177
			"Shape" => $Shape,
178
			"FreeZone" => $FreeZone
179
		];
180
 
181
		if ($Connections != NULL) {
182
			if (is_array($Connections)) {
183
				foreach($Connections as $Key => $Value){
184
					$this->Data[$NodeID]["Connections"][] = $Value;
185
				}
186
			} else {
187
				$this->Data[$NodeID]["Connections"][] = $Connections;
188
			}
189
		}
190
	}
191
 
192
	/* Set color attribute for a list of nodes */
193
	function setNodesColor($Nodes, array $Settings = [])
194
	{
195
		if (is_array($Nodes)) {
196
			foreach($Nodes as $Key => $NodeID) {
197
				if (isset($this->Data[$NodeID])) {
198
 
199
					(isset($Settings["R"])) AND $this->Data[$NodeID]["R"] = $Settings["R"];
200
					(isset($Settings["G"])) AND $this->Data[$NodeID]["G"] = $Settings["G"];
201
					(isset($Settings["B"])) AND $this->Data[$NodeID]["B"] = $Settings["B"];
202
					(isset($Settings["Alpha"])) AND $this->Data[$NodeID]["Alpha"] = $Settings["Alpha"];
203
					(isset($Settings["BorderR"])) AND $this->Data[$NodeID]["BorderR"] = $Settings["BorderR"];
204
					(isset($Settings["BorderG"])) AND $this->Data[$NodeID]["BorderG"] = $Settings["BorderG"];
205
					(isset($Settings["BorderB"])) AND $this->Data[$NodeID]["BorderB"] = $Settings["BorderB"];
206
					(isset($Settings["BorderAlpha"])) AND $this->Data[$NodeID]["BorderAlpha"] = $Settings["BorderAlpha"];
207
 
208
					if (isset($Settings["Surrounding"])) {
209
						$this->Data[$NodeID]["BorderR"] = $this->Data[$NodeID]["R"] + $Settings["Surrounding"];
210
						$this->Data[$NodeID]["BorderG"] = $this->Data[$NodeID]["G"] + $Settings["Surrounding"];
211
						$this->Data[$NodeID]["BorderB"] = $this->Data[$NodeID]["B"] + $Settings["Surrounding"];
212
					}
213
				}
214
			}
215
		} else {
216
			(isset($Settings["R"])) AND $this->Data[$Nodes]["R"] = $Settings["R"];
217
			(isset($Settings["G"])) AND $this->Data[$Nodes]["G"] = $Settings["G"];
218
			(isset($Settings["B"])) AND $this->Data[$Nodes]["B"] = $Settings["B"];
219
			(isset($Settings["Alpha"])) AND $this->Data[$Nodes]["Alpha"] = $Settings["Alpha"];
220
			(isset($Settings["BorderR"])) AND $this->Data[$Nodes]["BorderR"] = $Settings["BorderR"];
221
			(isset($Settings["BorderG"])) AND $this->Data[$Nodes]["BorderG"] = $Settings["BorderG"];
222
			(isset($Settings["BorderB"])) AND $this->Data[$Nodes]["BorderB"] = $Settings["BorderB"];
223
			(isset($Settings["BorderAlpha"])) AND $this->Data[$Nodes]["BorderAlpha"] = $Settings["BorderAlpha"];
224
 
225
			if (isset($Settings["Surrounding"])) {
226
				$this->Data[$Nodes]["BorderR"] = $this->Data[$Nodes]["R"] + $Settings["Surrounding"];
227
				$this->Data[$Nodes]["BorderG"] = $this->Data[$Nodes]["G"] + $Settings["Surrounding"];
228
				$this->Data[$Nodes]["BorderB"] = $this->Data[$Nodes]["B"] + $Settings["Surrounding"];
229
			}
230
		}
231
	}
232
 
233
	/* Returns all the nodes details */
234
	function dumpNodes()
235
	{
236
		return $this->Data;
237
	}
238
 
239
	/* Check if a connection exists and create it if required */
240
	function checkConnection($SourceID, $TargetID)
241
	{
242
		if (isset($this->Data[$SourceID]["Connections"])) {
243
			foreach($this->Data[$SourceID]["Connections"] as $Key => $ConnectionID) {
244
				if ($TargetID == $ConnectionID) {
245
					return TRUE;
246
				}
247
			}
248
		}
249
 
250
		$this->Data[$SourceID]["Connections"][] = $TargetID;
251
	}
252
 
253
	/* Get the median linked nodes position */
254
	function getMedianOffset($Key, $X, $Y)
255
	{
256
		$Cpt = 1;
257
		if (isset($this->Data[$Key]["Connections"])) {
258
			foreach($this->Data[$Key]["Connections"] as $ID => $NodeID) {
259
				if (isset($this->Data[$NodeID]["X"]) && isset($this->Data[$NodeID]["Y"])) {
260
					$X = $X + $this->Data[$NodeID]["X"];
261
					$Y = $Y + $this->Data[$NodeID]["Y"];
262
					$Cpt++;
263
				}
264
			}
265
		}
266
 
267
		return ["X" => $X / $Cpt,"Y" => $Y / $Cpt];
268
	}
269
 
270
	/* Return the ID of the attached partner with the biggest weight */
271
	function getBiggestPartner($Key)
272
	{
273
		if (!isset($this->Data[$Key]["Connections"])) {
274
			return "";
275
		}
276
 
277
		$MaxWeight = 0;
278
		$Result = "";
279
		foreach($this->Data[$Key]["Connections"] as $Key => $PeerID) {
280
			if ($this->Data[$PeerID]["Weight"] > $MaxWeight) {
281
				$MaxWeight = $this->Data[$PeerID]["Weight"];
282
				$Result = $PeerID;
283
			}
284
		}
285
 
286
		return $Result;
287
	}
288
 
289
	/* Do the initial node positions computing pass */
290
	function firstPass($Algorithm)
291
	{
292
		$CenterX = ($this->X2 - $this->X1) / 2 + $this->X1;
293
		$CenterY = ($this->Y2 - $this->Y1) / 2 + $this->Y1;
294
		/* Check connections reciprocity */
295
		foreach($this->Data as $Key => $Settings) {
296
			if (isset($Settings["Connections"])) {
297
				foreach($Settings["Connections"] as $ID => $ConnectionID) {
298
					$this->checkConnection($ConnectionID, $Key);
299
				}
300
			}
301
		}
302
 
303
		if ($this->AutoComputeFreeZone) {
304
			$this->autoFreeZone();
305
		}
306
 
307
		/* Get the max number of connections */
308
		$MaxConnections = 0;
309
		foreach($this->Data as $Key => $Settings) {
310
			if (isset($Settings["Connections"])) {
311
				if ($MaxConnections < count($Settings["Connections"])) {
312
					$MaxConnections = count($Settings["Connections"]);
313
				}
314
			}
315
		}
316
 
317
		if ($Algorithm == ALGORITHM_WEIGHTED) {
318
			foreach($this->Data as $Key => $Settings) {
319
				if ($Settings["Type"] == NODE_TYPE_CENTRAL) {
320
					$this->Data[$Key]["X"] = $CenterX;
321
					$this->Data[$Key]["Y"] = $CenterY;
322
				}
323
 
324
				if ($Settings["Type"] == NODE_TYPE_FREE) {
325
					$Connections = (isset($Settings["Connections"])) ? count($Settings["Connections"]) : 0;
326
					$Ring = $MaxConnections - $Connections;
327
					$Angle = rand(0, 360);
328
					$this->Data[$Key]["X"] = cos(deg2rad($Angle)) * ($Ring * $this->RingSize) + $CenterX;
329
					$this->Data[$Key]["Y"] = sin(deg2rad($Angle)) * ($Ring * $this->RingSize) + $CenterY;
330
				}
331
			}
332
		} elseif ($Algorithm == ALGORITHM_CENTRAL) {
333
			/* Put a weight on each nodes */
334
			foreach($this->Data as $Key => $Settings) {
335
				$this->Data[$Key]["Weight"] = (isset($Settings["Connections"])) ? count($Settings["Connections"]) : 0;
336
			}
337
 
338
			$MaxConnections = $MaxConnections + 1;
339
			for ($i = $MaxConnections; $i >= 0; $i--) {
340
				foreach($this->Data as $Key => $Settings) {
341
					if ($Settings["Type"] == NODE_TYPE_CENTRAL) {
342
						$this->Data[$Key]["X"] = $CenterX;
343
						$this->Data[$Key]["Y"] = $CenterY;
344
					}
345
 
346
					if ($Settings["Type"] == NODE_TYPE_FREE) {
347
						$Connections = (isset($Settings["Connections"])) ? count($Settings["Connections"]) : 0;
348
						if ($Connections == $i) {
349
							$BiggestPartner = $this->getBiggestPartner($Key);
350
							if ($BiggestPartner != "") {
351
								$Ring = $this->Data[$BiggestPartner]["FreeZone"];
352
								$Weight = $this->Data[$BiggestPartner]["Weight"];
353
								$AngleDivision = 360 / $this->Data[$BiggestPartner]["Weight"];
354
								$Done = FALSE;
355
								$Tries = 0;
356
								while (!$Done && $Tries <= $Weight * 2) {
357
									$Tries++;
358
									$Angle = floor(rand(0, $Weight) * $AngleDivision);
359
									if (!isset($this->Data[$BiggestPartner]["Angular"][$Angle]) || !isset($this->Data[$BiggestPartner]["Angular"])) {
360
										$this->Data[$BiggestPartner]["Angular"][$Angle] = $Angle;
361
										$Done = TRUE;
362
									}
363
								}
364
 
365
								if (!$Done) {
366
									$Angle = rand(0, 360);
367
									$this->Data[$BiggestPartner]["Angular"][$Angle] = $Angle;
368
								}
369
 
370
								$X = cos(deg2rad($Angle)) * ($Ring) + $this->Data[$BiggestPartner]["X"];
371
								$Y = sin(deg2rad($Angle)) * ($Ring) + $this->Data[$BiggestPartner]["Y"];
372
								$this->Data[$Key]["X"] = $X;
373
								$this->Data[$Key]["Y"] = $Y;
374
							}
375
						}
376
					}
377
				}
378
			}
379
		} elseif ($Algorithm == ALGORITHM_CIRCULAR) {
380
			$MaxConnections = $MaxConnections + 1;
381
			for ($i = $MaxConnections; $i >= 0; $i--) {
382
				foreach($this->Data as $Key => $Settings) {
383
					if ($Settings["Type"] == NODE_TYPE_CENTRAL) {
384
						$this->Data[$Key]["X"] = $CenterX;
385
						$this->Data[$Key]["Y"] = $CenterY;
386
					}
387
 
388
					if ($Settings["Type"] == NODE_TYPE_FREE) {
389
						$Connections = (isset($Settings["Connections"])) ? count($Settings["Connections"]) : 0;
390
						if ($Connections == $i) {
391
							$Ring = $MaxConnections - $Connections;
392
							$Angle = rand(0, 360);
393
							$X = cos(deg2rad($Angle)) * ($Ring * $this->RingSize) + $CenterX;
394
							$Y = sin(deg2rad($Angle)) * ($Ring * $this->RingSize) + $CenterY;
395
							$MedianOffset = $this->getMedianOffset($Key, $X, $Y);
396
							$this->Data[$Key]["X"] = $MedianOffset["X"];
397
							$this->Data[$Key]["Y"] = $MedianOffset["Y"];
398
						}
399
					}
400
				}
401
			}
402
		} elseif ($Algorithm == ALGORITHM_RANDOM) {
403
			foreach($this->Data as $Key => $Settings) {
404
				if ($Settings["Type"] == NODE_TYPE_FREE) {
405
					$this->Data[$Key]["X"] = $CenterX + rand(-20, 20);
406
					$this->Data[$Key]["Y"] = $CenterY + rand(-20, 20);
407
				}
408
 
409
				if ($Settings["Type"] == NODE_TYPE_CENTRAL) {
410
					$this->Data[$Key]["X"] = $CenterX;
411
					$this->Data[$Key]["Y"] = $CenterY;
412
				}
413
			}
414
		}
415
	}
416
 
417
	/* Compute one pass */
418
	function doPass()
419
	{
420
		/* Compute vectors */
421
		foreach($this->Data as $Key => $Settings) {
422
			if ($Settings["Type"] != NODE_TYPE_CENTRAL) {
423
				unset($this->Data[$Key]["Vectors"]);
424
				$X1 = $Settings["X"];
425
				$Y1 = $Settings["Y"];
426
				/* Repulsion vectors */
427
				foreach($this->Data as $Key2 => $Settings2) {
428
					if ($Key != $Key2) {
429
						$X2 = $this->Data[$Key2]["X"];
430
						$Y2 = $this->Data[$Key2]["Y"];
431
						$FreeZone = $this->Data[$Key2]["FreeZone"];
432
						$Distance = $this->getDistance($X1, $Y1, $X2, $Y2);
433
						$Angle = $this->getAngle($X1, $Y1, $X2, $Y2) + 180;
434
						/* Nodes too close, repulsion occurs */
435
						if ($Distance < $FreeZone) {
436
							$Force = log(pow(2, $FreeZone - $Distance));
437
							if ($Force > 1) {
438
								$this->Data[$Key]["Vectors"][] = ["Type" => "R","Angle" => $Angle % 360,"Force" => $Force];
439
							}
440
						}
441
						$LastKey = $Key2;
442
					}
443
				}
444
 
445
				/* Attraction vectors */
446
				if (isset($Settings["Connections"])) {
447
					foreach($Settings["Connections"] as $ID => $NodeID) {
448
						if (isset($this->Data[$NodeID])) {
449
							$X2 = $this->Data[$NodeID]["X"];
450
							$Y2 = $this->Data[$NodeID]["Y"];
451
							$FreeZone = $this->Data[$LastKey]["FreeZone"];
452
							$Distance = $this->getDistance($X1, $Y1, $X2, $Y2);
453
							$Angle = $this->getAngle($X1, $Y1, $X2, $Y2);
454
							if ($Distance > $FreeZone) {
455
								$Force = log(($Distance - $FreeZone) + 1);
456
							} else {
457
								$Force = log(($FreeZone - $Distance) + 1);
458
								$Angle += 180;
459
							}
460
 
461
							if ($Force > 1) {
462
								$this->Data[$Key]["Vectors"][] = ["Type" => "A","Angle" => $Angle % 360,"Force" => $Force];
463
							}
464
						}
465
					}
466
				}
467
			}
468
		}
469
 
470
		/* Move the nodes accoding to the vectors */
471
		foreach($this->Data as $Key => $Settings) {
472
			$X = $Settings["X"];
473
			$Y = $Settings["Y"];
474
			if (isset($Settings["Vectors"]) && $Settings["Type"] != NODE_TYPE_CENTRAL) {
475
				foreach($Settings["Vectors"] as $ID => $Vector) {
476
					$Type = $Vector["Type"];
477
					$Force = $Vector["Force"];
478
					$Angle = $Vector["Angle"];
479
					$Factor = $Type == "A" ? $this->MagneticForceA : $this->MagneticForceR;
480
					$X = cos(deg2rad($Angle)) * $Force * $Factor + $X;
481
					$Y = sin(deg2rad($Angle)) * $Force * $Factor + $Y;
482
				}
483
			}
484
 
485
			$this->Data[$Key]["X"] = $X;
486
			$this->Data[$Key]["Y"] = $Y;
487
		}
488
	}
489
 
490
	function lastPass()
491
	{
492
		/* Put everything inside the graph area */
493
		foreach($this->Data as $Key => $Settings) {
494
			$X = $Settings["X"];
495
			$Y = $Settings["Y"];
496
			($X < $this->X1) AND $X = $this->X1;
497
			($X > $this->X2) AND $X = $this->X2;
498
			($Y < $this->Y1) AND $Y = $this->Y1;
499
			($Y > $this->Y2) AND $Y = $this->Y2;
500
			$this->Data[$Key]["X"] = $X;
501
			$this->Data[$Key]["Y"] = $Y;
502
		}
503
 
504
		/* Dump all links */
505
		$Links = [];
506
		foreach($this->Data as $Key => $Settings) {
507
			$X1 = $Settings["X"];
508
			$Y1 = $Settings["Y"];
509
			if (isset($Settings["Connections"])) {
510
				foreach($Settings["Connections"] as $ID => $NodeID) {
511
					if (isset($this->Data[$NodeID])) {
512
						$X2 = $this->Data[$NodeID]["X"];
513
						$Y2 = $this->Data[$NodeID]["Y"];
514
						$Links[] = ["X1" => $X1,"Y1" => $Y1,"X2" => $X2,"Y2" => $Y2,"Source" => $Settings["Name"],"Destination" => $this->Data[$NodeID]["Name"]];
515
					}
516
				}
517
			}
518
		}
519
 
520
		/* Check collisions */
521
		$Conflicts = 0;
522
		foreach($this->Data as $Key => $Settings) {
523
			$X1 = $Settings["X"];
524
			$Y1 = $Settings["Y"];
525
			if (isset($Settings["Connections"])) {
526
				foreach($Settings["Connections"] as $ID => $NodeID) {
527
					if (isset($this->Data[$NodeID])) {
528
						$X2 = $this->Data[$NodeID]["X"];
529
						$Y2 = $this->Data[$NodeID]["Y"];
530
						foreach($Links as $IDLinks => $Link) {
531
							$X3 = $Link["X1"];
532
							$Y3 = $Link["Y1"];
533
							$X4 = $Link["X2"];
534
							$Y4 = $Link["Y2"];
535
							if (!($X1 == $X3 && $X2 == $X4 && $Y1 == $Y3 && $Y2 == $Y4)) {
536
								if ($this->intersect($X1, $Y1, $X2, $Y2, $X3, $Y3, $X4, $Y4)) {
537
									if ($Link["Source"] != $Settings["Name"] && $Link["Source"] != $this->Data[$NodeID]["Name"] && $Link["Destination"] != $Settings["Name"] && $Link["Destination"] != $this->Data[$NodeID]["Name"]) {
538
										$Conflicts++;
539
									}
540
								}
541
							}
542
						}
543
					}
544
				}
545
			}
546
		}
547
 
548
		return ($Conflicts / 2);
549
	}
550
 
551
	/* Center the graph */
552
	function center()
553
	{
554
		/* Determine the real center */
555
		$TargetCenterX = ($this->X2 - $this->X1) / 2 + $this->X1;
556
		$TargetCenterY = ($this->Y2 - $this->Y1) / 2 + $this->Y1;
557
		/* Get current boundaries */
558
		$XMin = $this->X2;
559
		$XMax = $this->X1;
560
		$YMin = $this->Y2;
561
		$YMax = $this->Y1;
562
		foreach($this->Data as $Key => $Settings) {
563
			$X = $Settings["X"];
564
			$Y = $Settings["Y"];
565
			($X < $XMin) AND $XMin = $X;
566
			($X > $XMax) AND $XMax = $X;
567
			($Y < $YMin) AND $YMin = $Y;
568
			($Y > $YMax) AND $YMax = $Y;
569
		}
570
 
571
		$CurrentCenterX = ($XMax - $XMin) / 2 + $XMin;
572
		$CurrentCenterY = ($YMax - $YMin) / 2 + $YMin;
573
		/* Compute the offset to apply */
574
		$XOffset = $TargetCenterX - $CurrentCenterX;
575
		$YOffset = $TargetCenterY - $CurrentCenterY;
576
		/* Correct the points position */
577
		foreach($this->Data as $Key => $Settings) {
578
			$this->Data[$Key]["X"] = $Settings["X"] + $XOffset;
579
			$this->Data[$Key]["Y"] = $Settings["Y"] + $YOffset;
580
		}
581
	}
582
 
583
	/* Create the encoded string */
584
	function drawSpring($Object, array $Settings = [])
585
	{
586
		$this->pChartObject = $Object;
587
		$Pass = isset($Settings["Pass"]) ? $Settings["Pass"] : 50;
588
		$Retries = isset($Settings["Retry"]) ? $Settings["Retry"] : 10;
589
		$this->MagneticForceA = isset($Settings["MagneticForceA"]) ? $Settings["MagneticForceA"] : 1.5;
590
		$this->MagneticForceR = isset($Settings["MagneticForceR"]) ? $Settings["MagneticForceR"] : 2;
591
		$this->RingSize = isset($Settings["RingSize"]) ? $Settings["RingSize"] : 40;
592
		$DrawVectors = isset($Settings["DrawVectors"]) ? $Settings["DrawVectors"] : FALSE;
593
		$DrawQuietZone = isset($Settings["DrawQuietZone"]) ? $Settings["DrawQuietZone"] : FALSE;
594
		$CenterGraph = isset($Settings["CenterGraph"]) ? $Settings["CenterGraph"] : TRUE;
595
		$TextPadding = isset($Settings["TextPadding"]) ? $Settings["TextPadding"] : 4;
596
		$Algorithm = isset($Settings["Algorithm"]) ? $Settings["Algorithm"] : ALGORITHM_WEIGHTED;
597
		$FontSize = $Object->FontSize;
598
		$this->X1 = $Object->GraphAreaX1;
599
		$this->Y1 = $Object->GraphAreaY1;
600
		$this->X2 = $Object->GraphAreaX2;
601
		$this->Y2 = $Object->GraphAreaY2;
602
		$Conflicts = 1;
603
		$Jobs = 0;
604
		$this->History["MinimumConflicts"] = - 1;
605
		while ($Conflicts != 0 && $Jobs < $Retries) {
606
			$Jobs++;
607
			/* Compute the initial settings */
608
			$this->firstPass($Algorithm);
609
			/* Apply the vectors */
610
			if ($Pass > 0) {
611
				for ($i = 0; $i <= $Pass; $i++) {
612
					$this->doPass();
613
				}
614
			}
615
 
616
			$Conflicts = $this->lastPass();
617
			if ($this->History["MinimumConflicts"] == - 1 || $Conflicts < $this->History["MinimumConflicts"]) {
618
				$this->History["MinimumConflicts"] = $Conflicts;
619
				$this->History["Result"] = $this->Data;
620
			}
621
		}
622
 
623
		$Conflicts = $this->History["MinimumConflicts"];
624
		$this->Data = $this->History["Result"];
625
		if ($CenterGraph) {
626
			$this->center();
627
		}
628
 
629
		/* Draw the connections */
630
		$Drawn = [];
631
		foreach($this->Data as $Key => $Settings) {
632
			$X = $Settings["X"];
633
			$Y = $Settings["Y"];
634
			if (isset($Settings["Connections"])) {
635
				foreach($Settings["Connections"] as $ID => $NodeID) {
636
					if (!isset($Drawn[$Key])) {
637
						$Drawn[$Key] = [];
638
					}
639
 
640
					if (!isset($Drawn[$NodeID])) {
641
						$Drawn[$NodeID] = [];
642
					}
643
 
644
					if (isset($this->Data[$NodeID]) && !isset($Drawn[$Key][$NodeID]) && !isset($Drawn[$NodeID][$Key])) {
645
						$Color = ["R" => $this->Default["LinkR"],"G" => $this->Default["LinkG"],"B" => $this->Default["LinkB"],"Alpha" => $this->Default["Alpha"]];
646
						#if ($this->Links != "") {
647
						if (count($this->Links) > 0) {
648
							if (isset($this->Links[$Key][$NodeID]["R"])) {
649
								$Color = ["R" => $this->Links[$Key][$NodeID]["R"],"G" => $this->Links[$Key][$NodeID]["G"],"B" => $this->Links[$Key][$NodeID]["B"],"Alpha" => $this->Links[$Key][$NodeID]["Alpha"]];
650
							}
651
 
652
							if (isset($this->Links[$Key][$NodeID]["Ticks"])) {
653
								$Color["Ticks"] = $this->Links[$Key][$NodeID]["Ticks"];
654
							}
655
						}
656
 
657
						$X2 = $this->Data[$NodeID]["X"];
658
						$Y2 = $this->Data[$NodeID]["Y"];
659
						$this->pChartObject->drawLine($X, $Y, $X2, $Y2, $Color);
660
						$Drawn[$Key][$NodeID] = TRUE;
661
						#if (isset($this->Links) && $this->Links != "") {
662
						if (count($this->Links) > 0) {
663
							if (isset($this->Links[$Key][$NodeID]["Name"]) || isset($this->Links[$NodeID][$Key]["Name"])) {
664
								$Name = isset($this->Links[$Key][$NodeID]["Name"]) ? $this->Links[$Key][$NodeID]["Name"] : $this->Links[$NodeID][$Key]["Name"];
665
								$TxtX = ($X2 - $X) / 2 + $X;
666
								$TxtY = ($Y2 - $Y) / 2 + $Y;
667
								if ($X <= $X2) {
668
									$Angle = (360 - $this->getAngle($X, $Y, $X2, $Y2)) % 360;
669
								} else {
670
									$Angle = (360 - $this->getAngle($X2, $Y2, $X, $Y)) % 360;
671
								}
672
 
673
								$Settings = $Color;
674
								$Settings["Angle"] = $Angle;
675
								$Settings["Align"] = TEXT_ALIGN_BOTTOMMIDDLE;
676
								$this->pChartObject->drawText($TxtX, $TxtY, $Name, $Settings);
677
							}
678
						}
679
					}
680
				}
681
			}
682
		}
683
 
684
		/* Draw the quiet zones */
685
		if ($DrawQuietZone) {
686
			foreach($this->Data as $Key => $Settings) {
687
				$X = $Settings["X"];
688
				$Y = $Settings["Y"];
689
				$FreeZone = $Settings["FreeZone"];
690
				$this->pChartObject->drawFilledCircle($X, $Y, $FreeZone, ["R" => 0,"G" => 0,"B" => 0,"Alpha" => 2]);
691
			}
692
		}
693
 
694
		/* Draw the nodes */
695
		foreach($this->Data as $Key => $Settings) {
696
			$X = $Settings["X"];
697
			$Y = $Settings["Y"];
698
			$Name = $Settings["Name"];
699
			$FreeZone = $Settings["FreeZone"];
700
			$Shape = $Settings["Shape"];
701
			$Size = $Settings["Size"];
702
			$Color = [
703
				"R" => $Settings["R"],
704
				"G" => $Settings["G"],
705
				"B" => $Settings["B"],
706
				"Alpha" => $Settings["Alpha"],
707
				"BorderR" => $Settings["BorderR"],
708
				"BorderG" => $Settings["BorderG"],
709
				"BorderB" => $Settings["BorderB"],
710
				"BorderApha" => $Settings["BorderAlpha"]
711
			];
712
			if ($Shape == NODE_SHAPE_CIRCLE) {
713
				$this->pChartObject->drawFilledCircle($X, $Y, $Size, $Color);
714
			} elseif ($Shape == NODE_SHAPE_TRIANGLE) {
715
				$Points = [cos(deg2rad(270)) * $Size + $X, sin(deg2rad(270)) * $Size + $Y, cos(deg2rad(45)) * $Size + $X, sin(deg2rad(45)) * $Size + $Y, cos(deg2rad(135)) * $Size + $X, sin(deg2rad(135)) * $Size + $Y, ];
716
				$this->pChartObject->drawPolygon($Points, $Color);
717
			} elseif ($Shape == NODE_SHAPE_SQUARE) {
718
				$Offset = $Size / 2;
719
				$Size = $Size / 2;
720
				$this->pChartObject->drawFilledRectangle($X - $Offset, $Y - $Offset, $X + $Offset, $Y + $Offset, $Color);
721
			}
722
 
723
			if ($Name != "") {
724
				$LabelOptions = ["R" => $this->Labels["R"],"G" => $this->Labels["G"],"B" => $this->Labels["B"],"Alpha" => $this->Labels["Alpha"]];
725
				if ($this->Labels["Type"] == LABEL_LIGHT) {
726
					$LabelOptions["Align"] = TEXT_ALIGN_BOTTOMLEFT;
727
					$this->pChartObject->drawText($X, $Y, $Name, $LabelOptions);
728
				} elseif ($this->Labels["Type"] == LABEL_CLASSIC) {
729
					$LabelOptions["Align"] = TEXT_ALIGN_TOPMIDDLE;
730
					$LabelOptions["DrawBox"] = TRUE;
731
					$LabelOptions["BoxAlpha"] = 50;
732
					$LabelOptions["BorderOffset"] = 4;
733
					$LabelOptions["RoundedRadius"] = 3;
734
					$LabelOptions["BoxRounded"] = TRUE;
735
					$LabelOptions["NoShadow"] = TRUE;
736
					$this->pChartObject->drawText($X, $Y + $Size + $TextPadding, $Name, $LabelOptions);
737
				}
738
			}
739
		}
740
 
741
		/* Draw the vectors */
742
		if ($DrawVectors) {
743
			foreach($this->Data as $Key => $Settings) {
744
				$X1 = $Settings["X"];
745
				$Y1 = $Settings["Y"];
746
				if (isset($Settings["Vectors"]) && $Settings["Type"] != NODE_TYPE_CENTRAL) {
747
					foreach($Settings["Vectors"] as $ID => $Vector) {
748
						$Type = $Vector["Type"];
749
						$Force = $Vector["Force"];
750
						$Angle = $Vector["Angle"];
751
						$Factor = $Type == "A" ? $this->MagneticForceA : $this->MagneticForceR;
752
						$Color = $Type == "A" ? ["FillR" => 255,"FillG" => 0,"FillB" => 0] : ["FillR" => 0,"FillG" => 255,"FillB" => 0];
753
						$X2 = cos(deg2rad($Angle)) * $Force * $Factor + $X1;
754
						$Y2 = sin(deg2rad($Angle)) * $Force * $Factor + $Y1;
755
						$this->pChartObject->drawArrow($X1, $Y1, $X2, $Y2, $Color);
756
					}
757
				}
758
			}
759
		}
760
 
761
		return ["Pass" => $Jobs,"Conflicts" => $Conflicts];
762
	}
763
 
764
	/* Return the distance between two points */
765
	function getDistance($X1, $Y1, $X2, $Y2)
766
	{
767
		return sqrt(($X2 - $X1) * ($X2 - $X1) + ($Y2 - $Y1) * ($Y2 - $Y1));
768
	}
769
 
770
	/* Return the angle made by a line and the X axis */
771
	function getAngle($X1, $Y1, $X2, $Y2)
772
	{
773
		#$Opposite = $Y2 - $Y1;
774
		#$Adjacent = $X2 - $X1;
775
		$Angle = rad2deg(atan2(($Y2 - $Y1), ($X2 - $X1)));
776
 
777
		return ($Angle > 0) ? $Angle : (360 - abs($Angle));
778
 
779
	}
780
 
781
	function intersect($X1, $Y1, $X2, $Y2, $X3, $Y3, $X4, $Y4)
782
	{
783
		$A = (($X3 * $Y4 - $X4 * $Y3) * ($X1 - $X2) - ($X1 * $Y2 - $X2 * $Y1) * ($X3 - $X4));
784
		$B = (($Y1 - $Y2) * ($X3 - $X4) - ($Y3 - $Y4) * ($X1 - $X2));
785
		if ($B == 0) {
786
			return FALSE;
787
		}
788
 
789
		$Xi = $A / $B;
790
		$C = ($X1 - $X2);
791
		if ($C == 0) {
792
			return FALSE;
793
		}
794
 
795
		$Yi = $Xi * (($Y1 - $Y2) / $C) + (($X1 * $Y2 - $X2 * $Y1) / $C);
796
		if ($Xi >= min($X1, $X2) && $Xi >= min($X3, $X4) && $Xi <= max($X1, $X2) && $Xi <= max($X3, $X4)) {
797
			if ($Yi >= min($Y1, $Y2) && $Yi >= min($Y3, $Y4) && $Yi <= max($Y1, $Y2) && $Yi <= max($Y3, $Y4)) {
798
				return TRUE;
799
			}
800
		}
801
 
802
		return FALSE;
803
	}
804
}
805
 
806
?>