Compute the distance between a point and a polygonal path. Given a point (x0,y0), and a path (x(n),y(n)), the distance to the path is the distance to the closest line segment (x(i),y(i)).
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
real(kind=wp), | intent(in) | :: | x0 | |||
real(kind=wp), | intent(in) | :: | y0 | |||
real(kind=wp), | intent(in), | dimension(n) | :: | x | ||
real(kind=wp), | intent(in), | dimension(n) | :: | y | ||
integer, | intent(in) | :: | n |
function distance_from_point_to_path(x0, y0, x, y, n) result(d) implicit none !arguments: integer,intent(in) :: n real(wp),intent(in) :: x0 real(wp),intent(in) :: y0 real(wp),dimension(n),intent(in) :: x real(wp),dimension(n),intent(in) :: y real(wp) :: d integer :: l,m,i real(wp) :: dmin !is the point inside, outside, or on the path: call locpt (x0, y0, x, y, n, l, m) select case(l) case(1,-1) !point is not on the path if (n==1) then !only one point in the path: d = norm2([x0-x(1), y0-y(1)]) else do i=1,n !loop through all line segments in the path !the distance to the path is the distance from the closest line segment if (i<n) then d = distance_from_point_to_line_segment( & [x(i),y(i)], [x(i+1),y(i+1)], [x0,y0]) else !note: if 1st /= nth point, then have to check that line segment also. if (x(1) /= x(n) .or. y(1) /= y(n)) then d = distance_from_point_to_line_segment( & [x(n),y(n)], [x(1),y(1)], [x0,y0]) end if end if !get lowest value: if (d<dmin .or. i==1) dmin = d end do end if !set sign of d: d = dmin * l ! <0 if outside the path ! >0 if inside the path case default !point is on the path d = 0.0_wp end select end function distance_from_point_to_path