A本身無限長,假設B也無限長,直接求得AB的交點座標,然後再判斷該座標是否在定長線段B的內部就可以了啊
AB本身就是兩條直線,知道兩端點就可以知道其直線方程,B也是一樣,兩個方程聯立,
得到一個座標,再看該座標是否在B的定義域內就可以啊
A的兩點爲(x1,y1),(x2,y2)
則A的直線方程爲l1:y-y1=(y2-y1)(x-x1)/(x2-x1)
B的兩點爲(x3,y3),(x4,y4)
則B的直線方程爲l2:y-y3=(y4-y3)(x-x3)/(x4-x3)
聯立解出交點座標爲的橫座標爲:
x=(k2x3-y3-k1x1+y1)/(k2-k1)
其中k1=(y2-y1)/(x2-x1)
k2=(y4-y3)/(x4-x3)
可以推導出來
x = ((x2 - x1) * (x3 - x4) * (y3 - y1) -
x3 * (x2 - x1) * (y3 - y4) + x1 * (y2 - y1) * (x3 - x4)) /
((y2 - y1) * (x3 - x4) - (x2 - x1) * (y3 - y4));
同理也可以推導出y的值:
y = ((y2 - y1) * (y3 - y4) * (x3 - x1) -
y3 * (y2 - y1) * (x3 - x4) + y1 * (x2 - x1) * (y3 - y4)) /
((y2 - y1) * (y3 - y4) - (y2 - y1) * (x3 - x4));
發現x和y的求解公式,結構相同,只要把x換成y下標不變,就是求解y值的公式,
原理部分來自 http://zhidao.baidu.com/question/191530048.html?push=ql
********************************************************************
下面附上java的實現,
前提是:a 線段1起點座標
b 線段1終點座標
c 線段2起點座標
d 線段2終點座標
import java.awt.Point;
public class AlgorithmUtil {
public static void main(String[] args) {
AlgorithmUtil.GetIntersection(new Point(1, 2), new Point(1, 2),
new Point(1, 2), new Point(1, 2));
AlgorithmUtil.GetIntersection(new Point(1, 2), new Point(1, 2),
new Point(1, 4), new Point(1, 4));
AlgorithmUtil.GetIntersection(new Point(100, 1), new Point(100, 100),
new Point(100, 101), new Point(100, 400));
AlgorithmUtil.GetIntersection(new Point(5, 5), new Point(100, 100),
new Point(100, 5), new Point(5, 100));
}
/**
* 判斷兩條線是否相交 a 線段1起點座標 b 線段1終點座標 c 線段2起點座標 d 線段2終點座標 intersection 相交點座標
* reutrn 是否相交: 0 : 兩線平行 -1 : 不平行且未相交 1 : 兩線相交
*/
private static int GetIntersection(Point a, Point b, Point c, Point d) {
Point intersection = new Point(0, 0);
if (Math.abs(b.y - a.y) + Math.abs(b.x - a.x) + Math.abs(d.y - c.y)
+ Math.abs(d.x - c.x) == 0) {
if ((c.x - a.x) + (c.y - a.y) == 0) {
System.out.println("ABCD是同一個點!");
} else {
System.out.println("AB是一個點,CD是一個點,且AC不同!");
}
return 0;
}
if (Math.abs(b.y - a.y) + Math.abs(b.x - a.x) == 0) {
if ((a.x - d.x) * (c.y - d.y) - (a.y - d.y) * (c.x - d.x) == 0) {
System.out.println("A、B是一個點,且在CD線段上!");
} else {
System.out.println("A、B是一個點,且不在CD線段上!");
}
return 0;
}
if (Math.abs(d.y - c.y) + Math.abs(d.x - c.x) == 0) {
if ((d.x - b.x) * (a.y - b.y) - (d.y - b.y) * (a.x - b.x) == 0) {
System.out.println("C、D是一個點,且在AB線段上!");
} else {
System.out.println("C、D是一個點,且不在AB線段上!");
}
return 0;
}
if ((b.y - a.y) * (c.x - d.x) - (b.x - a.x) * (c.y - d.y) == 0) {
System.out.println("線段平行,無交點!");
return 0;
}
intersection.x = ((b.x - a.x) * (c.x - d.x) * (c.y - a.y) -
c.x * (b.x - a.x) * (c.y - d.y) + a.x * (b.y - a.y) * (c.x - d.x)) /
((b.y - a.y) * (c.x - d.x) - (b.x - a.x) * (c.y - d.y));
intersection.y = ((b.y - a.y) * (c.y - d.y) * (c.x - a.x) - c.y
* (b.y - a.y) * (c.x - d.x) + a.y * (b.x - a.x) * (c.y - d.y))
/ ((b.x - a.x) * (c.y - d.y) - (b.y - a.y) * (c.x - d.x));
if ((intersection.x - a.x) * (intersection.x - b.x) <= 0
&& (intersection.x - c.x) * (intersection.x - d.x) <= 0
&& (intersection.y - a.y) * (intersection.y - b.y) <= 0
&& (intersection.y - c.y) * (intersection.y - d.y) <= 0) {
System.out.println("線段相交於點(" + intersection.x + "," + intersection.y + ")!");
return 1; // '相交
} else {
System.out.println("線段相交於虛交點(" + intersection.x + "," + intersection.y + ")!");
return -1; // '相交但不在線段上
}
}
}