How can I implement the distance from a point to a line segment in GLSL?

此生再无相见时 提交于 2019-12-12 02:53:45

问题


I just can't seem to spot my bug here, can you?

bool oblong (vec2 p, vec2 a, vec2 b, float r) {
 return (((b.y-a.y)*(p.x-a.x)+(b.x-a.x)*(p.y-a.y))^2/((b.x-a.x)^2+(b.y-a.y)^2)<= r);
}

This is my second GLSL program, (my first was a circle.) Thanks for your input!


回答1:


You don't explain very well what the function is supposed to do and what your single-character variable names actually mean. I'm going to guess that a and b are the points on the line segment, and p is the point of interest. r must be some distance that the function tests against (generally, you should return the distance and let the user test against it. If they want to keep it, it's there prerogative).

I guess your real problem is that in neither C, C++, or GLSL is ^ the "raise to a power" operator.

In any case, a correct version of this function would be as follows:

float DistToLine(vec2 pt1, vec2 pt2, vec2 testPt)
{
  vec2 lineDir = pt2 - pt1;
  vec2 perpDir = vec2(lineDir.y, -lineDir.x);
  vec2 dirToPt1 = pt1 - testPt;
  return abs(dot(normalize(perpDir), dirToPt1));
}

Note that this code has not been tested. I'm just implementing the solution presented at the given site. This is implemented in vector notation with vector arithmetic. Note that I very rarely get the X and Y components (I only do it once to get the perpendicular).



来源:https://stackoverflow.com/questions/9246100/how-can-i-implement-the-distance-from-a-point-to-a-line-segment-in-glsl

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!