DistancePointSegment: Compute distance between a point and a segment

Description Usage Arguments Details Value Author(s) References See Also Examples

View source: R/DistancePointSegment.R

Description

Compute distance between a point and a segment

Usage

1
DistancePointSegment(px, py, x1, y1, x2, y2)

Arguments

px

x coordinate of point

py

y coordinate of point

x1

x coordinate of one end of segment

y1

y coordinate of one end of segment

x2

x coordinate of other end of segment

y2

y coordinate of other end of segment

Details

Computes the distance between a point and a segment via the shortest line. This line will be perpendicular to the segment if the point opposes the line, or it will be attached directly to an endpoint.

Value

returns a scalar value measuring the distance

Author(s)

Shane T. Mueller and Brandon Perelman

References

See Mueller et al., (2016). https://sites.google.com/a/mtu.edu/mapping/

See Also

LineMagnitude,ClosestPoint

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
##select a random point and find its closest point
##on the segment.
x1 <- runif(1)*20
y1 <- runif(1)*20

s1x <- 5;s1y <- 5
s2x <- 0;s2y <- 15

d <- DistancePointSegment(x1,y1,s1x,s1y,s2x,s2y)
plot(c(s1x,s2x),c(s1y,s2y),pch=16,xlab="x",ylab="y",
      ylim=c(-1,20),xlim=c(-1,20),type="o")
points(x1,y1,col="red",pch=16,cex=2)

p2 <-  ClosestPoint(x1,y1,s1x,s1y,s2x,s2y)
segments(p2[1],p2[2],x1,y1,lty=3)
text(10,2,paste("Distance from line to point:",
       round(d,3)))

pathmapping documentation built on May 2, 2019, 4:20 a.m.