如果你的线是由截距和斜率而不是两个点定义的,试试这个来找到交点:
intersect <- function(l1, l2){
x <- (l2[1] - l1[1]) / (l1[2] - l2[2])
y <- l1[1] + l1[2] * x
return(xy=c(x, y))
}
# Lines defined by intercept and slope
l1 <- c(10, -2) # Y = l1[1] + l1[2] * X
l2 <- c( 0, 2) # Y = l2[1] + l2[2] * X
xy <- intersect(l1, l2) # Returns the xy coordinates of the intersection
xy
# [1] 2.5 5.0
plot(xy[1], xy[2], xlim=c(0, 6), ylim=c(0, 10), cex=3)
abline(l1[1], l1[2])
abline(l2[1], l2[2])
abline(h=5, v=2.5, lty=3)
text(1, 8.5, "Line 1", srt=-45)
text(1, 2.5, "Line 2", srt=45)