简体   繁体   English

这个 function 如何计算从线起点到交叉点的距离?

[英]How does this function compute the distance from line start to the intersection?

I have this code in GLSL which looks like this:我在 GLSL 中有这段代码,如下所示:

float Intersect(vec2 lineOneStart, vec2 lineOneEnd, vec2 lineTwoStart, vec2 lineTwoEnd) {
  vec2 line2Perp = vec2(lineTwoEnd.y - lineTwoStart.y, lineTwoStart.x - lineTwoEnd.x);
  float line1Proj = dot(lineOneEnd - lineOneStart, line2Perp);

  if (abs(line1Proj) < 0.0001) {
    return 0.;
  }

   return dot(lineTwoStart - lineOneStart, line2Perp) / line1Proj;
}

I have drawn up a visualization of what I think this coding is doing, in the following illustration:我已经绘制了我认为这个编码正在做什么的可视化,如下图所示:

在此处输入图像描述

Hopefully that image is correct, if not please let me know.希望这张图片是正确的,如果不是,请告诉我。

Basically, I am trying to write a function that, given two line segments, returns the distance from lineOneStart to the intersection point in lineTwo .基本上,我正在尝试编写一个 function ,给定两条线段,返回从lineOneStartlineTwo交点的距离。 In reality, lineOne extends forever in its direction, but for simplicity I just gave it a very long length such that it will always reach lineTwo .实际上,lineOne 在它的方向上永远延伸,但为了简单起见,我只是给它一个很长的长度,这样它就会一直到达lineTwo

I obtained this code from elsewhere, but the problem is I don't understand how it works, or even if it is working.我从其他地方获得了这段代码,但问题是我不明白它是如何工作的,或者即使它正在工作。 I obtained the code from the demo posted at the bottom of this article我从本文底部发布的演示中获得了代码

In the article, the author describes the code as the following:在文章中,作者将代码描述如下:

...Compute the intersection point between and the line segment and the light ray at the current angle, but this is just a couple of dot products and a divide, so nothing too heavy for a modern GPU. ...计算当前角度的线段和光线之间的交点,但这只是几个点积和一个分界线,所以对于现代 GPU 来说没有什么太重的。

But it doesn't really seem to be doing that?但它似乎并没有这样做? In that the first line of the function seems to make the line go to the start of lineTwo , rather than along its angle to the closest intersection point.在 function 的第一行似乎使行 go 到lineTwo的开始,而不是沿着它的角度到最近的交点。

In addition, the dot product has always been mystifying to me, so I'm having trouble seeing how two dot products here is giving me the claimed result.此外,点积对我来说一直很神秘,所以我很难看到这里的两个点积如何给我声称的结果。 Is this code correct, and if so, how does it work?这段代码是否正确,如果正确,它是如何工作的?

If you've a endless line which is defined by a point P and a normalized direction R and a second endless line, which is defined by a point Q and a direction S , then the intersection point of the endless lines X is:如果您有一条由点P和归一化方向R定义的无限线,以及由点Q和方向S定义的第二条无限线,那么无限线X的交点是:

alpha ... angle between Q-P and R
beta  ... angle between R and S

gamma  =  180° - alpha - beta

h  =  | Q - P | * sin(alpha)
u  =  h / sin(beta)

t  = | Q - P | * sin(gamma) / sin(beta)

t  =  dot(Q-P, (S.y, -S.x)) / dot(R, (S.y, -S.x))  =  determinant(mat2(Q-P, S)) / determinant(mat2(R, S))
u  =  dot(Q-P, (R.y, -R.x)) / dot(R, (S.y, -S.x))  =  determinant(mat2(Q-P, R)) / determinant(mat2(R, S))

X  =  P + R * t  =  Q + S * u

In general The dot product of 2 vectors is equal the cosine of the angle between the 2 vectors multiplied by the magnitude (length) of both vectors.一般来说,两个向量的点积等于两个向量之间的夹角的余弦乘以两个向量的大小(长度)。

dot( A, B ) == | A | * | B | * cos( angle_A_B ) 


In your case line2Perp is (Sy, -Sx) :在你的情况下line2Perp(Sy, -Sx)

 vec2 line2Perp = vec2(lineTwoEnd.y - lineTwoStart.y, lineTwoStart.x - lineTwoEnd.x);

line1Proj is dot(R, (Sy, -Sx)) : line1Projdot(R, (Sy, -Sx))

float line1Proj = dot(lineOneEnd - lineOneStart, line2Perp);

and lineTwoStart - lineOneStart is QP :lineTwoStart - lineOneStartQP

return dot(lineTwoStart - lineOneStart, line2Perp) / line1Proj;

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

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