Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/api/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Geometry 从点到分段直线的最短距离_Geometry_Line_Distance_Point - Fatal编程技术网

Geometry 从点到分段直线的最短距离

Geometry 从点到分段直线的最短距离,geometry,line,distance,point,Geometry,Line,Distance,Point,我有一条在2D中有80个点的分段线和一个不在这条线上的点p(X/Y) 我需要知道点p'在这条线上的什么位置,它与点p的距离最短 有没有一个简单的计算方法 编辑: 输入文件: str(coords) 'data.frame': 80 obs. of 2 variables: $ x: num 2140 2162 2169 2167 2158 ... $ y: num 1466 1437 1412 1390 1369 ... str(point) 'data.frame': 1

我有一条在2D中有80个点的分段线和一个不在这条线上的点p(X/Y)

我需要知道点p'在这条线上的什么位置,它与点p的距离最短

有没有一个简单的计算方法

编辑:

输入文件:

str(coords)
'data.frame':   80 obs. of  2 variables:
 $ x: num  2140 2162 2169 2167 2158 ...
 $ y: num  1466 1437 1412 1390 1369 ...

str(point)
'data.frame':   1 obs. of  2 variables:
 $ x: num  1778
 $ y: num  1911
输出文件:

str(coords)
'data.frame':   80 obs. of  2 variables:
 $ x: num  2140 2162 2169 2167 2158 ...
 $ y: num  1466 1437 1412 1390 1369 ...

str(point)
'data.frame':   1 obs. of  2 variables:
 $ x: num  1778
 $ y: num  1911

分段线上的点

我现在有了解决方案……尽管它不是很有效。
for(j in 1:(length(coords[,1])-1)){
 d <- abs((coords[j+1,1]-coords[j,1])*(coords[j,2]-point[1,2])-
      (coords[j,1]-point[1,1])*(coords[j+1,2]-coords[j,2]))/
      sqrt((coords[j+1,1]-coords[j,1])^2+(coords[j+1,2]-coords[j,2])^2)

 t <- abs(((point[1,1]-coords[j,1])*(coords[j+1,1]-coords[j,1])+
      (point[1,2]-coords[j,2])*(coords[j+1,2]-coords[j,2]))/
      ((coords[j+1,1]-coords[j,1])^2+(coords[j+1,2]-coords[j,2])^2))
 x <- coords[j,1]+t*(coords[j+1,1]-coords[j,1])
 y <- coords[j,2]+t*(coords[j+1,2]-coords[j,2])

 if(min(coords$x[j],coords$x[j+1]) <= x && x <= max(coords$x[j],coords$x[j+1]) &&
    min(coords$y[j],coords$y[j+1]) <= y && y <= max(coords$y[j],coords$y[j+1])){
  if(coords[j,] != c(x,y) && coords[j+1,] != c(x,y)){
   distance[2*j,1] <- d
   coordinates[2*j,] <- c(x,y)
  }
 }
}
也许有人觉得这很有用。 为了得到更好的结果,我改变了P的坐标

distance <- data.frame(dist = NA)
coordinates <- data.frame(x=NA,y=NA)

coords <- data.frame(x=c(2140,2162,2169,2167,2158),y=c(1466,1437,1412,1390,1369))
point <- data.frame(x=2130,y=1400)


for(j in 1:(length(coords[,1]))){
  distance[2*j-1,1] <- sqrt((coords[j,1]-point[1,1])^2+(coords[j,2]-point[1,2])^2)
  coordinates[2*j-1,] <- coords[j,]
}

距离你知道什么会很棒吗?具有样本输入和所需输出的。对于SO来说,仅仅询问包装建议被认为是不合适的。使用标准几何图形来查找点到整条线的距离,然后如果该点不在管段上,则查找最近的管段端点。这与
R
没有任何关系。如果你发布了公式,你就更有可能知道如何将其放入代码中。