如何判断两条线是否相交?(100分)

  • 主题发起人 主题发起人 nsj
  • 开始时间 开始时间
N

nsj

Unregistered / Unconfirmed
GUEST, unregistred user!
分别知道两条线的两个端点,如何判断它们是否相交?
 
死哦,<br>应该是<br>如果是xy平面的话<br>a(x1,y1)--b(x2,y2)<br>c(x3,y3)--b(x4,y4)<br>(x1-x2) /(x3-x4) &nbsp; = ( y1-y2) /(y3-y4)=平行线<br>这样行不,好久不做数学题了<br>不能用abs的<br>
 
用两直线间的最小距离
 
如果是xy平面的话<br>a(x1,y1)--b(x2,y2)<br>c(x3,y3)--d(x4,y4)<br><br>var<br>&nbsp; &nbsp; x, x1,y1,x2,y2,x3,y3,x4,y4:integer;<br>begin<br>&nbsp; &nbsp; x:=(y2-y1)*(x4-x3)-(y4-y3)*(x2-x1);<br>&nbsp; &nbsp; if (x&lt;&gt;0) then<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; x:=((x2-x1)*(x4*y3-x3*y4)-(x4-x3)*(x2*y1-x1*y2)) div x;<br>&nbsp; &nbsp; &nbsp; &nbsp; y1:=x1; y2:=x2; y3:=x3; y4:=x4;<br>&nbsp; &nbsp; &nbsp; &nbsp; if x1&gt;x2 then<br>&nbsp; &nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; y1:=x2; y2:=x1;<br>&nbsp; &nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; &nbsp; &nbsp; if x3&gt;x4 then<br>&nbsp; &nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; y3:=x4; y4:=x3;<br>&nbsp; &nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; &nbsp; &nbsp; if InRange(x, y1, y2) and InRange(x, y3, y4) then<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ShowMessage('相交');<br>&nbsp; &nbsp; end;<br>end;<br>
 
to yostgxf:<br>&nbsp; &nbsp;a,b和c.d分别是两条线的端点吗?
 
平面的还是立体的?<br>
 
测试通过,没问题。<br>测试方法:在form上放一个足够大的image,托动鼠标划线测试。<br><br>设置变量:<br>var<br>&nbsp; &nbsp; x1,y1,x2,y2,x3,y3,x4,y4:integer;<br>&nbsp; &nbsp; isMove:boolean;<br><br>procedure TForm1.FormShow(Sender: TObject);<br>begin<br>&nbsp; &nbsp; x1:=400;<br>&nbsp; &nbsp; y1:=30;<br>&nbsp; &nbsp; x2:=30;<br>&nbsp; &nbsp; y2:=400;<br>&nbsp; &nbsp; Image1.Canvas.Pen.Color:=clRed;<br>&nbsp; &nbsp; Image1.Canvas.MoveTo(x1, y1);<br>&nbsp; &nbsp; Image1.Canvas.LineTo(x2, y2);<br>end;<br><br>procedure TForm1.Image1MouseDown(Sender: TObject; Button: TMouseButton;<br>&nbsp; Shift: TShiftState; X, Y: Integer);<br>begin<br>&nbsp; &nbsp; x3:=X; y3:=Y; x4:=X; y4:=Y;<br>&nbsp; &nbsp; isMove:=true;<br>end;<br><br>procedure TForm1.Image1MouseMove(Sender: TObject; Shift: TShiftState; X,<br>&nbsp; Y: Integer);<br>begin<br>&nbsp; &nbsp; if isMove then<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; Canvas.Pen.Color:= clRed;<br>&nbsp; &nbsp; &nbsp; &nbsp; Canvas.Pen.Mode := pmNotXor;<br>&nbsp; &nbsp; &nbsp; &nbsp; Canvas.MoveTo(x3, y3);<br>&nbsp; &nbsp; &nbsp; &nbsp; Canvas.LineTo(x4, y4);<br>&nbsp; &nbsp; &nbsp; &nbsp; Canvas.MoveTo(x3, y3);<br>&nbsp; &nbsp; &nbsp; &nbsp; Canvas.LineTo(X, Y);<br>&nbsp; &nbsp; &nbsp; &nbsp; x4:=X; y4:=Y;<br>&nbsp; &nbsp; &nbsp; &nbsp; Canvas.Pen.Mode := pmCopy;<br>&nbsp; &nbsp; end;<br>end;<br><br>procedure TForm1.Image1MouseUp(Sender: TObject; Button: TMouseButton;<br>&nbsp; Shift: TShiftState; X, Y: Integer);<br>var<br>&nbsp; &nbsp; tmpX: integer;<br>begin<br>&nbsp; &nbsp; if isMove then<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; isMove:=false;<br>&nbsp; &nbsp; &nbsp; &nbsp; tmpX:=(y2-y1)*(x4-x3)-(y4-y3)*(x2-x1);<br>&nbsp; &nbsp; &nbsp; &nbsp; if (x&lt;&gt;0) then<br>&nbsp; &nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; tmpX:=((x2-x1)*(x4*y3-x3*y4)-(x4-x3)*(x2*y1-x1*y2)) div tmpX;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; y1:=x1; y2:=x2; y3:=x3; y4:=x4;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if x1&gt;x2 then<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; y1:=x2; y2:=x1;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if x3&gt;x4 then<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; y3:=x4; y4:=x3;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if InRange(tmpX, y1, y2) and InRange(tmpX, y3, y4) then<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ShowMessage('相交');<br>&nbsp; &nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; end;<br>end;<br>
 
我修改了一下,改成一个函数了<br>http://www.delphibbs.com/keylife/iblog_show.asp?xid=7195
 
后退
顶部