From 157c6a04ce767f54c0229abd10a0c57021543da7 Mon Sep 17 00:00:00 2001 From: Dennis Eichhorn Date: Thu, 16 Nov 2017 11:11:48 +0100 Subject: [PATCH] Optimize polygon --- Math/Geometry/Shape/D2/Polygon.php | 219 +++++------------------------ 1 file changed, 36 insertions(+), 183 deletions(-) diff --git a/Math/Geometry/Shape/D2/Polygon.php b/Math/Geometry/Shape/D2/Polygon.php index 12ef97e34..75fc28a04 100644 --- a/Math/Geometry/Shape/D2/Polygon.php +++ b/Math/Geometry/Shape/D2/Polygon.php @@ -44,77 +44,14 @@ class Polygon implements D2ShapeInterface */ private $coord = []; - /** - * Polygon perimeter. - * - * @var float - * @since 1.0.0 - */ - private $perimeter = 0.0; - - /** - * Polygon surface. - * - * @var float - * @since 1.0.0 - */ - private $surface = 0.0; - - /** - * Interior angle sum of the polygon. - * - * @var int - * @since 1.0.0 - */ - private $interiorAngleSum = 0; - - /** - * Exterior angle sum of the polygon. - * - * @var float - * @since 1.0.0 - */ - private $exteriorAngleSum = 0.0; - - /** - * Polygon barycenter. - * - * @var float[] - * @since 1.0.0 - */ - private $barycenter = ['x' => 0.0, 'y' => 0.0]; - - /** - * Polygon edge length. - * - * @var float - * @since 1.0.0 - */ - private $edgeLength = 0.0; - - /** - * Polygon inner length. - * - * @var float - * @since 1.0.0 - */ - private $innerLength = 0.0; - - /** - * Polygon inner edge angular. - * - * @var int - * @since 1.0.0 - */ - private $innerEdgeAngular = 0; - /** * Constructor. * * @since 1.0.0 */ - public function __construct() + public function __construct(array $coord) { + $this->coord = $coord; } /** @@ -128,7 +65,9 @@ class Polygon implements D2ShapeInterface */ public function pointInPolygon(array $point) : int { - return self::isPointInPolygon($point, $this->coord[] = $this->coord[0]); + $coord = $this->coord; + $coord[] = $this->coord[0]; + return self::isPointInPolygon($point, $coord); } /** @@ -189,20 +128,6 @@ class Polygon implements D2ShapeInterface return 1; } - /** - * Is point on vertex? - * - * @param array $point Point location - * - * @return bool - * - * @since 1.0.0 - */ - public function onVertex(array $point) : bool - { - return self::isOnVertex($point, $this->coord); - } - /** * Is point on vertex? * @@ -225,47 +150,23 @@ class Polygon implements D2ShapeInterface } /** - * Set polygon coordinates. + * Get interior angle sum * - * @param array[] $coord Coordinates - * - * @return void + * @return int * * @since 1.0.0 */ - public function setCoordinates($coord) /* : void */ - { - $this->coord = $coord; - } - - /** - * Set polygon coordinate. - * - * @param int $i Index - * @param int|float $x X coordinate - * @param int|float $y Y coordinate - * - * @return void - * - * @since 1.0.0 - */ - public function setCoordinate($i, $x, $y) /* : void */ - { - $this->coord[$i] = ['x' => $x, 'y' => $y]; - } - - /** - * {@inheritdoc} - */ public function getInteriorAngleSum() : int { - $this->interiorAngleSum = (count($this->coord) - 2) * 180; - - return $this->interiorAngleSum; + return (count($this->coord) - 2) * 180; } /** - * {@inheritdoc} + * Get exterior angle sum + * + * @return int + * + * @since 1.0.0 */ public function getExteriorAngleSum() { @@ -273,29 +174,24 @@ class Polygon implements D2ShapeInterface } /** - * {@inheritdoc} - */ - public function getInteriorAngleSumFormula() - { - return ''; - } - - /** - * {@inheritdoc} - */ - public function getExteriorAngleSumFormula() - { - return ''; - } - - /** - * {@inheritdoc} + * Get surface area + * + * @return float + * + * @since 1.0.0 */ public function getSurface() : float { return abs($this->getSignedSurface()); } + /** + * Get signed surface area + * + * @return float + * + * @since 1.0.0 + */ private function getSignedSurface() : float { $count = count($this->coord); @@ -313,40 +209,11 @@ class Polygon implements D2ShapeInterface } /** - * {@inheritdoc} - */ - public function setSurface($surface) /* : void */ - { - $this->reset(); - - $this->surface = $surface; - } - - /** - * {@inheritdoc} - */ - public function reset() /* : void */ - { - $this->coord = []; - $this->barycenter = ['x' => 0.0, 'y' => 0.0]; - $this->perimeter = 0.0; - $this->surface = 0.0; - $this->interiorAngleSum = 0; - $this->edgeLength = 0.0; - $this->innerLength = 0.0; - $this->innerEdgeAngular = 0; - } - - /** - * {@inheritdoc} - */ - public function getSurfaceFormula() - { - return ''; - } - - /** - * {@inheritdoc} + * Get perimeter + * + * @return float + * + * @since 1.0.0 */ public function getPerimeter() : float { @@ -361,27 +228,13 @@ class Polygon implements D2ShapeInterface } /** - * {@inheritdoc} + * Get barycenter + * + * @return array + * + * @since 1.0.0 */ - public function setPerimeter($perimeter) /* : void */ - { - $this->reset(); - - $this->perimeter = $perimeter; - } - - /** - * {@inheritdoc} - */ - public function getPerimeterFormula() - { - return ''; - } - - /** - * {@inheritdoc} - */ - public function getBarycenter() + public function getBarycenter() : array { $this->barycenter['x'] = 0; $this->barycenter['y'] = 0;