と交差するかどうかの確認:How do you detect where two line segments intersect?2つの線分が、私はこの質問への答えとして投稿されました。このコードは使用してい寛容
をそれは2つの線分場合、この関数は唯一の交点を返すことに私の理解であります正確に交差する。この関数を修正して公差を含める必要があるので、線分がほぼと交差する(つまり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
}
EDIT:、画像は以下のシナリオの種類を示す明確化のためれるだろうほぼ交差する2つの線分。
Nearly intersecting lines - image
ほぼ交差していることをご指定ください。二次元の飛行機では、どのように見えるかわかりません。 – schorsch312
多分これはinterrestのです:https://stackoverflow.com/questions/563198/how-do-you-detect-where-two-line-segments-intersect – 4386427
リンクされたQ&Aの参照は答えを持っているべきです/あなたです必要。 –