Become an expert in R — Interactive courses, Cheat Sheets, certificates and more!
Get Started for Free

nearestPointOnSegment

Get the nearest point on a segment to a given point


Description

This function calculates the coordinates of and the distance to the nearest point on a segment to a given point. This function does not work with geographic coordinates.

Usage

nearestPointOnSegment(s, p)

Arguments

s

A matrix representing the coordinates of the segment. The matrix has 2x2 dimension where each row represents one of the end points.

p

A vector representing the X and Y coordinates of the point.

Value

A vector with three numeric values representing X and Y coordinates of the nearest point on a segment to a given point as well as the distance between both points.

Author(s)

German Carrillo

References

The function was ported to R based on this code: http://pastebin.com/n9rUuGRh

See Also

Examples

segment = cbind(c(1,2),c(1,1.5))
point = c(1.2,1.5)
nearestPointOnSegment(segment, point)

maptools

Tools for Handling Spatial Objects

v1.1-1
GPL (>= 2)
Authors
Roger Bivand [cre, aut] (<https://orcid.org/0000-0003-2392-6140>), Nicholas Lewin-Koh [aut], Edzer Pebesma [ctb], Eric Archer [ctb], Adrian Baddeley [ctb], Nick Bearman [ctb], Hans-Jörg Bibiko [ctb], Steven Brey [ctb], Jonathan Callahan [ctb], German Carrillo [ctb], Stéphane Dray [ctb], David Forrest [ctb], Michael Friendly [ctb], Patrick Giraudoux [ctb], Duncan Golicher [ctb], Virgilio Gómez Rubio [ctb], Patrick Hausmann [ctb], Karl Ove Hufthammer [ctb], Thomas Jagger [ctb], Kent Johnson [ctb], Matthew Lewis [ctb] (<https://orcid.org/0000-0003-2244-4078>), Sebastian Luque [ctb], Don MacQueen [ctb], Andrew Niccolai [ctb], Edzer Pebesma [ctb], Oscar Perpiñán Lamigueiro [ctb], Ethan Plunkett [ctb], Ege Rubak [ctb] (<https://orcid.org/0000-0002-6675-533X>), Tom Short [ctb], Greg Snow [ctb], Ben Stabler [ctb], Murray Stokely [ctb], Rolf Turner [ctb]
Initial release
2021-03-14

We don't support your browser anymore

Please choose more modern alternatives, such as Google Chrome or Mozilla Firefox.