Optimize polygon

This commit is contained in:
Dennis Eichhorn 2017-11-16 11:11:48 +01:00
parent 2be03dbb3e
commit 157c6a04ce

View File

@ -44,77 +44,14 @@ class Polygon implements D2ShapeInterface
*/ */
private $coord = []; 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. * Constructor.
* *
* @since 1.0.0 * @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 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; 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? * Is point on vertex?
* *
@ -225,47 +150,23 @@ class Polygon implements D2ShapeInterface
} }
/** /**
* Set polygon coordinates. * Get interior angle sum
* *
* @param array[] $coord Coordinates * @return int
*
* @return void
* *
* @since 1.0.0 * @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 public function getInteriorAngleSum() : int
{ {
$this->interiorAngleSum = (count($this->coord) - 2) * 180; return (count($this->coord) - 2) * 180;
return $this->interiorAngleSum;
} }
/** /**
* {@inheritdoc} * Get exterior angle sum
*
* @return int
*
* @since 1.0.0
*/ */
public function getExteriorAngleSum() public function getExteriorAngleSum()
{ {
@ -273,29 +174,24 @@ class Polygon implements D2ShapeInterface
} }
/** /**
* {@inheritdoc} * Get surface area
*/ *
public function getInteriorAngleSumFormula() * @return float
{ *
return ''; * @since 1.0.0
}
/**
* {@inheritdoc}
*/
public function getExteriorAngleSumFormula()
{
return '';
}
/**
* {@inheritdoc}
*/ */
public function getSurface() : float public function getSurface() : float
{ {
return abs($this->getSignedSurface()); return abs($this->getSignedSurface());
} }
/**
* Get signed surface area
*
* @return float
*
* @since 1.0.0
*/
private function getSignedSurface() : float private function getSignedSurface() : float
{ {
$count = count($this->coord); $count = count($this->coord);
@ -313,40 +209,11 @@ class Polygon implements D2ShapeInterface
} }
/** /**
* {@inheritdoc} * Get perimeter
*/ *
public function setSurface($surface) /* : void */ * @return float
{ *
$this->reset(); * @since 1.0.0
$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}
*/ */
public function getPerimeter() : float 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 */ public function getBarycenter() : array
{
$this->reset();
$this->perimeter = $perimeter;
}
/**
* {@inheritdoc}
*/
public function getPerimeterFormula()
{
return '';
}
/**
* {@inheritdoc}
*/
public function getBarycenter()
{ {
$this->barycenter['x'] = 0; $this->barycenter['x'] = 0;
$this->barycenter['y'] = 0; $this->barycenter['y'] = 0;