diff --git a/lib/geometry/Polygon.class.php b/lib/geometry/Polygon.class.php index c502ad7..f9e789a 100644 --- a/lib/geometry/Polygon.class.php +++ b/lib/geometry/Polygon.class.php @@ -29,9 +29,14 @@ class Polygon extends Collection foreach($pts as $k => $p){ $j = ($k + 1) % $c; 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')); + $precision = 12; + $pX = number_format($p->getX(), $precision); + $pY = number_format($p->getY(), $precision); + $ptsJX = number_format($pts[$j]->getX(), $precision); + $ptsJY = number_format($pts[$j]->getY(), $precision); + + $a = bcadd($a, bcsub(bcmul($pX, $ptsJY), bcmul($pY, $ptsJX))); + $area = $signed ? bcdiv($a, '2') : abs(bcdiv($a, '2')); } else { $a += ($p->getX() * $pts[$j]->getY()) - ($p->getY() * $pts[$j]->getX()); @@ -80,9 +85,15 @@ class Polygon extends Collection foreach($pts as $k => $p){ $j = ($k + 1) % $c; 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)); + $precision = 12; + $pX = number_format($p->getX(), $precision); + $pY = number_format($p->getY(), $precision); + $ptsJX = number_format($pts[$j]->getX(), $precision); + $ptsJY = number_format($pts[$j]->getY(), $precision); + + $P = bcsub(bcmul($pX, $ptsJY), bcmul($pY, $ptsJX)); + $cn['x'] = bcadd($cn['x'], bcmul(bcadd($pX, $ptsJX), $P)); + $cn['y'] = bcadd($cn['y'], bcmul(bcadd($pY, $ptsJY), $P)); } else { $P = ($p->getX() * $pts[$j]->getY()) - ($p->getY() * $pts[$j]->getX());