在earlier answered question中,我曾询问如何找到由(x1,y1),(x2,y2)
定义的线段与无限线之间的交点,我在该线上有一个点,其斜率或角度为度。
一个答案建议使用参数线方程来找到两条无限线之间的交点,然后如果交点落在给定线段上则解析。我很喜欢这种技术并随之而来。
以下是答案建议将我的无限线转换为参数形式:
dx = Cos(slope)
dy = Sin(Slope)
x = x0 + t * dx
y = y0 + t * dy
我想知道两件事:
在我的实现中(使用glsl
)我看到意外的结果,因为线的斜率接近完全垂直度。
截至目前,这是我的glsl
实施,除了this one之外还基于已经链接的答案:
bool lineIntersection (out vec2 intersect, in vec2 point, in float slope, in vec2 pA, in vec2 pB) {
// See: https://gamedev.stackexchange.com/questions/44720/line-intersection-from-parametric-equation
// https://stackoverflow.com/questions/41687083/formula-to-determine-if-an-infinite-line-and-a-line-segment-intersect/41687904#41687904
bool isIntersecting = false;
float dx = cos(slope);
float dy = sin(slope);
float dxx = pB.x - pA.x;
float dyy = pB.y - pA.y;
float denominator = ((dxx * dy) - (dyy * dx));
if (denominator == 0.0) {
// Lines are parallel
return isIntersecting;
}
float u = ((dx * (pA.y - point.y)) + (dy * (point.x - pA.x))) / denominator;
if (u >= 0 && u <= 1) {
// Intersection occured on line segment
isIntersecting = true;
intersect = pA + (u * vec2(dxx, dyy));
}
return isIntersecting;
}
答案 0 :(得分:0)
我认为slope
是角度。有时slope
被理解为等式k
的系数y = a + k * x
- 在这种情况下,k
是角度的正切。而且这种方程式并不普遍 - 它不适用于垂直线。
通常三角函数的工作角度为弧度,而不是度。如果第一段不是您的问题,请检查弧度/度数。
答案 1 :(得分:0)
查找
这是否正确(具体使用cos和sin查找dx和dy 分别对坡度?)
将一条线视为笛卡尔平面中的向量,向量由 2点构造,为简单起见,将(x1,y1)
想象为原点/初始点 (0,0)
而(x2,y2)
只是 (x,y)
为了更好看:
其中:
|v|
是矢量/线的幅度(标量值)。这可能是相对的,在物理上它可以是力(2N,3N等),或者它可以只是距离函数sqrt(x^2+y^2)
Ø
是矢量/线的方向,可以是度或弧度 如果你再次看到它实际上看起来像三角形。
根据三角法
cos Ø
= 相邻边长/斜边长度 = b / a sin Ø
= 相反长度/斜边长度 = c / a 让我们从cosØ开始:
cos Ø
= 相邻边长/斜边长度 = b / a
相邻边长是 x轴长度因此:
cos Ø
= x轴长度 / 斜边长度
因此
x轴长度 = 斜边长度 * cos Ø
如果我们要将其应用于上面第二张图片的矢量组件,你会得到:
cos Ø
= |v| cos Ø
如果您将之前的步骤应用于 y轴,您将获得
sin Ø
= |v| sin Ø
因此
(x,y)
=(x-axis length
,y-axis length
)=(|v| cosØ
,|v| sinØ
)
在你的代码中写成:
(dx,dy)
=(Cos
(斜率),Sin
(斜率))