我正在尝试编写线交叉代码来检测两条线是否相交。我有东西的形式是有 O 对象可以有 Lo(l 下标 O) 线,每条线有 2 个点,每个点有 ax 和 a y。这是记录格式。
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;
但根据我的代码,所有行总是相交.....因此我犯了一个错误......我是否以愚蠢的方式这样做有什么想法我做错了什么?