Learn R Programming

berryFunctions (version 1.4)

distance: calculate distance between points

Description

calculate distance between points on planar surface

Usage

distance(xpt, ypt, xref, yref)

Arguments

xpt
vector with x-coordinate(s) of point(s)
ypt
ditto for y
xref
single x coordinate of reference point
yref
ditto for y

Value

  • vector with the distances

Details

The function is quite simple: sqrt((xref - xpt)^2 + (yref - ypt)^2)

See Also

nndist in the package spatstat for distance to nearest neighbour

Examples

Run this code
A <- c(3,  9,-1)
B <- c(7, -2, 4)
plot(A,B)
text(A,B, paste0("P",1:3), adj=1.1)
points(3,5, col=2, pch=16)
segments(3,5, A,B)
distance(A,B, 3,5)
text(c(3.2,6,1), c(6,1,4), round(distance(A,B, 3,5),2) )

Run the code above in your browser using DataLab