在 R 中工作,我试图绘制流横截面,在与识别的“沿岸”点相对的交叉点插入一个点,并计算沿岸线下的面积。它是处理许多横截面的循环的一部分。我想出的最佳解决方案是使用 approx 函数,但是所有点都不完全在交点上,我无法弄清楚我做错了什么。
由于它是循环的一部分,因此很难提供示例数据,但下面的代码示例会在图像中生成结果。蓝色三角形应该位于虚“岸”线和实心横截面周线之间的交点。
###sample data
stn.sub.sort <- data.frame(dist = c(0,1.222,2.213,2.898,4.453,6.990,7.439,7.781,8.753,10.824,10.903,13.601,17.447), depth=c(-0.474,-0.633,0,-0.349,-1.047,-2.982,-2.571,-3.224,-3.100,-3.193,-2.995,-0.065,-0.112), Bankful = c(0,0,0,0,1,0,0,0,0,0,0,0,0))
###plot cross section with identified bankful
plot(stn.sub.sort$dist,
as.numeric(stn.sub.sort$depth),
type="b",
col=ifelse(stn.sub.sort$Bankful==1,"red","black"),
ylab="Depth (m)",
xlab="Station (m)",
ylim=range(stn.sub.sort$depth),
xlim=range(stn.sub.sort$dist),
main="3")
###visualize bankful line of intersection
abline(h=stn.sub.sort$depth[stn.sub.sort$Bankful==1],
lty=2,
col="black")
###approximate point at intersection
index.bf=which(stn.sub.sort$Bankful==1)
index.approx<-which(stn.sub.sort$dist>stn.sub.sort$dist[index.bf])
sbf <- approx(stn.sub.sort$depth[index.approx],
stn.sub.sort$dist[index.approx],
xout=stn.sub.sort$depth[index.bf])
###plot opposite bankful points
points(sbf$y,sbf$x,pch=2,col="blue")