刚给你写了个函数,来判断两线段是否相交的,(使用delphi中的坐标系),测了一下还能行。你试试看吧,参数传对就能返回正确结果(四个坐标点中任意两个相同的情况没判断,小bug :))
function TForm1.GetIntersectPoint(StartPoint1, EndPoint1, StartPoint2, EndPoint2 : TPoint) : Boolean;
function GetLineLength(Point1, Point2 : TPoint) : Double;
begin
Result := SQRT(SQR(Point2.X - Point1.X) + SQR(Point2.Y - Point1.Y));
end;
function ReturnPointLineOrNot(tmpPoint, PointS, PointE : TPoint) : Boolean;
begin
Result := (GetLineLength(tmpPoint, PointS) + GetLineLength(tmpPoint, PointE) - GetLineLength(PointS, PointE)) <= 1;
end;
var
Length1, Length2, fRate : Double;
i : integer;
tmpPoint : TPoint;
begin
Result := False;
Length1 := GetLineLength(StartPoint1, EndPoint1);
Length2 := GetLineLength(StartPoint2, EndPoint2);
if (Length1 > Length2) then
begin
if StartPoint2.X < EndPoint2.X then
for i := Integer(StartPoint2.X) to Integer(EndPoint2.X) do
begin
tmpPoint.X := i;
tmpPoint.Y := Abs(i * Round((StartPoint2.Y - EndPoint2.Y) / (StartPoint2.X - EndPoint2.X)));
if ReturnPointLineOrNot(tmpPoint, StartPoint1, EndPoint1) then
begin
Result := True;
Exit;
end;
end
else
for i := Integer(StartPoint2.X) to Integer(EndPoint2.X) do
begin
tmpPoint.X := i;
tmpPoint.Y := Abs(i * Round((StartPoint2.Y - EndPoint2.Y) / (StartPoint2.X - EndPoint2.X)));
if ReturnPointLineOrNot(tmpPoint, StartPoint1, EndPoint1) then
begin
Result := True;
Exit;
end;
end;
end
else
begin
if StartPoint1.X < EndPoint1.X then
for i := Integer(StartPoint1.X) to Integer(EndPoint1.X) do
begin
tmpPoint.X := i;
tmpPoint.Y := Abs(i * Round((StartPoint1.Y - EndPoint1.Y) / (StartPoint1.X - EndPoint1.X)));
if ReturnPointLineOrNot(tmpPoint, StartPoint2, EndPoint2) then
begin
Result := True;
Exit;
end;
end
else
for i := Integer(StartPoint1.X) to Integer(EndPoint1.X) do
begin
tmpPoint.X := i;
tmpPoint.Y := Abs(i * Round((StartPoint1.Y - EndPoint2.Y) / (StartPoint1.X - EndPoint2.X)));
if ReturnPointLineOrNot(tmpPoint, StartPoint2, EndPoint2) then
begin
Result := True;
Exit;
end;
end;
end;
end;