Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/polygon drawer #4

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 54 additions & 0 deletions src/Drawer/Polygon/NativePolygonDrawer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

namespace Runalyze\StaticMaps\Drawer\Polygon;

use Runalyze\StaticMaps\Drawer\PainterAwareTrait;

class NativePolygonDrawer
{
use PainterAwareTrait;

/** @var bool */
protected $IsNativeFunctionAvailable;

/** @var array [polyline_1, polyline_2, ...] with each polyline [[x, y], [x, y], ...] */
protected $Polylines = [];

public function __construct()
{
$this->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);

// 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);
});
}
}

protected function prepareNativeDrawing($resource)
{
imagesetthickness($resource, $this->LineWidth);
imagealphablending($resource, true);

if ($this->IsNativeFunctionAvailable) {
imageantialias($resource, true);
}
}
}
67 changes: 67 additions & 0 deletions src/Feature/Polygon.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php

namespace Runalyze\StaticMaps\Feature;

use Intervention\Image\Image;
use Intervention\Image\ImageManager;
use Runalyze\StaticMaps\Viewport\ViewportInterface;
use Runalyze\StaticMaps\Drawer\Polygon\NativePolygonDrawer;
use Runalyze\StaticMaps\Drawer\Polyline\NativePolylineDrawer;

class Polygon extends \Runalyze\StaticMaps\Feature\Route
{
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);
}

/**
* 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);
$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;
}
}