/** * @return Polygon */ public function getResult() { $result = new Polygon(); $numContours = $this->countContours(); if ($numContours > 0) { /** @var PolygonNode $nPolyNode */ $nPolyNode = null; for ($polyNode = $this->topNode; $polyNode != null; $polyNode = $nPolyNode) { $nPolyNode = $polyNode->getNext(); if ($polyNode->getActive() != 0) { $polygon = $result; if ($numContours > 1) { $polygon = new Polygon(); } if ($polyNode->getProxy()->isHole()) { $polygon->setIsHole($polyNode->getProxy()->isHole()); } // --- This algorithm puts the verticies into the Polygon in // reverse order --- for ($vtx = $polyNode->getProxy()->getVertexList()[PolygonUtils::LEFT]; $vtx != null; $vtx = $vtx->getNext()) { $polygon->addVertex(new Point($vtx->getX(), $vtx->getY())); } if ($numContours > 1) { $result->addPolygon($polygon); } } } // --- Sort holes to the end of the list --- $orig = $result; $result = new Polygon(); for ($i = 0; $i < $orig->getNumInnerPoly(); $i++) { $inner = $orig->getInnerPolygon($i); if (!$inner->isHole()) { $result->addPolygon($inner); } } for ($i = 0; $i < $orig->getNumInnerPoly(); $i++) { $inner = $orig->getInnerPolygon($i); if ($inner->isHole()) { $result->addPolygon($inner); } } } return $result; }