How to get the intersection point of two vector?

前端 未结 2 573
我在风中等你
我在风中等你 2020-12-19 04:17
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

2条回答
  •  旧时难觅i
    2020-12-19 04:41

    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)
    

    enter image description here

提交回复
热议问题