diff --git a/src/Drawer/Polygon/NativePolygonDrawer.php b/src/Drawer/Polygon/NativePolygonDrawer.php new file mode 100644 index 0000000..b4e43c8 --- /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); + + // 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); + } + } +} diff --git a/src/Feature/Polygon.php b/src/Feature/Polygon.php new file mode 100644 index 0000000..8653ce9 --- /dev/null +++ b/src/Feature/Polygon.php @@ -0,0 +1,67 @@ +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; + } +}