Point::anglePolar PHP Method

anglePolar() static public method

* Polar angle difference between $pointA and $pointC, $pointB is the origin, $pointA is 0 angle
static public anglePolar ( $pointA, $pointB, $pointC )
    static function anglePolar($pointA, $pointB, $pointC)
    {
        if (!isset(self::$anglePolarTable[$pointA->guid . ';' . $pointB->guid . ';' . $pointC->guid])) {
            $Ax = $pointA->x - $pointB->x;
            $Ay = $pointA->y - $pointB->y;
            $Cx = $pointC->x - $pointB->x;
            $Cy = $pointC->y - $pointB->y;
            if ($Ax <= 0 && pow($Ay, 2) == 0) {
                $Apolar = 180;
            } else {
                $Apolar = rad2deg(2 * atan($Ay / ($Ax + sqrt(pow($Ax, 2) + pow($Ay, 2)))));
            }
            if ($Cx + sqrt(pow($Cx, 2) + pow($Cy, 2)) == 0) {
                $Cpolar = 180;
            } else {
                $Cpolar = rad2deg(2 * atan($Cy / ($Cx + sqrt(pow($Cx, 2) + pow($Cy, 2)))));
            }
            $result = $Cpolar - $Apolar;
            if ($result < 0) {
                $result += 360;
            }
            self::$anglePolarTable[$pointA->guid . ';' . $pointB->guid . ';' . $pointC->guid] = $result;
        }
        return self::$anglePolarTable[$pointA->guid . ';' . $pointB->guid . ';' . $pointC->guid];
    }

Usage Example

 function polygon_finder_recursive(Graph $graph, $currentPolygon, $currentVertex, $lastVertex)
 {
     // Interdiction path table
     // Struct : array[ pointGuid1 ][ pointGuid2 ]
     static $pathTable = array();
     $polygons = array();
     $return = false;
     $vertices = $graph->vertices;
     $edges = $graph->edges;
     if (is_null($lastVertex) || !isset($pathTable[$lastVertex->guid][$currentVertex->guid])) {
         // The path loops = area found
         if (in_array($currentVertex, $currentPolygon)) {
             // Working backward to find the closure point, exclude non-area included vertices
             $polygon = new Polygon();
             do {
                 $newPoint = array_pop($currentPolygon);
                 $polygon->addPoint($newPoint);
             } while ($currentVertex != $newPoint);
             $currentPolygon = $polygon;
             // If the polygon area doesn't include the central point
             if ($polygon->includes(reset($vertices)) !== 1) {
                 // Update the interdiction table
                 $j = count($currentPolygon) - 1;
                 for ($k = 0; $k < count($currentPolygon); $k++) {
                     //$pathTable[ $currentPolygon[ $j ]->guid ][ $currentPolygon[ $k ]->guid ] = true;
                     $pathTable[$currentPolygon[$k]->guid][$currentPolygon[$j]->guid] = true;
                     $j++;
                     if ($j == count($currentPolygon)) {
                         $j = 0;
                     }
                 }
                 $return = $currentPolygon;
             }
         } else {
             $currentPolygon[] = $currentVertex;
             if (is_null($lastVertex)) {
                 // First point : we search every line from the point
                 foreach (array_keys($edges[$currentVertex->guid]) as $guid) {
                     $polygon = polygon_finder_recursive($graph, $currentPolygon, $vertices[$guid], $currentVertex);
                     if ($polygon !== false) {
                         $polygonList[] = $polygon;
                     }
                     $return = $polygonList;
                 }
             } else {
                 // Existing line : we follow the first available path with the smallest angle
                 $angleList = array();
                 foreach (array_keys($edges[$currentVertex->guid]) as $guid) {
                     // Stop condition : already passed through here in this direction
                     if ($lastVertex->guid != $guid && !isset($pathTable[$currentVertex->guid][$vertices[$guid]->guid])) {
                         $angleList[$guid] = Point::anglePolar($lastVertex, $currentVertex, $vertices[$guid]);
                     }
                 }
                 asort($angleList);
                 list($guid, $angle) = each($angleList);
                 if (!is_null($guid)) {
                     $return = polygon_finder_recursive($graph, $currentPolygon, $vertices[$guid], $currentVertex);
                 }
             }
         }
     }
     return $return;
 }