简体   繁体   English

R查找连接4个坐标的两条线的交点

[英]R Find the point of intersection of two lines connecting 4 coordinates

I apologize in advance if my code looks very amateurish. 如果我的代码看起来很业余,我事先表示歉意。 I'm trying to assign quadrants to 4 measurement stations approximately located on the edges of a town. 我正在尝试将象限分配给大约位于城镇边缘的4个测量站。

I have the coordinates of these 4 stations: 我有这四个站的坐标:

a  <- c(13.2975,52.6556)
b  <- c(14.0083,52.5583)
c  <- c(13.3722,52.3997)
d  <- c(12.7417,52.6917)

Now my idea was to create lines connecting the north-south and east-west stations: 现在我的想法是创建连接南北站和东西站的线路:

line.1 <- matrix(c(d[1],b[1],d[2],b[2]),ncol=2)
line.2 <- matrix(c(a[1],c[1],a[2],c[2]),ncol=2)

Plotting all the stations the connecting lines looks allright, however not very helpful for analyzing it on a computer. 绘制所有站点的连接线看起来不错,但是对于在计算机上进行分析不是很有帮助。

So I calculated the eucledian vectors for the two lines: 所以我计算了两行的eucledian向量:

vec.1 <- as.vector(c((b[1]-d[1]),(b[2]-d[2])))

vec.2 <- as.vector(c((c[1]-a[1]),(c[2]-a[2])))

which allowed me to calculate the angle between the two lines in degrees: 这使我能够以度为单位计算两条线之间的角度:

alpha <- acos((vec.1%*%vec.2) / (sqrt(vec.1[1]^2+vec.1[2]^2)*  
          sqrt(vec.2[1]^2+vec.2[2]^2)))) * 180/pi

The angle I get for alpha is 67.7146°. 我为alpha获得的角度是67.7146°。 This looks fairly good. 这看起来还不错。 From this angle I can easily calculate the other 3 angles of the intersection, however I need values relative to the grid so I can assign values from 0°-360° for the wind directions. 从这个角度,我可以轻松计算出交叉点的其他3个角度,但是我需要相对于网格的值,因此可以为风向分配0°-360°的值。

Now my next planned step was to find the point where the two lines intersect, add a horizontal and vertical abline through that point and then calculate the angle relative to the grid. 现在,我的下一个计划步骤是找到两条线相交的点,在该点上添加水平和垂直垂直abline ,然后计算相对于网格的角度。 However I can't find a proper example that does that and I don't think I have a nice linear equation system I could solve. 但是,我找不到合适的示例,而且我认为我没有一个可以解决的线性方程组。

Is my code way off? 我的代码有路吗? Or maybe anyone knows of a package which could help me? 也许有人知道可以帮助我的软件包? It feels like my whole approach is a bit wrong. 感觉我的整个方法有点错误。

Okay I managed to calculate the intersection point, using line equations. 好吧,我设法使用线方程来计算交点。 Here is how. 这是怎么回事。

The basic equation for two points is like this: 两点的基本公式如下:

y - y_1 = (y_2-y_1/x_2-x_1) * (x-x_1)

If you make one for each of the two lines, you can just substitute the fractions. 如果您为两行中的每一行做一个,则可以替换分数。

k.1 <- ((c[2]-a[2])/(c[1]-a[1]))
k.2 <- ((b[2]-d[2])/(b[1]-d[1]))  

Reshaping the two functions you get a final form for y : 重塑这两个函数,可以得到y的最终形式:

y <- (((-k.1/k.2)*d[2]+k.1*d[1]-k.1*c[1]+d[2])/(1-k.1/k.2))

This one you can now use to calculate the x -value: 您现在可以使用此值来计算x值:

x <- ((y-d[2])+d[1]*k.2)/k.2

In my case I get 就我而言

y = 52.62319 y = 52.62319

x = 13.3922 x = 13.3922

I'm starting to really enjoy this program! 我开始真正喜欢这个程序了!

Wikipedia has a good article on finding the intersection between two line segments with an explicit formula. Wikipedia上有一篇很好的文章,内容是使用明确的公式查找两条线段之间的交点。 However, you don't need to know the point of intersection to calculate the angle to the grid (or axes of coordinate system.) Just compute the angles from your vec.1 and vec.2 to the basis vectors: 但是,您无需知道相交点即可计算与网格(或坐标系轴)的角度。只需计算从vec.1vec.2到基本矢量的角度即可:

e1 <- c(1, 0)
e2 <- c(0, 1)

as you have done. 正如您所做的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM