Learn R Programming

pracma (version 1.7.3)

segm_distance: Segment Distance

Description

The minimum distance between a point and a segment, or the minimum distance between points of two segments.

Usage

segm_distance(p1, p2, p3, p4 = c())

Arguments

p1, p2
end points of the first segment.
p3, p4
end points of the second segment, or the point p3 alone if p4 is NULL.

Value

  • Returns a list with component l the minimum distance and components p, q the two nearest points.

    If p4=c() then point p lies on the segment and q is p4.

Details

If p4=c(), determines the orthogonal line to the segment through the single point and computes the distance to the intersection point.

Otherwise, it computes the distances of all four end points to the other segment and takes the minimum of those.

See Also

segm_intersect

Examples

Run this code
plot(c(0, 1), c(0, 1), type = "n", asp=1, 
     xlab = "", ylab = "", main = "Segment Distances")
grid()
for (i in 1:20) {
    s1 <- matrix(runif(4), 2, 2)
    s2 <- matrix(runif(4), 2, 2)
    lines(s1[, 1], s1[, 2], col = "red")
    lines(s2[, 1], s2[, 2], col = "darkred")
    S <- segm_distance(s1[1,], s1[2,], s2[1,], s2[2,])
    S$d
    points(c(S$p[1], S$q[1]), c(S$p[2], S$q[2]), pch=20, col="navy")
    lines(c(S$p[1], S$q[1]), c(S$p[2], S$q[2]), col="gray")
}

Run the code above in your browser using DataLab