write a c program to check line segment intersection. we have already discussed the line segment intersection theory on our previous post  http://wikistack.com/line-segment-intersection/. Now we know how to make a generic line equation ax+ by = c from given line segment.

To check whether two line segments intersect or not

  • From given line segment , extract generic line equation A1x + B1y = C1, where A1,B and C1 define the line. How to get A1,B1 and C1 from given line segment , has been discussed at http://wikistack.com/line-segment-intersection/
  • If the two lines are not parallel , they will intersect somewhere else. Say it is (x,y).
  • It is obvious that intersection point would be common for both the lines.
  • Check whether intersection point (x,y) is on both the line segments, or not.

c program to check line segment intersection

cpp program to check line segment intersection

Let us assume we have been given two line segment AB (0,2 2,0) and CD (0,0,2,2). Another input is EF(1,-3,2,-2)  and GH(3,-1,4,-2). The below program will check whether AB and CD intersect or not and same as for EF and GH.

 




Related Contents to follow