-
Notifications
You must be signed in to change notification settings - Fork 0
/
base.php
66 lines (49 loc) · 1.82 KB
/
base.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
<?php
// Here are the algorithm and some base functions
// Here will be calculation to find point from map
function seek_point($user_point, $polygon_map) {
// Counter that calculates amount of intersections with polygon borders (lines)
$intersections = 0;
// How many coordinates we have on map, so many iterations..
$count = count($polygon_map);
for ($i=0; $i<=$count; $i++) {
// Select two next points from array (line)
$point1 = $polygon_map[$i];
$point2 = $polygon_map[$i+1];
// If last round, our second point is starting point
if ($i == $count) {
$point2 = $polygon_map[0];
}
// Check if given point is between min and max coordinate points
if (min($point1->lon, $point2->lon) < $user_point->lon && $user_point->lon <= max($point1->lon, $point2->lon) && $user_point->lat <= max($point1->lat, $point2->lat) && $point1->lon != $point2->lon) {
$crossing = (($user_point->lon - $point1->lon) * ($point2->lat - $point1->lat) / ($point2->lon - $point1->lon)) + $point1->lat;
if ($user_point->lat <= $crossing || $point1->lat == $point2->lat) {
$intersections++;
}
}
}
// Modulo, if intersections (starts from value 0) is even it will be inside of our polygon map
if ($intersections % 2 == 0) {
return FALSE;
} else {
return TRUE;
}
}
// Performance testing function
function performance_test($count, $user_point, $polygon_map) {
$results = Array();
// Check time before calculations
$before = microtime(true);
for ($i=0 ; $i<$count ; $i++) {
seek_point($user_point, $polygon_map);
}
// Check time again after calculations
$after = microtime(true);
// Calculate all seeks and one seek per second
$results['count'] = $count;
$results['one_seek'] = (($after-$before)/$count)." sec / seek";
$results['all_seeks'] = $after-$before." sec";
// Return results on array
return $results;
}
?>