From 1c6db7780f6beb5f927e5edcaaa427c72e6586c6 Mon Sep 17 00:00:00 2001 From: Joe Rushton Date: Wed, 27 Mar 2019 15:31:23 +0000 Subject: [PATCH 1/2] Add a polygon feature and a polygon drawer --- src/Drawer/Polygon/NativePolygonDrawer.php | 54 ++++++++++++++++ src/Feature/Polygon.php | 75 ++++++++++++++++++++++ 2 files changed, 129 insertions(+) create mode 100644 src/Drawer/Polygon/NativePolygonDrawer.php create mode 100644 src/Feature/Polygon.php diff --git a/src/Drawer/Polygon/NativePolygonDrawer.php b/src/Drawer/Polygon/NativePolygonDrawer.php new file mode 100644 index 0000000..7351a23 --- /dev/null +++ b/src/Drawer/Polygon/NativePolygonDrawer.php @@ -0,0 +1,54 @@ +IsNativeFunctionAvailable = function_exists('imageantialias'); + } + + public function addPolygon(array $points) + { + $this->Polylines[] = array_map(function ($point) { + return [(int)round($point[0]), (int)round($point[1])]; + }, $points); + } + + public function drawPolygons($image) + { + $this->prepareNativeDrawing($image->getCore()); + + $color = $this->allocateColor($image->getCore()); + foreach ($this->Polylines as $points) { + $numPoints = count($points); + + $flattened = collect($points)->flatten()->toArray(); + $image->polygon($flattened, function ($draw) use ($color) { + $draw->border(1, $color); + $draw->background($color); + }); + } + } + + protected function prepareNativeDrawing($resource) + { + imagesetthickness($resource, $this->LineWidth); + imagealphablending($resource, true); + + if ($this->IsNativeFunctionAvailable) { + imageantialias($resource, true); + } + } +} diff --git a/src/Feature/Polygon.php b/src/Feature/Polygon.php new file mode 100644 index 0000000..0961c57 --- /dev/null +++ b/src/Feature/Polygon.php @@ -0,0 +1,75 @@ +LineSegments = $this->getLineSegments($coordinates); + $this->LineCallback = function (LineShape $draw) use ($lineColor) { + $draw->color($lineColor); + }; + $this->LineWidth = $lineWidth; + $this->LineColor = $this->getLineColorArray($lineColor); + } + + public function render(ImageManager $imageManager, Image $image, ViewportInterface $viewport) + { + $drawer = new NativePolygonDrawer(); + $drawer->setPainter( + $this->LineColor[0], + $this->LineColor[1], + $this->LineColor[2], + $this->LineColor[3], + $this->LineWidth + ); + + foreach ($this->LineSegments as $segment) { + $points = $this->getPoints($viewport, $segment); + + $drawer->addPolygon($points); + } + + $drawer->drawPolygons($image); + } + + private function getPoints($viewport, $segment) + { + $points = [$this->getRelativePositionForPoint($viewport, $segment[0])]; + $numPoints = count($segment); + $lastPoint = 0; + + if (1 == $numPoints) { + $numPoints = 2; + $segment[1] = $segment[0]; + } + + for ($i = 1; $i < $numPoints; ++$i) { + list($x1, $y1) = $this->getRelativePositionForPoint($viewport, $segment[$lastPoint]); + list($x2, $y2) = $this->getRelativePositionForPoint($viewport, $segment[$i]); + + if (0.0 < $this->LineSimplificationTolerance + && $i !== $numPoints - 1 + && sqrt(pow($x2 - $x1, 2.0) + pow($y2 - $y1, 2.0)) < $this->LineSimplificationTolerance) { + continue; + } + + $points[] = [$x2, $y2]; + $lastPoint = $i; + } + + return $points; + } +} From 58529e6b6ccd597b23a6d2be225cb59ec160dd10 Mon Sep 17 00:00:00 2001 From: Joe Macbook Date: Sat, 30 Mar 2019 22:10:49 +0000 Subject: [PATCH 2/2] Removed redundant code, add docblock, remove dependency on collection class --- src/Drawer/Polygon/NativePolygonDrawer.php | 4 ++-- src/Feature/Polygon.php | 24 ++++++++-------------- 2 files changed, 10 insertions(+), 18 deletions(-) diff --git a/src/Drawer/Polygon/NativePolygonDrawer.php b/src/Drawer/Polygon/NativePolygonDrawer.php index 7351a23..b4e43c8 100644 --- a/src/Drawer/Polygon/NativePolygonDrawer.php +++ b/src/Drawer/Polygon/NativePolygonDrawer.php @@ -34,8 +34,8 @@ public function drawPolygons($image) foreach ($this->Polylines as $points) { $numPoints = count($points); - $flattened = collect($points)->flatten()->toArray(); - $image->polygon($flattened, function ($draw) use ($color) { + // Flatten points into one big flat array of integers + $image->polygon(array_merge(...$points), function ($draw) use ($color) { $draw->border(1, $color); $draw->background($color); }); diff --git a/src/Feature/Polygon.php b/src/Feature/Polygon.php index 0961c57..8653ce9 100644 --- a/src/Feature/Polygon.php +++ b/src/Feature/Polygon.php @@ -10,21 +10,6 @@ class Polygon extends \Runalyze\StaticMaps\Feature\Route { - /** - * @param array $coordinates - * @param string|array $color - * @param int $lineWidth - */ - public function __construct(array $coordinates, $lineColor = '#000', int $lineWidth = 1) - { - $this->LineSegments = $this->getLineSegments($coordinates); - $this->LineCallback = function (LineShape $draw) use ($lineColor) { - $draw->color($lineColor); - }; - $this->LineWidth = $lineWidth; - $this->LineColor = $this->getLineColorArray($lineColor); - } - public function render(ImageManager $imageManager, Image $image, ViewportInterface $viewport) { $drawer = new NativePolygonDrawer(); @@ -45,7 +30,14 @@ public function render(ImageManager $imageManager, Image $image, ViewportInterfa $drawer->drawPolygons($image); } - private function getPoints($viewport, $segment) + /** + * Converts coordinates into relative x,y pixel points for drawing as a polygon + * + * @param ViewportInterface $viewport + * @param array $segment + * @return array + */ + private function getPoints(ViewportInterface $viewport, iterable $segment) { $points = [$this->getRelativePositionForPoint($viewport, $segment[0])]; $numPoints = count($segment);