public function triangulate() { $triangles = []; $points = $this->getPoints(); // Loop until it remains a triangle while (count($points) > 3) { for ($p = 0; $p < count($points); $p++) { $a = $points[$p - 1 == -1 ? count($points) - 1 : $p - 1]; $b = $points[$p]; $c = $points[($p + 1) % count($points)]; $angle = Line::getAntiClockwiseAngleBetweenLines(new Line($a, $b), new Line($b, $c)); if ($angle == 180) { array_splice($points, $p, 1); } else { if ($angle > 180) { $triangle = (new Polygon([$a, $b, $c]))->getDetermined(); foreach ($points as $point) { if (!in_array($point, [$a, $b, $c])) { if ($triangle->isPointInside($point, true)) { continue 2; } } } array_splice($points, $p, 1); $triangles[] = $triangle; continue 2; } } } } $triangles[] = (new Polygon($points))->getDetermined(); return $triangles; }
public function isConvex() { $lines = $this->getLines(); $lines[] = $lines[0]; // to loop on every angles for ($i = 1, $imax = count($lines); $i < $imax; $i++) { $angle = Line::getAntiClockWiseAngleBetweenLines($lines[$i], $lines[$i - 1]); if (abs($angle) > 180) { return false; } } return true; }