2010-06-29 77 views
0

我正在尝试编写线路交叉点代码来检测两条线是否相交。 我有东西的形式是有O对象,可以有Lo(l下标O)线,每行有2个点,每个点有一个x和一个y。 这是记录格式。pascal中的线路交叉点代码

TPoint = record 
    x,y:integer; 
    end; 
    TLine = record 
    Point : array[0..1] of TPoint; 
    Color : Tcolor; 
    end; 
    TFill = record 
    Point : TPoint; 
    Color : Tcolor; 
    end; 
    TDObject = record 
    Lines : array of TLine; 
    Fills : array of TFill; 
    Rotation : integer; 
    Position : Tpoint; 
    BoundTop,Boundleft,Boundbottom,Boundright:integer; 
    end; 

我调用Code来遍历我希望测试碰撞的两个对象的每个行组合。

Function DoCollide(obj1,obj2:Tdobject):boolean; 
var i,j:integer; 
coll:boolean; 
begin 
    coll:=false; 
    for i:=0 to length(obj1.lines) do 
    begin 
    for j:=0 to length(obj2.lines) do 
    begin 
     coll:=DoesIntersect(obj2.lines[i],obj2.lines[j])or coll; 
    end; 
    end; 
    result:=coll; 
end; 

各行测试,像这样

Function DoesIntersect(Line1,Line2:Tline):boolean; 

var 
    m1,m2,c1,c2,intersect:real; 
    v1,v2:Boolean; 
begin 
//return true if lines cross 
    // if line if verticle do not workout gradient 
    if ((line1.point[1].x)-(line1.point[0].x))=0 then 
    v1:=true // remember line 1 is verticle 
    else 
    begin 
    m1 := ((line1.point[1].y)-(line1.point[0].y))/((line1.point[1].x)-(line1.point[0].x)); 
    c1 := line1.point[0].y - (line1.point[0].x*m1); 
    end; 

    if ((line2.point[1].x)-(line2.point[0].x))=0 then 
    v2:=true // remember line 2 is verticle 
    else 
    begin 
    m2 := ((line2.point[1].y)-(line2.point[0].y))/((line2.point[1].x)-(line2.point[0].x)); 
    c2 := line2.point[0].y - (line2.point[0].x*m2); 
    end; 

    if ((NOT(m1=m2)) and (NOT(v1 or v2))) then // non parrellel and non verticle 
    begin 

     //lines cross find where 
     intersect := (c2-c1)/(m1-m2); //line intersect solved for x 
     if ((round(intersect)>= Min(line1.point[0].x,line1.point[1].x)) 
     and(round(intersect)<=max(line1.point[0].x,line1.point[1].x)) 
     and(round(intersect)>=min(line2.point[0].x,line2.point[1].x)) 
     and(round(intersect)<=max(line2.point[0].x,line2.point[1].x))) then 
     result := true 
     else 
     result := false 

    end 
    else if (v1 and v2) then // both lines are parralel 
    begin 
     // double verticle parallel exeption 
     if (((line1.Point[0].y>=min(line2.Point[0].y,line2.Point[1].y)) 
     and(line1.Point[0].y<=max(line2.Point[0].y,line2.Point[1].y))) 
     or ((line1.Point[1].y>=min(line2.Point[0].y,line2.Point[1].y)) 
     and(line1.Point[1].y<=max(line2.Point[0].y,line2.Point[1].y))) 
     or ((line2.Point[0].y>=min(line1.Point[0].y,line1.Point[1].y)) 
     and(line2.Point[0].y<=max(line1.Point[0].y,line1.Point[1].y))) 
     or ((line2.Point[1].y>=min(line1.Point[0].y,line1.Point[1].y)) 
     and(line2.Point[1].y<=max(line1.Point[0].y,line1.Point[1].y)))) then 
     result := true 
     else 
     result := false; 

    end 
    else if (v1 and not v2) then // line 1 is verticle and line 2 is not 
    begin 

     if ((((line1.Point[0].x*m2+c2)>=min(line1.Point[0].y,line1.Point[1].y)) 
     and ((line1.Point[0].x*m2+c2)<=max(line1.Point[0].y,line1.Point[1].y)))) then 
     result := true 
     else 
     result := false 
    end 
    else if (v2 and not v1) then // line 2 is verticle and line 1 is not 
    begin 

     if (((line2.Point[0].x*m1+c1)>min(line2.Point[0].y,line2.Point[1].y)) 
     and ((line2.Point[0].x*m1+c1)<max(line2.Point[0].y,line2.Point[1].y))) then 
     result := true 
     else 
     result := false 

    end 
    else if (m1=m2) then // parrellel non verticle lines 
    begin 

     if (((line1.Point[0].x>=min(line2.Point[0].x,line2.Point[1].x)) 
     and(line1.Point[0].x<=max(line2.Point[0].x,line2.Point[1].x))) 
     or ((line1.Point[1].x>=min(line2.Point[0].x,line2.Point[1].x)) 
     and(line1.Point[1].x<=max(line2.Point[0].x,line2.Point[1].x))) 
     or ((line2.Point[0].x>=min(line1.Point[0].x,line1.Point[1].x)) 
     and(line2.Point[0].x<=max(line1.Point[0].x,line1.Point[1].x))) 
     or ((line2.Point[1].x>=min(line1.Point[0].x,line1.Point[1].x)) 
     and(line2.Point[1].x<=max(line1.Point[0].x,line1.Point[1].x)))) then 
     result := true 
     else 
     result := false; 

    end; 
end; 

完成,但根据我的所有代码行总是相交.....因此,我犯了一个错误......我在一个做这个愚蠢的方式任何想法我做错了什么?

回答

1

还有更好的方法detecting whether two sets of lines intersect,但现在不用担心。

我担心你的程序运行时间足够长,以至于检测到所有事物都相交;你迭代超出数组的范围,所以你的程序应该崩溃了。始终保持范围检查启用。

如果你打算做几何,你应该确保线线段区分。在两个维度中,非平行线总是相交。即使平行线如果重合,也可以相交。你也可以得到并行的拼写垂直正确。

你的计算intersect是错误的。你需要通过差异y以划分在山坡上的差异 -intercepts:

if c1 = c2 then 
    intersect := c1 
else 
    intersect := (m1 - m2)/(c2 - c1); 

如果两条线是垂直的,那么它是不够的,检查是否他们在Ÿ坐标重叠。您还需要检查其坐标是否相等。同样,对于平行的非垂直线,您需要检查截距是否相等。

如果你修复了所有这些问题,仍然得到错误的结果,那么是时候关掉调试器了。找到一对你的函数返回的线段true for,但并不真正相交。调用这些值的函数,并使用调试器逐步完成您的功能。为了使调试更容易,您需要将这些多行的条件表达式拆分为多个中间变量,以便您可以分别检查每个变量。确定哪个计算错误,然后修复它。确保您的测试数据集包含将执行函数中每个可能的条件路径的元素。