Skip to content

Commit

Permalink
Adding option for areas to output signed (negative) areas. Chaning ce…
Browse files Browse the repository at this point in the history
…ntroid method to use signed-areas
  • Loading branch information
phayes committed Jan 9, 2012
1 parent d8c7c1e commit 0ad1656
Showing 1 changed file with 6 additions and 4 deletions.
10 changes: 6 additions & 4 deletions lib/geometry/Polygon.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public function __construct(array $linestrings) {
}
}

public function area($exterior_only = FALSE) {
public function area($exterior_only = FALSE, $signed = FALSE) {
if ($this->geos() && $exterior_only == FALSE) {
return $this->geos()->area();
}
Expand All @@ -50,7 +50,9 @@ public function area($exterior_only = FALSE) {
$a = $a + ($p->getX() * $pts[$j]->getY()) - ($p->getY() * $pts[$j]->getX());
}

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

if ($exterior_only == TRUE) {
return $area;
}
Expand All @@ -74,7 +76,7 @@ public function centroid() {
$c = count($pts);
if((int)$c == '0') return NULL;
$cn = array('x' => '0', 'y' => '0');
$a = $this->area(TRUE);
$a = $this->area(TRUE, TRUE);

// If this is a polygon with no area. Just return the first point.
if ($a == 0) {
Expand All @@ -90,7 +92,7 @@ public function centroid() {

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

$centroid = new Point($cn['x'], $cn['y']);
return $centroid;
}
Expand Down

0 comments on commit 0ad1656

Please sign in to comment.