转载自:http://blog.csdn.net/shao941122/article/details/51488639
本文主要讲怎么判断两个线段是否相交
参考博客:
http://www.geeksforgeeks.org/check-if-two-given-line-segments-intersect/
http://www.cise.ufl.edu/~sitharam/COURSES/CG/kreveldintrolinesegment.pdf
http://geomalgorithms.com/a09-_intersect-3.html
http://hsfzxjy.github.io/the-simplest-way-to-find-out-if-two-segments-are-intersected/
How to check if two given line segments intersect?
Given two line segments (p1, q1) and (p2, q2), find if the given line segments intersect with each other.
Before we discuss solution, let us define notion of orientation. Orientation of an ordered triplet of points in the plane can be
–counterclockwise
–clockwise
–colinear
The following diagram shows different possible orientations of (a, b, c)
How is Orientation useful here?
Two segments (p1,q1) and (p2,q2) intersect if and only if one of the following two conditions is verified1. General Case:
– (p1, q1, p2) and (p1, q1, q2) have different orientations and
– (p2, q2, p1) and (p2, q2, q1) have different orientations.Examples:
2. Special Case
– (p1, q1, p2), (p1, q1, q2), (p2, q2, p1), and (p2, q2, q1) are all collinear and
– the x-projections of (p1, q1) and (p2, q2) intersect
– the y-projections of (p1, q1) and (p2, q2) intersectExamples:
可以使用java.awt.geom.Line2D的朋友,请直接使用系统API: intersectsLine (http://docs.oracle.com/javase/7/docs/api/java/awt/geom/Line2D.html)
不能使用系统API的小伙伴,我们只能自己造轮子了。
C++语言实现:
输出结果:
Java语言实现: