a = c(1,5,2,6,3,6,3,5,7)
b= c(5,3,5,7,2,6,9,3,6)
plot(a,type = \"l\")
lines(b)
Are there any function to get all the intersection points of two vec
Using R's spatial facilities:
library(sp) ## Provides basic spatial classes/methods, incl. SpatialLines
library(rgeos) ## Supports topological operations, including intersection
## Read in data and wrap them up as SpatialLines objects
a = c(1,5,2,6,3,6,3,5,7)
b = c(5,3,5,7,2,6,9,3,6)
SL1 <- SpatialLines(list(Lines(Line(cbind(seq_along(a),a)), "A")))
SL2 <- SpatialLines(list(Lines(Line(cbind(seq_along(b),b)), "B")))
## Find intersections
coords <- coordinates(gIntersection(SL1, SL2))
## Check that it worked
plot(a,type = "l")
lines(b)
points(coords, col="red", pch=16)
