Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- // Check the direction these three points rotate
- // Therefore I use this awesome way @Peter Wone[1] wrote. It is really well explained by @Tom[2].
- // [1] http://stackoverflow.com/a/16725715/3001970
- // [2] http://stackoverflow.com/a/30160064/3001970
- function RotationDirection($p1x, $p1y, $p2x, $p2y, $p3x, $p3y) {
- if ((($p3y - $p1y) * ($p2x - $p1x)) > (($p2y - $p1y) * ($p3x - $p1x))) return 1;
- elseif ((($p3y - $p1y) * ($p2x - $p1x)) == (($p2y - $p1y) * ($p3x - $p1x))) return 0;
- else return -1;
- }
- // Checks if two line have a intersection
- function checkIntersection($p1x, $p1y, $p2x, $p2y, $p3x, $p3y, $p4x, $p4y) {
- // Determine the direction they rotate. (You can combine this all into one step.)
- $face1CounterClockwise = RotationDirection($p1x, $p1y, $p2x, $p2y, $p4x, $p4y);
- $face2CounterClockwise = RotationDirection($p1x, $p1y, $p2x, $p2y, $p3x, $p3y);
- $face3CounterClockwise = RotationDirection($p1x, $p1y, $p3x, $p3y, $p4x, $p4y);
- $face4CounterClockwise = RotationDirection($p2x, $p2y, $p3x, $p3y, $p4x, $p4y);
- // If face 1 and face 2 rotate different directions and face 3 and face 4 rotate different directions,
- // then the lines intersect.
- $intersect = ($face1CounterClockwise !== $face2CounterClockwise &&
- $face3CounterClockwise !== $face4CounterClockwise);
- // If lines are on top of each other.
- if ($face1CounterClockwise == false && $face2CounterClockwise == false &&
- $face3CounterClockwise == false && $face4CounterClockwise == false)
- $intersect = true;
- return $intersect;
- }
- // if the lines intersect, the result contains the x and y of the intersection
- // modifed from soure: jsfiddle.net/justin_c_rounds/Gd2S2/
- function checkLineIntersection($line1StartX, $line1StartY, $line1EndX, $line1EndY,
- $line2StartX, $line2StartY, $line2EndX, $line2EndY) {
- $result = array ( 'x' => 0,
- 'y' => 0);
- $denominator = (($line2EndY - $line2StartY) * ($line1EndX - $line1StartX)) -
- (($line2EndX - $line2StartX) * ($line1EndY - $line1StartY));
- if ($denominator == 0) {
- return $result;
- }
- $a = $line1StartY - $line2StartY;
- $b = $line1StartX - $line2StartX;
- $numerator1 = (($line2EndX - $line2StartX) * $a) - (($line2EndY - $line2StartY) * $b);
- $numerator2 = (($line1EndX - $line1StartX) * $a) - (($line1EndY - $line1StartY) * $b);
- $a = $numerator1 / $denominator;
- $b = $numerator2 / $denominator;
- // if we cast these lines infinitely in both directions, they intersect here:
- $result['x'] = $line1StartX + ($a * ($line1EndX - $line1StartX));
- $result['y'] = $line1StartY + ($a * ($line1EndY - $line1StartY));
- $result['x'] = round($result['x'], 3);
- $result['y'] = round($result['y'], 3);
- return $result;
- };
- // check if point x y exist in array
- function point_exists ($point, $points){
- $return = false;
- foreach($points as $p){
- if ($p['x'] == $point['x'] && $p['y'] == $point['y'])
- $return = true;
- }
- return $return;
- }
- $list_lines = array (
- array (715.341, 0, 757.297, 600),
- array (0, 249.169, 800, 179.178),
- array (0, 256.196, 800, 186.205),
- array (0, 284.225, 800, 200.142),
- array (396.716, 0, 481.041, 600),
- array (0, 311.374, 800, 227.29),
- array (0, 355.76, 800, 229.053),
- array (0, 521.525, 800, 437.442),
- array (696.134, 0, 611.809, 600)
- );
- $line1 = 0;
- $line2 = 0;
- $points = array();
- foreach ($list_lines as $f){
- $line2 = 0; // reset counter of secound loop
- foreach ($list_lines as $s){
- // only check intersection if line1 and line2 are not the same
- if (implode($f) !== implode($s)) {
- // check if intersection exists
- $intesect = checkIntersection($f[0], $f[1], $f[2], $f[3], $s[0], $s[1], $s[2], $s[3]);
- if ($intesect) {
- // get point x,y
- $point = checkLineIntersection ($f[0], $f[1], $f[2], $f[3], $s[0], $s[1], $s[2], $s[3]);
- // check if point exists
- if (!point_exists($point, $points)) {
- // insert point into array and write line IDs
- array_push($point, $line1, $line2);
- array_push($points, $point);
- }
- }
- }
- $line2++;
- }
- $line1++;
- }
- // output
- echo "<pre>";
- foreach ($points as $x => $point ){
- echo "Point $x. ". implode($point, ", ") ."\n";
- }
- echo "</pre>";
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement