public function LineString::lineSegmentIntersect in geoPHP 7
Same name and namespace in other branches
- 8 geoPHP/lib/geometry/LineString.class.php \LineString::lineSegmentIntersect()
File
- geoPHP/
lib/ geometry/ LineString.class.php, line 160
Class
- LineString
- LineString. A collection of Points representing a line. A line can have more than one segment.
Code
public function lineSegmentIntersect($segment) {
$p0_x = $this
->startPoint()
->x();
$p0_y = $this
->startPoint()
->y();
$p1_x = $this
->endPoint()
->x();
$p1_y = $this
->endPoint()
->y();
$p2_x = $segment
->startPoint()
->x();
$p2_y = $segment
->startPoint()
->y();
$p3_x = $segment
->endPoint()
->x();
$p3_y = $segment
->endPoint()
->y();
$s1_x = $p1_x - $p0_x;
$s1_y = $p1_y - $p0_y;
$s2_x = $p3_x - $p2_x;
$s2_y = $p3_y - $p2_y;
$fps = -$s2_x * $s1_y + $s1_x * $s2_y;
$fpt = -$s2_x * $s1_y + $s1_x * $s2_y;
if ($fps == 0 || $fpt == 0) {
return FALSE;
}
$s = (-$s1_y * ($p0_x - $p2_x) + $s1_x * ($p0_y - $p2_y)) / $fps;
$t = ($s2_x * ($p0_y - $p2_y) - $s2_y * ($p0_x - $p2_x)) / $fpt;
if ($s > 0 && $s < 1 && $t > 0 && $t < 1) {
// Collision detected
return TRUE;
}
return FALSE;
}