function legacy_gmap_polyutil_point_line_dist in GMap Module 7.2
Distance between a point and a line segment.
Parameters
array $q: Point to measure.
array $p1: Start point of line segment.
array $p2: End point of line segment.
Return value
float poliutil_dist.
1 call to legacy_gmap_polyutil_point_line_dist()
- legacy_gmap_polyutil_dp_encode in tests/
inc/ gmap_polyutil.inc - Implementation of the Douglas-Peucker polyline simplification algorithm.
File
- tests/
inc/ gmap_polyutil.inc, line 88 - Encoded polyline utilities.
Namespace
tests\incCode
function legacy_gmap_polyutil_point_line_dist($q, $p1, $p2) {
if ($p1[0] == $p2[0] && $p1[1] == $p2[1]) {
// lp1 and lp2 are the same point--they don't define a line--so we return
// the distance between two points.
return legacy_gmap_polyutil_dist($q, $p1);
}
// Use the dot product to find where q lies with respect to the line segment
// p1p2. For more information, see:
// http://local.wasp.uwa.edu.au/~pbourke/geometry/pointline/
// http://www.codeguru.com/forum/printthread.php?t=194400
$u = (($p2[1] - $p1[1]) * ($q[1] - $p1[1]) + ($p2[0] - $p1[0]) * ($q[0] - $p1[0])) / (pow($p2[1] - $p1[1], 2) + pow($p2[0] - $p1[0], 2));
// Point is not alongside segment, it is further off in $p1's direction.
if ($u <= 0) {
return legacy_gmap_polyutil_dist($q, $p1);
}
elseif ($u >= 1) {
return legacy_gmap_polyutil_dist($q, $p2);
}
else {
// Point is alongside segment
// calculate distance between q and the nearest point on the line segment
// use $u to calculate the nearest point on the line segment:
// p1 + u*(p2 - p1) => [p1x + u*(p2x - p1x), p1y + u*(p2y - p1y)].
return legacy_gmap_polyutil_dist($q, array(
$p1[0] + $u * ($p2[0] - $p1[0]),
$p1[1] + $u * ($p2[1] - $p1[1]),
));
}
}