Mega Code Archive

 
Categories / Delphi / Graphic
 

Determine if two 2D segments intersect each other

Title: Determine if two 2D segments intersect each other function Intersect(const x1,y1,x2,y2,x3,y3,x4,y4:Double):Boolean; var UpperX,UpperY : Double; LowerX,LowerY : Double; Ax,Bx,Cx : Double; Ay,By,Cy : Double; D,F,E : Double; begin Result := false; Ax := x2 - x1; Bx := x3 - x4; if Ax begin LowerX := x2; UpperX := x1; end else begin UpperX := x2; LowerX := x1; end; if Bx 0.0 then begin if (UpperX Exit; end else if (Upperx Exit; Ay := y2 - y1; By := y3 - y4; if Ay begin LowerY := y2; UpperY := y1; end else begin UpperY := y2; LowerY := y1; end; if By 0.0 then begin if (UpperY Exit; end else if (UpperY Exit; Cx := x1 - x3; Cy := y1 - y3; d := (By * Cx) - (Bx * Cy); f := (Ay * Bx) - (Ax * By); if f 0.0 then begin if (d f) then Exit; end else if (d 0.0) or (d Exit; e := (Ax * Cy) - (Ay * Cx); if f 0.0 then begin if (e f) then Exit; end else if(e 0.0) or (e Exit; Result := true; (* //Simple method, yet not so accurate for certain situations: Result := (Orientation(x1,y1,x2,y2,x3,y3) Orientation(x1,y1,x2,y2,x4,y4)) and (Orientation(x3,y3,x4,y4,x1,y1) Orientation(x3,y3,x4,y4,x2,y2)); *) end; (* End Of SegmentIntersect *)