-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathLineTest.php
184 lines (153 loc) · 4.73 KB
/
LineTest.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
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
175
176
177
178
179
180
181
182
183
184
<?php
use \JWare\GeoPHP\Line;
use \JWare\GeoPHP\Point;
use \JWare\GeoPHP\Polygon;
class LineTest extends \PHPUnit\Framework\TestCase {
public function testInstantiationOfLine() {
$this->assertInstanceOf('\JWare\GeoPHP\Line', new Line(
new Point(0, 0),
new Point(1, 1)
));
}
/**
* Tests clone method
*/
public function testClone() {
$line = new Line(
new Point(0, 0),
new Point(1, 1)
);
$clone = $line->clone();
$this->assertEquals($clone->getPoints(), $line->getPoints());
}
/**
* Tests the determinant of the line
*/
public function testDeterminant() {
$line1 = new Line(
new Point(1.34, 2.87),
new Point(3.23, 4.5)
);
$line2 = new Line(
new Point(3.23, 4.5),
new Point(1.34, 2.87)
);
$this->assertEquals(-3.2401, $line1->determinant());
// NOTE: Line(a, b).determinant() == -Line(b, a).determinant()
$this->assertEquals($line1->determinant(), -$line2->determinant());
}
/**
* Tests the difference in 'x' components (Δx).
*/
public function testDx() {
$line1 = new Line(
new Point(2.77, 4.23),
new Point(2.77, 7.23)
);
$line2 = new Line(
new Point(2.77, 4.23),
new Point(3.77, 7.23)
);
$this->assertEquals(0, $line1->dx());
$this->assertEquals(1, $line2->dx());
}
/**
* Tests the difference in 'y' components (Δy).
*/
public function testDy() {
$line1 = new Line(
new Point(2.77, 4.23),
new Point(2.77, 7.23)
);
$line2 = new Line(
new Point(2.77, 14.32),
new Point(3.77, 9.55)
);
$this->assertEquals(3, $line1->dy());
$this->assertEquals(-4.77, $line2->dy());
}
/**
* Tests the slope of a line
*/
public function testSlope() {
$line1 = new Line(
new Point(2.87, 4.23),
new Point(2.77, 7)
);
$line2 = new Line(
new Point(3.45, 11.3),
new Point(0.87, 34.243)
);
$line3 = new Line(
new Point(0.87, 34.243),
new Point(3.45, 11.3)
);
$line4 = new Line(
new Point(2, 34),
new Point(2, 11)
);
$this->assertEquals(-27.7, $line1->slope());
$this->assertEquals(-8.8926356589147, $line2->slope());
// NOTE: Line(a, b).slope() == Line(b, a).slope()
$this->assertEquals($line2->slope(), $line3->slope());
$this->assertEquals(INF, $line4->slope());
}
/**
* Test intersection method
*/
public function testIntersects() {
// Lines
$line1 = new Line(
new Point(1, 1),
new Point(5, 5)
);
$line2 = new Line(
new Point(3, 3),
new Point(5, 1)
);
$line3 = new Line(
new Point(-1, 1),
new Point(-5, -10)
);
// Points
$point1 = new Point(3, 3);
$point2 = new Point(-1, 1);
$point3 = new Point(-5, -10);
// Polygons
$polygon1 = new Polygon([
new Point(2, 4),
new Point(4, 4),
new Point(4, 2),
new Point(3, 1),
new Point(2.5, 3),
new Point(2, 4)
]);
$polygon2 = new Polygon([
new Point(-3, -4),
new Point(-1, -5),
new Point(-2, -6),
new Point(-3, -4)
]);
// With Point
$this->assertTrue($line1->intersectsPoint($point1));
$this->assertTrue($line2->intersectsPoint($point1));
$this->assertFalse($line3->intersectsPoint($point1));
$this->assertTrue($line3->intersectsPoint($point2));
$this->assertTrue($line3->intersectsPoint($point3));
// With Line
$this->assertTrue($line1->intersectsLine($line2));
$this->assertTrue($line2->intersectsLine($line1));
$this->assertTrue($line1->intersectsLine($line1));
$this->assertFalse($line1->intersectsLine($line3));
$this->assertFalse($line2->intersectsLine($line3));
$this->assertFalse($line3->intersectsLine($line2));
// With Polygon
$this->assertTrue($line1->intersectsPolygon($polygon1));
$this->assertTrue($line2->intersectsPolygon($polygon1));
$this->assertTrue($line3->intersectsPolygon($polygon2));
$this->assertFalse($line1->intersectsPolygon($polygon2));
$this->assertFalse($line2->intersectsPolygon($polygon2));
$this->assertFalse($line3->intersectsPolygon($polygon1));
}
}
?>