views:

29

answers:

1

Hello, i've been trying multiple functions including 2D ones to try to get this somewhat working, but no luck yet...

I have 2 line segments of latlng endpoints on earth, and i want to know if and where the 2 lines intersect.

I'm currently working with this which a physics major says should be doing the job for a 2d plane but its not. it always returns true for intersect

[code]function intersectPoint($line1start, $line1end, $line2start, $line2end) //($p0_x, $p0_y, $p1_x, $p1_y, $p2_x, $p2_y, $p3_x, $p3_y) { $p0_x = $line1start['lat']; $p0_y = $line1start['lng']; $p1_x = $line1end['lat']; $p1_y = $line1end['lng']; $p2_x = $line2start['lat']; $p2_y = $line2start['lng']; $p3_x = $line1end['lat']; $p3_y = $line1end['lng'];

$s1_x = (double) $p1_x - (double) $p0_x;
$s1_y = (double) $p1_y - (double) $p0_y;

// s1_x = p1_x - p0_x; // s1_y = p1_y - p0_y; $s2_x = (double) $p3_x - (double) $p2_x; $s2_y = (double) $p3_y - (double) $p2_y; $s3_x = (double) $p0_x - (double) $p2_x; $s3_y = (double) $p0_y - (double) $p2_y; // s2_x = p3_x - p2_x; // s2_y = p3_y - p2_y;

$s = (double) ((double)(-$s1_y * $s3_x + $s1_x * $s3_y) / (double) (-$s2_x * $s1_y + $s1_x * $s2_y));
$t = (double) ((double)( $s2_x * $s3_y - $s2_y * $s3_x) / (double) (-$s2_x * $s1_y + $s1_x * $s2_y));

// s = (-s1_y * (p0_x - p2_x) + s1_x * (p0_y - p2_y)) / (-s2_x * s1_y + s1_x * s2_y); // t = ( s2_x * (p0_y - p2_y) - s2_y * (p0_x - p2_x)) / (-s2_x * s1_y + s1_x * s2_y);

if ($s >= 0 && $s <= 1 && $t >= 0 && $t <= 1)
{
    AppCommUtility::echof(" FUNC RETURNED TRUE $s >= 0 && $s <= 1 && $t >= 0 && $t <= 1");
    // Collision detected
    return array(
        'lat' => $p0_x + ($t * $s1_x),
        'lng' => $p0_y + ($t * $s1_y)
    );
}

return null; // No collision

}[/code]

A: 

Assumption: your line segments are great circle arcs.

Any pair of distinct great circles intersects exactly twice. So, you can:

  1. Find the two points of intersection.
  2. See if the intersection points lie in your arcs.

Here is a discussion of this method.

mbeckish