线段相交
方程式
1.如果
则线段平行
2.
求出u,v的值,
如果 0<=u<=1,0<=v<=1,则线段相交,否则不相交
3.根据 u或者v 可以求的交点
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| bool IsLineIntersected( lt::Vector2<float> a1, lt::Vector2<float> b1, lt::Vector2<float> a2, lt::Vector2<float> b2) { lt::Vector2<float> a1b1 = b1 - a1; lt::Vector2<float> a2b2 = b2 - a2; lt::Vector2<float> a2a1 = a1 - a2; lt::Vector2<float> a1a2 = a2 - a1; if (a1b1.x * a2b2.y - a2b2.x * a1b1.y == 0) { return false; } float u = (a2b2.x * a2a1.y - a2a1.x * a2b2.y) / (a1b1.x * a2b2.y - a2b2.x * a1b1.y); float v = (a1b1.x * a1a2.y - a1a2.x * a1b1.y) / (a2b2.x * a1b1.y - a1b1.x * a2b2.y); return u >= 0 && u <= 1 && v >= 0 && v<=1; }
lt::Vector2<float> PointLineIntersected(lt::Vector2<float> a1, lt::Vector2<float> b1, lt::Vector2<float> a2, lt::Vector2<float> b2) { lt::Vector2<float> a1b1 = b1 - a1; lt::Vector2<float> a2b2 = b2 - a2; lt::Vector2<float> a2a1 = a1 - a2; lt::Vector2<float> a1a2 = a2 - a1; float u = (a2b2.x * a2a1.y - a2a1.x * a2b2.y) / (a1b1.x * a2b2.y - a2b2.x * a1b1.y); return a1 + a1b1 * u; }
|