Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use BCMath (where available) for all float arithmetic #115

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions geoPHP.inc
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,10 @@ class geoPHP
);
}

static function bcmathInstalled() {
return extension_loaded('bcmath');
}

static function geosInstalled($force = NULL) {
static $geos_installed = NULL;
if ($force !== NULL) $geos_installed = $force;
Expand Down
116 changes: 98 additions & 18 deletions lib/geometry/LineString.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,20 @@ public function length() {
foreach ($this->getPoints() as $delta => $point) {
$previous_point = $this->geometryN($delta);
if ($previous_point) {
$length += sqrt(pow(($previous_point->getX() - $point->getX()), 2) + pow(($previous_point->getY()- $point->getY()), 2));
if (geoPHP::bcmathInstalled()) {
$length = bcadd(
$length,
bcsqrt(
bcadd(
bcpow(bcsub($previous_point->getX(),$point->getX()), '2'),
bcpow(bcsub($previous_point->getY(), $point->getY()), '2')
)
)
);
}
else {
$length += sqrt(pow(($previous_point->getX() - $point->getX()), 2) + pow(($previous_point->getY()- $point->getY()), 2));
}
}
}
return $length;
Expand All @@ -88,17 +101,45 @@ public function greatCircleLength($radius = 6378137) {
$lon1 = deg2rad($point->getX());
$lon2 = deg2rad($next_point->getX());
$dlon = $lon2 - $lon1;
$length +=
$radius *
if (geoPHP::bcmathInstalled()) {
$length = bcadd(
$length,
bcmul(
$radius,
atan2(
bcsqrt(
bcadd(
bcpow(bcmul(cos($lat2), sin($dlon)), '2'),
bcpow(
bcsub(
bcmul(cos($lat1), sin($lat2)),
bcmul(bcmul(sin($lat1), cos($lat2)), cos($dlon))
),
'2'
)
)
),
bcadd(
bcmul(sin($lat1), sin($lat2)),
bcmul(bcmul(cos($lat1), cos($lat2)), cos($dlon))
)
)
)
);
}
else {
$length +=
$radius *
atan2(
sqrt(
pow(cos($lat2) * sin($dlon), 2) +
pow(cos($lat1) * sin($lat2) - sin($lat1) * cos($lat2) * cos($dlon), 2)
pow(cos($lat1) * sin($lat2) - sin($lat1) * cos($lat2) * cos($dlon), 2)
)
,
sin($lat1) * sin($lat2) +
cos($lat1) * cos($lat2) * cos($dlon)
cos($lat1) * cos($lat2) * cos($dlon)
);
}
}
// Returns length in meters.
return $length;
Expand All @@ -111,14 +152,36 @@ public function haversineLength() {
$point = $points[$i];
$next_point = $points[$i+1];
if (!is_object($next_point)) {continue;}
$degree = rad2deg(
acos(
sin(deg2rad($point->getY())) * sin(deg2rad($next_point->getY())) +
if (geoPHP::bcmathInstalled()) {
$degree = rad2deg(
acos(
bcadd(
bcmul(
sin(deg2rad($point->getY())),
sin(deg2rad($next_point->getY()))
),
bcmul(
bcmul(
cos(deg2rad($point->getY())),
cos(deg2rad($next_point->getY()))
),
cos(deg2rad(abs(bcsub($point->getX(), $next_point->getX()))))
)
)
)
);
$degrees = bcadd($degrees, $degree);
}
else {
$degree = rad2deg(
acos(
sin(deg2rad($point->getY())) * sin(deg2rad($next_point->getY())) +
cos(deg2rad($point->getY())) * cos(deg2rad($next_point->getY())) *
cos(deg2rad(abs($point->getX() - $next_point->getX())))
)
);
$degrees += $degree;
cos(deg2rad(abs($point->getX() - $next_point->getX())))
)
);
$degrees += $degree;
}
}
// Returns degrees
return $degrees;
Expand Down Expand Up @@ -167,18 +230,35 @@ public function lineSegmentIntersect($segment) {
$p3_x = $segment->endPoint()->x();
$p3_y = $segment->endPoint()->y();

$s1_x = $p1_x - $p0_x; $s1_y = $p1_y - $p0_y;
$s2_x = $p3_x - $p2_x; $s2_y = $p3_y - $p2_y;
if (geoPHP::bcmathInstalled()) {
$s1_x = bcsub($p1_x, $p0_x);
$s1_y = bcsub($p1_y, $p0_y);
$s2_x = bcsub($p3_x, $p2_x);
$s2_y = bcsub($p3_y, $p2_y);

$fps = (-$s2_x * $s1_y) + ($s1_x * $s2_y);
$fpt = (-$s2_x * $s1_y) + ($s1_x * $s2_y);
$fps = bcadd(bcmul(bcmul('-1', $s2_x), $s1_y), bcmul($s1_x, $s2_y));
$fpt = bcadd(bcmul(bcmul('-1', $s2_x), $s1_y), bcmul($s1_x, $s2_y));
}
else {
$s1_x = $p1_x - $p0_x; $s1_y = $p1_y - $p0_y;
$s2_x = $p3_x - $p2_x; $s2_y = $p3_y - $p2_y;

$fps = (-$s2_x * $s1_y) + ($s1_x * $s2_y);
$fpt = (-$s2_x * $s1_y) + ($s1_x * $s2_y);
}

if ($fps == 0 || $fpt == 0) {
return FALSE;
}

$s = (-$s1_y * ($p0_x - $p2_x) + $s1_x * ($p0_y - $p2_y)) / $fps;
$t = ( $s2_x * ($p0_y - $p2_y) - $s2_y * ($p0_x - $p2_x)) / $fpt;
if (geoPHP::bcmathInstalled()) {
$s = bcdiv(bcadd(bcmul(bcmul('-1', $s1_y), bcsub($p0_x, $p2_x)), bcmul($s1_x, bcsub($p0_y, $p2_y))), $fps);
$t = bcdiv(bcsub(bcmul($s2_x, bcsub($p0_y, $p2_y)), bcmul($s2_y, bcsub($p0_x, $p2_x))), $fpt);
}
else {
$s = (-$s1_y * ($p0_x - $p2_x) + $s1_x * ($p0_y - $p2_y)) / $fps;
$t = ( $s2_x * ($p0_y - $p2_y) - $s2_y * ($p0_x - $p2_x)) / $fpt;
}

if ($s > 0 && $s < 1 && $t > 0 && $t < 1) {
// Collision detected
Expand Down
63 changes: 46 additions & 17 deletions lib/geometry/Polygon.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,19 +28,30 @@ public function area($exterior_only = FALSE, $signed = FALSE) {
$a = '0';
foreach($pts as $k => $p){
$j = ($k + 1) % $c;
$a = $a + ($p->getX() * $pts[$j]->getY()) - ($p->getY() * $pts[$j]->getX());
if (geoPHP::bcmathInstalled()) {
$a = bcadd($a, bcsub(bcmul($p->getX(), $pts[$j]->getY()), bcmul($p->getY(), $pts[$j]->getX())));
if ($signed) $area = bcdiv($a, '2');
else $area = abs(bcdiv($a, '2'));
}
else {
$a += ($p->getX() * $pts[$j]->getY()) - ($p->getY() * $pts[$j]->getX());
if ($signed) $area = ($a / 2);
else $area = abs(($a / 2));
}
}

if ($signed) $area = ($a / 2);
else $area = abs(($a / 2));


if ($exterior_only == TRUE) {
return $area;
}
foreach ($this->components as $delta => $component) {
if ($delta != 0) {
$inner_poly = new Polygon(array($component));
$area -= $inner_poly->area();
if (geoPHP::bcmathInstalled()) {
$area = bcsub($area, $inner_poly->area());
}
else {
$area -= $inner_poly->area();
}
}
}
return $area;
Expand Down Expand Up @@ -68,14 +79,27 @@ public function centroid() {

foreach($pts as $k => $p){
$j = ($k + 1) % $c;
$P = ($p->getX() * $pts[$j]->getY()) - ($p->getY() * $pts[$j]->getX());
$cn['x'] = $cn['x'] + ($p->getX() + $pts[$j]->getX()) * $P;
$cn['y'] = $cn['y'] + ($p->getY() + $pts[$j]->getY()) * $P;
if (geoPHP::bcmathInstalled()) {
$P = bcsub(bcmul($p->getX(), $pts[$j]->getY()), bcmul($p->getY(), $pts[$j]->getX()));
$cn['x'] = bcadd($cn['x'], bcmul(bcadd($p->getX(), $pts[$j]->getX()), $P));
$cn['y'] = bcadd($cn['y'], bcmul(bcadd($p->getY(), $pts[$j]->getY()), $P));
}
else {
$P = ($p->getX() * $pts[$j]->getY()) - ($p->getY() * $pts[$j]->getX());
$cn['x'] = $cn['x'] + ($p->getX() + $pts[$j]->getX()) * $P;
$cn['y'] = $cn['y'] + ($p->getY() + $pts[$j]->getY()) * $P;
}
}

$cn['x'] = $cn['x'] / ( 6 * $a);
$cn['y'] = $cn['y'] / ( 6 * $a);


if (geoPHP::bcmathInstalled()) {
$cn['x'] = bcdiv($cn['x'], bcmul('6', $a));
$cn['y'] = bcdiv($cn['y'], bcmul('6', $a));
}
else {
$cn['x'] = $cn['x'] / ( 6 * $a);
$cn['y'] = $cn['y'] / ( 6 * $a);
}

$centroid = new Point($cn['x'], $cn['y']);
return $centroid;
}
Expand Down Expand Up @@ -176,10 +200,15 @@ public function pointInPolygon($point, $pointOnBoundary = true, $pointOnVertex =
&& $point->y() <= max($vertex1->y(), $vertex2->y())
&& $point->x() <= max($vertex1->x(), $vertex2->x())
&& $vertex1->y() != $vertex2->y()) {
$xinters =
($point->y() - $vertex1->y()) * ($vertex2->x() - $vertex1->x())
/ ($vertex2->y() - $vertex1->y())
+ $vertex1->x();
if (extension_loaded('bcmath')) {
$xinters = bcadd(bcdiv(bcmul(bcsub($point->y(), $vertex1->y()), bcsub($vertex2->x(), $vertex1->x())), bcsub($vertex2->y(), $vertex1->y())), $vertex1->x());
}
else {
$xinters =
($point->y() - $vertex1->y()) * ($vertex2->x() - $vertex1->x())
/ ($vertex2->y() - $vertex1->y())
+ $vertex1->x();
}
if ($xinters == $point->x()) {
// Check if point is on the polygon boundary (other than horizontal)
return $pointOnBoundary ? TRUE : FALSE;
Expand Down