2dmath.class.php
3.87 KB
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
<?php
class Vector2{
public $x;
public $y;
public function __construct($x = 0.0, $y = 0.0){
$this->x = $x;
$this->y = $y;
}
public function zero(){
$this->x = 0.0;
$this->y = 0.0;
}
public function add($v){
$this->x += $v->x;
$this->y += $v->y;
}
public function sub($v){
$this->x -= $v->x;
$this->y -= $v->y;
}
public static function addPoint($v1, $v2){
return new Vector2( $v1->x + $v2->x, $v1->y + $v2->y );
}
public static function subPoint($v1, $v2){
return new Vector2( $v1->x - $v2->x, $v1->y - $v2->y );
}
public function length(){
return sqrt(($this->x * $this->x) + ($this->y * $this->y));
}
public function length2(){
return abs(($this->x * $this->x) + ($this->y * $this->y));
}
public function mulScalar($value){
$this->x *= $value;
$this->y *= $value;
}
public function divScalar($value){
$this->x /= $value;
$this->y /= $value;
}
public function normalize(){
$len = $this->length();
if(!$len) $this->zero();
else{
$len = 1.0/$len;
$this->x *= $len;
$this->y *= $len;
}
}
public function normal(){
$len = $this->length();
if(!$len) return new Vector2();
$len = 1.0/$len;
return new Vector2($this->x*$len, $this->y*$len);
}
public function dotProduct($v){
return ($this->x * $v->x) + ($this->y * $v->y);
}
public static function dotProduct2($v1, $v2){
return ($v1->x * $v2->x) + ($v1->y * $v2->y);
}
public function switchDirection(){
$this->x = -($this->x);
$this->y = -($this->y);
}
public function distance($v){
$x = $this->x - $v->x;
$y = $this->y - $v->y;
return sqrt( ($x*$x) + ($y*$y) );
}
public function distance2($v){
$x = $this->x - $v->x;
$y = $this->y - $v->y;
return ($x*$x) + ($y*$y);
}
public static function pointDistance($v1, $v2){
$x = $v1->x - $v2->x;
$y = $v1->y - $v2->y;
return sqrt( ($x*$x) + ($y*$y) );
}
public static function pointDistance2($v1, $v2){
$x = $v1->x - $v2->x;
$y = $v1->y - $v2->y;
return ($x*$x) + ($y*$y);
}
};
class Polygon2{
public $vertex;
public function __construct($vertex = null){
if(!$vertex) $this->vertex = array();
else $this->vertex = $vertex;
}
public function addPoint($x, $y){
$this->vertex[] = new Vector2($x,$y);
}
};
class Circle2{
public $radius;
public $center;
public function __construct($center = null, $radius = 0){
$this->center = $center ? $center : new Vector2();
$this->radius = $radius;
}
public function fromPolygon($poly){
if($poly->vertex){
$max_x = -(INF);
$max_y = -(INF);
$min_x = INF;
$min_y = INF;
foreach($poly->vertex as $v){
if($v->x > $max_x) $max_x = $v->x;
if($v->x < $min_x) $min_x = $v->x;
if($v->y > $max_y) $max_y = $v->y;
if($v->y < $min_y) $min_y = $v->y;
}
$max_x = abs(($max_x - $min_x)/2.0);
$max_y = abs(($max_y - $min_y)/2.0);
$this->center->x = $min_x + $max_x;
$this->center->y = $min_y + $max_y;
$this->radius = $max_x > $max_y ? $max_x : $max_y;
}
else{
$this->center->zero();
$this->radius = 0.0;
}
}
public function testCircle($circ){
$v = Vector2::subPoint($this->center, $circ->center);
$dist = $v->length2();
$r1 = abs($this->radius);
$r1 *= $r1;
$r2 = abs($circ->radius);
$r2 *= $r2;
return ($dist <= ($r2 + $r1)) ? true : false;
}
public function testPoint($point){
$dist = Vector2::subPoint($this->center, $point)->length2();
$r2 = $this->radius;
$r2 *= $r2;
return ($dist <= abs($r2)) ? true : false;
}
};
?>