Exemplo n.º 1
0
 public static function findOverlappingOfTwoLines($line1, $line2, &$overlap1, &$overlap2)
 {
     $isLine1Reversed = FALSE;
     // Check same line or not
     if (!Lines::isTwoPiecesOnSameLine($line1, $line2)) {
         return FALSE;
     }
     // Check same directions or not
     if (!Lines::areTwoVectorHaveSameDirection($line1, $line2)) {
         $line1->reverse();
         $isLine1Reversed = TRUE;
     }
     // Points
     $point1 = $line1->point1;
     $point2 = $line1->point2;
     // Line 1
     $point3 = $line2->point1;
     $point4 = $line2->point2;
     // Line 2
     // Calculate line coordinates from point1
     $x1 = 0;
     $x2 = Points::twoPointsDistance($point1, $point2);
     $x3 = Points::twoPointsDistance($point1, $point3);
     $x4 = Points::twoPointsDistance($point1, $point4);
     // Set sign to coordinates
     if (!Lines::areTwoVectorHaveSameDirection($line1, new \Classes\Utils\AbstractInstance\Line($point1, $point3))) {
         $x3 *= -1;
     }
     // Set sign to coordinates
     if (!Lines::areTwoVectorHaveSameDirection($line1, new \Classes\Utils\AbstractInstance\Line($point1, $point4))) {
         $x4 *= -1;
     }
     // Overlapping length
     $length = min($x2, $x4) - max($x1, $x3);
     // Check if therу is overlapping
     if ($length <= 0) {
         return FALSE;
     }
     // Create coordinate array
     $arrayToSort = array(0 => array('point' => 1, 'value' => $x1), 1 => array('point' => 2, 'value' => $x2), 2 => array('point' => 3, 'value' => $x3), 3 => array('point' => 4, 'value' => $x4));
     $sortedArray = Constant::sortArrayByIndex($arrayToSort, 'value');
     // Check variant of overlapping
     // 1-3-2-4
     if ($sortedArray[0]['point'] == 1 && $sortedArray[1]['point'] == 3 && $sortedArray[2]['point'] == 2 && $sortedArray[3]['point'] == 4) {
         $overlap1 = array($x3, $x2);
         $overlap2 = array(0, $x2 - $x3);
     }
     // 3-1-4-2
     if ($sortedArray[0]['point'] == 3 && $sortedArray[1]['point'] == 1 && $sortedArray[2]['point'] == 4 && $sortedArray[3]['point'] == 2) {
         $overlap1 = array(0, $x4);
         $overlap2 = array($x1 - $x3, $x4 - $x3);
     }
     // 1-3-4-2
     if ($sortedArray[0]['point'] == 1 && $sortedArray[1]['point'] == 3 && $sortedArray[2]['point'] == 4 && $sortedArray[3]['point'] == 2) {
         $overlap1 = array($x3, $x4);
         $overlap2 = array(0, $x4 - $x3);
     }
     // 3-1-2-4
     if ($sortedArray[0]['point'] == 3 && $sortedArray[1]['point'] == 1 && $sortedArray[2]['point'] == 2 && $sortedArray[3]['point'] == 4) {
         $overlap1 = array(0, $x2);
         $overlap2 = array($x1 - $x3, $x2 - $x3);
     }
     // Reverse overlapping of line1
     if ($isLine1Reversed) {
         // $x2 = Length(line1)
         $begin = $x2 - $overlap1[1];
         $end = $x2 - $overlap1[0];
         $overlap1[0] = $begin;
         $overlap1[1] = $end;
     }
     return TRUE;
 }