2010-06-29 37 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,但並不真正相交。調用這些值的函數,並使用調試器逐步完成您的功能。爲了使調試更容易,您需要將這些多行的條件表達式拆分爲多箇中間變量,以便您可以分別檢查每個變量。確定哪個計算錯誤,然後修復它。確保您的測試數據集包含將執行函數中每個可能的條件路徑的元素。