这是对我最后一个问题的某种跟进。我需要找到一种算法来检测线交叉/重叠。
以下代码在大多数情况下运行良好,但不能正确处理重合行:
public static function checkLinesIntersection(p_a:Point,p_b:Point,p_c:Point,p_d:Point):Point
{
// Denominator
var d:Number = (p_d.y - p_c.y) * (p_b.x - p_a.x) - (p_d.x - p_c.x) * (p_b.y - p_a.y);
//
var n_a:Number = (p_d.x - p_c.x) * (p_a.y - p_c.y) - (p_d.y - p_c.y) * (p_a.x - p_c.x);
var n_b:Number = (p_b.x - p_a.x) * (p_a.y - p_c.y) - (p_b.y - p_a.y) * (p_a.x - p_c.x);
var ua:Number = n_a / d;
var ub:Number = n_b / d;
var p_intersection:Point = new Point();
//coincidental but also happenes when lines are just aligned on the same plane
if(d + n_a + n_b == 0){
//how can i find if the lines really coincide and are not just in alignment, or parallel?
};
if ((ua >= 0 && ua <= 1 && ub >= 0 && ub <= 1))
{
p_intersection.x = p_a.x + (ua * (p_b.x - p_a.x));
p_intersection.y = p_a.y + (ua * (p_b.y - p_a.y));
return p_intersection;
}
return null;
}
如何改进代码,以便仅将重合线而非平行线评估为交点?
多谢!