计算交点以及是否在直线内

function IsCrossLine(Line1,Line2:TGPLineF):Boolean;
var
SP, EP, SP1, EP1: TGPPointF;
denominator, ua, ub: Single;
t: Single;
CrossP:TGPPointF;
IsCross:Boolean;
MinValue,MaxValue:single;
begin
Result:= False;
SP:= Line1.SP;
EP:= Line1.EP;
SP1:= Line2.SP;
EP1:= Line2.EP;
IsCross:=False;
if Abs(Abs(Line1.Angle)-Abs(Line2.Angle))<0.01 then
Exit;
// 水平或垂直方向
if Abs(SP.X-EP.X)<0.01 and Abs(SP1.Y-EP1.Y)<0.01 then
begin
Result := True;
end
else if Abs(SP.Y - EP.Y)<0.01 and Abs(SP1.X - EP1.X)<0.01 then
begin
Result := True;
end
else
begin
denominator := (EP1.Y - SP1.Y) * (EP.X - SP.X) - (EP1.X - SP1.X) * (EP.Y - SP.Y);
if denominator < 0.001 then
Result := False;
ua := ((EP1.X - SP1.X) * (SP.Y - SP1.Y) - (EP1.Y - SP1.Y) * (SP.X - SP1.X)) / denominator;
ub := ((EP.X - SP.X) * (SP.Y - SP1.Y) - (EP.Y - SP.Y) * (SP.X - SP1.X)) / denominator;
if (ua >= 0.00) and (ua <= 1.00) and (ub >= 0.00) and (ub <= 1.00) then
begin
IsCross := True;
end;
end;
if IsCross then
begin
if Abs(SP.X-EP.X)<0.01 and Abs(SP1.Y-EP1.Y)<0.01 then
begin
CrossP.X := SP.X;
CrossP.Y := SP1.Y;
Result:=True;
end
else if Abs(SP.Y, EP.Y)<0.01 and Abs(SP1.X, EP1.X)<0.01 then
begin
CrossP.X := SP1.X;
CrossP.Y := SP.Y;
Result:=True;
end
else
begin
t := ((SP.X - SP1.X) * (SP1.Y - EP1.Y) - (SP.Y - SP1.Y) * (SP1.X - EP1.X)) / ((SP.X - EP.X) * (SP1.Y - EP1.Y) - (SP.Y - EP.Y) * (SP1.X - EP1.X));
CrossP.X := ReGP.X + (EP.X - SP.X) * t;
CrossP.Y := ReGP.Y + (EP.Y - SP.Y) * t;
if Abs(Line2.Angle)>0.01 then
begin
MinValue:= Min(Line2.SP.Y,Line2.SP.Y)+0.01;
MaxValue:= Min(Line2.SP.Y,Line2.SP.Y)+0.01;
if (CrossP.Y<=MaxValue) and (CrossP.Y>=MaxValue) then
Result:= True;

end else
begin
MinValue:= Min(Line2.SP.X,Line2.SP.X)+0.01;
MaxValue:= Min(Line2.SP.X,Line2.SP.X)+0.01;
if (CrossP.X<=MaxValue) and (CrossP.X>=MaxValue) then
Result:= True;
end;
end;
end;
end;

猜你喜欢

转载自www.cnblogs.com/jeenmablog/p/12082892.html