Advertisement
wittich

Find intersection of lines

Aug 28th, 2015
332
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.42 KB | None | 0 0
  1. <?php
  2.  
  3. // Check the direction these three points rotate
  4. // Therefore I use this awesome way @Peter Wone[1] wrote. It is really well explained by @Tom[2].
  5. // [1] http://stackoverflow.com/a/16725715/3001970
  6. // [2] http://stackoverflow.com/a/30160064/3001970
  7. function RotationDirection($p1x, $p1y, $p2x, $p2y, $p3x, $p3y) {
  8.     if ((($p3y - $p1y) * ($p2x - $p1x)) > (($p2y - $p1y) * ($p3x - $p1x))) return 1;
  9.     elseif ((($p3y - $p1y) * ($p2x - $p1x)) == (($p2y - $p1y) * ($p3x - $p1x))) return 0;
  10.     else return -1;
  11. }
  12.  
  13. // Checks if two line have a intersection
  14. function checkIntersection($p1x, $p1y, $p2x, $p2y, $p3x, $p3y, $p4x, $p4y) {
  15.    
  16.     // Determine the direction they rotate. (You can combine this all into one step.)
  17.     $face1CounterClockwise = RotationDirection($p1x, $p1y, $p2x, $p2y, $p4x, $p4y);
  18.     $face2CounterClockwise = RotationDirection($p1x, $p1y, $p2x, $p2y, $p3x, $p3y);
  19.     $face3CounterClockwise = RotationDirection($p1x, $p1y, $p3x, $p3y, $p4x, $p4y);
  20.     $face4CounterClockwise = RotationDirection($p2x, $p2y, $p3x, $p3y, $p4x, $p4y);
  21.    
  22.     // If face 1 and face 2 rotate different directions and face 3 and face 4 rotate different directions,
  23.     // then the lines intersect.
  24.     $intersect = ($face1CounterClockwise !== $face2CounterClockwise &&
  25.                   $face3CounterClockwise !== $face4CounterClockwise);
  26.    
  27.     // If lines are on top of each other.
  28.     if ($face1CounterClockwise == false && $face2CounterClockwise == false &&
  29.         $face3CounterClockwise == false && $face4CounterClockwise == false)
  30.         $intersect = true;
  31.    
  32.     return $intersect;
  33.    
  34. }
  35.  
  36. // if the lines intersect, the result contains the x and y of the intersection  
  37. // modifed from soure: jsfiddle.net/justin_c_rounds/Gd2S2/
  38. function checkLineIntersection($line1StartX, $line1StartY, $line1EndX, $line1EndY,
  39.                                $line2StartX, $line2StartY, $line2EndX, $line2EndY) {
  40.    
  41.    
  42.     $result = array ( 'x' => 0,
  43.                      'y' => 0);
  44.    
  45.     $denominator = (($line2EndY - $line2StartY) * ($line1EndX - $line1StartX)) -
  46.         (($line2EndX - $line2StartX) * ($line1EndY - $line1StartY));
  47.     if ($denominator == 0) {
  48.         return $result;
  49.     }
  50.    
  51.     $a = $line1StartY - $line2StartY;
  52.     $b = $line1StartX - $line2StartX;
  53.     $numerator1 = (($line2EndX - $line2StartX) * $a) - (($line2EndY - $line2StartY) * $b);
  54.     $numerator2 = (($line1EndX - $line1StartX) * $a) - (($line1EndY - $line1StartY) * $b);
  55.     $a = $numerator1 / $denominator;
  56.     $b = $numerator2 / $denominator;
  57.    
  58.     // if we cast these lines infinitely in both directions, they intersect here:
  59.     $result['x'] = $line1StartX + ($a * ($line1EndX - $line1StartX));
  60.     $result['y'] = $line1StartY + ($a * ($line1EndY - $line1StartY));
  61.    
  62.     $result['x'] = round($result['x'], 3);
  63.     $result['y'] = round($result['y'], 3);
  64.     return $result;
  65.    
  66. };
  67.  
  68.  
  69. // check if point x y exist in array
  70. function point_exists ($point, $points){        
  71.     $return = false;        
  72.     foreach($points as $p){            
  73.         if ($p['x'] == $point['x'] && $p['y'] == $point['y'])
  74.             $return = true;            
  75.     }        
  76.     return $return;        
  77. }
  78.  
  79.  
  80.  
  81. $list_lines = array (
  82.     array (715.341, 0, 757.297, 600),
  83.     array (0, 249.169, 800, 179.178),
  84.     array (0, 256.196, 800, 186.205),
  85.     array (0, 284.225, 800, 200.142),
  86.     array (396.716, 0, 481.041, 600),
  87.     array (0, 311.374, 800, 227.29),
  88.     array (0, 355.76, 800, 229.053),
  89.     array (0, 521.525, 800, 437.442),
  90.     array (696.134, 0, 611.809, 600)
  91. );
  92.    
  93. $line1 = 0;
  94. $line2 = 0;
  95. $points = array();
  96.  
  97. foreach ($list_lines as $f){    
  98.     $line2 = 0; // reset counter of secound loop    
  99.     foreach ($list_lines as $s){        
  100.         // only check intersection if line1 and line2 are not the same
  101.         if (implode($f) !== implode($s)) {            
  102.             // check if intersection exists
  103.             $intesect = checkIntersection($f[0], $f[1], $f[2], $f[3], $s[0], $s[1], $s[2], $s[3]);            
  104.             if ($intesect) {                
  105.                 // get point x,y
  106.                 $point = checkLineIntersection ($f[0], $f[1], $f[2], $f[3], $s[0], $s[1], $s[2], $s[3]);                
  107.                 // check if point exists  
  108.                 if (!point_exists($point, $points)) {                    
  109.                     // insert point into array and write line IDs
  110.                     array_push($point, $line1, $line2);
  111.                     array_push($points, $point);                    
  112.                 }                                  
  113.             }                    
  114.         }        
  115.         $line2++;        
  116.     }    
  117.     $line1++;      
  118. }
  119.  
  120. // output
  121. echo "<pre>";
  122. foreach ($points as $x => $point ){    
  123.     echo "Point $x. ". implode($point, ", ") ."\n";
  124. }
  125. echo "</pre>";
  126. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement