我使用这个代码被张贴作为回答这个问题:How do you detect where two line segments intersect?确定是否两条线段的公差相交
这是我的理解是这个函数只返回一个交叉点,如果两条线段完全相交。我需要修改此函数以包含一个公差,以便在线段近似相交(即在0.01范围内)时返回交点。我不太了解支撑这个功能的数学,所以我希望有人能够提供帮助。
由于
// Returns 1 if the lines intersect, otherwise 0. In addition, if the lines
// intersect the intersection point may be stored in the floats i_x and i_y.
char get_line_intersection(float p0_x, float p0_y, float p1_x, float p1_y,
float p2_x, float p2_y, float p3_x, float p3_y, float *i_x, float *i_y)
{
float s1_x, s1_y, s2_x, s2_y;
s1_x = p1_x - p0_x; s1_y = p1_y - p0_y;
s2_x = p3_x - p2_x; s2_y = p3_y - p2_y;
float s, t;
s = (-s1_y * (p0_x - p2_x) + s1_x * (p0_y - p2_y))/(-s2_x * s1_y + s1_x * s2_y);
t = (s2_x * (p0_y - p2_y) - s2_y * (p0_x - p2_x))/(-s2_x * s1_y + s1_x * s2_y);
if (s >= 0 && s <= 1 && t >= 0 && t <= 1)
{
// Collision detected
if (i_x != NULL)
*i_x = p0_x + (t * s1_x);
if (i_y != NULL)
*i_y = p0_y + (t * s1_y);
return 1;
}
return 0; // No collision
}
编辑:澄清,图像下面描述的那种情景的,由此两条线段将几乎相交。
Nearly intersecting lines - image
请指出,你几乎相交的意思。在二维飞机上,我不知道这应该是什么样子。 – schorsch312
也许这是interrest:https://stackoverflow.com/questions/563198/how-do-you-detect-where-two-line-segments-intersect – 4386427
在链接的问答中的参考应该有答案/ s你需要。 –