forked from librenms-plugins/Weathermap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
WMVector.class.php
121 lines (102 loc) · 2.36 KB
/
WMVector.class.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
<?php
/**
* Utility class for 2D vectors.
* Mostly used in the VIA calculations
*/
class WMVector
{
public $dx;
public $dy;
public function __construct($dx = 0, $dy = 0)
{
$this->dx = $dx;
$this->dy = $dy;
}
public function flip()
{
$this->dx = - $this->dx;
$this->dy = - $this->dy;
}
public function getAngle()
{
return rad2deg(atan2((-$this->dy), ($this->dx)));
}
public function getSlope()
{
if ($this->dx == 0) {
// special case - if slope is infinite, fudge it to be REALLY BIG instead. Close enough for TV.
wm_debug("Slope is infinite.\n");
return 1e10;
}
return ($this->dy / $this->dx);
}
/**
* @param float $angle
*/
public function rotate($angle)
{
$points = array();
$points[0] = $this->dx;
$points[1] = $this->dy;
rotateAboutPoint($points, 0, 0, $angle);
$this->dx = $points[0];
$this->dy = $points[1];
}
/**
* @return WMVector
*/
public function getNormal()
{
$len = $this->length();
if ($len==0) {
return new WMVector(0, 0);
}
return new WMVector($this->dy / $len, -$this->dx / $len);
}
/**
* Turn vector into unit-vector
*/
public function normalise()
{
$len = $this->length();
if ($len > 0 && $len != 1) {
$this->dx = $this->dx / $len;
$this->dy = $this->dy / $len;
}
}
/**
* Calculate the square of the vector length.
* Save calculating a square-root if all you need to do is compare lengths
*
* @return float
*/
public function squaredLength()
{
if (($this->dx == 0) && ($this->dy == 0)) {
return 0;
}
$squaredLength = ($this->dx) * ($this->dx) + ($this->dy) * ($this->dy);
return $squaredLength;
}
/**
* @return float
*/
public function length()
{
if ($this->dx==0 && $this->dy==0) {
return 0;
}
return (sqrt($this->squaredLength()));
}
public function asString()
{
return $this->__toString();
}
/**
* @return string
*/
public function __toString()
{
return sprintf("[%f,%f]", $this->dx, $this->dy);
}
}