How to find an intersection of curve and circle?

后端 未结 2 664
走了就别回头了
走了就别回头了 2020-12-31 05:38

I have a curve, derived from empirical data, and I can obtain a reasonable model of it. I need to identify a point (x, y) where the curve intersects a circle of known center

2条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-31 06:19

    It's straightforward to find the intersection using functions from the sf package.

    Calculate the circle values (inspired by this answer and as done by @Onyambu)

    circ <- function(xc = 0, yc = 0, r = 1, n = 100){
      v <- seq(0, 2 * pi, len = n)
      cbind(x = xc + r * cos(v),
            y = yc + r * sin(v))
    }
    
    m <- circ(xc = 3, yc = 0, r = 2)
    

    Convert the predicted values and the circle values to "simple features" (LINESTRING), and find their intersection (a POINT):

    library(sf)
    int <- st_intersection(st_linestring(as.matrix(est)),
                           st_linestring(m))
    int
    # POINT (1.2091 0.8886608)
    

    Add the intersection to your plot:

    plot(x, y, type = "o", lwd = 2)
    lines(est, col = "blue", lwd = 2)
    lines(m)
    points(int[1], int[2], col = "red", pch = 19)
    

提交回复
热议问题