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));
總結:
//第一條直線 double x1 = 10, y1 = 20, x2 = 100, y2 = 200; double a = (y1 - y2) / (x1 - x2); double b = (x1 * y2 - x2 * y1) / (x1 - x2); System.out.println("求出該直線方程為: y=" + a + "x + " + b); //第二條 double x3 = 50, y3 = 20, x4 = 20, y4 = 100; double c = (y3 - y4) / (x3 - x4); double d = (x3 * y4 - x4 * y3) / (x3 - x4); System.out.println("求出該直線方程為: y=" + c + "x + " + d); double x = ((x1 - x2) * (x3 * y4 - x4 * y3) - (x3 - x4) * (x1 * y2 - x2 * y1)) / ((x3 - x4) * (y1 - y2) - (x1 - x2) * (y3 - y4)); double y = ((y1 - y2) * (x3 * y4 - x4 * y3) - (x1 * y2 - x2 * y1) * (y3 - y4)) / ((y1 - y2) * (x3 - x4) - (x1 - x2) * (y3 - y4)); System.out.println("他們的交點為: (" + x + "," + y + ")");
下面附上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; // '相交但不在線段上 } } }
第二種方法: 利用斜率公式, 直線方程為ax+bx+c=0, 先求出a,b,c, 然后再求出交點
public static void main(String[] args) { Point2D p1 = new Point2D.Double(10, 20); Point2D p2 = new Point2D.Double(100, 200); Point2D p3 = new Point2D.Double(50, 20); Point2D p4 = new Point2D.Double(20, 100); Param pm1 = CalParam(p1, p2); Param pm2 = CalParam(p3, p4); Point2D rp = getIntersectPoint(pm1, pm2); System.out.println("他們的交點為: (" + rp.getX() + "," + rp.getY() + ")"); } /** * 計算兩點的直線方程的參數a,b,c * @param p1 * @param p2 * @return */ public static Param CalParam(Point2D p1, Point2D p2){ double a,b,c; double x1 = p1.getX(), y1 = p1.getY(), x2 = p2.getX(), y2 = p2.getY(); a = y2 - y1; b = x1 - x2; c = (x2 - x1) * y1 - (y2 - y1) * x1; if (b < 0) { a *= -1; b *= -1; c *= -1; }else if (b == 0 && a < 0) { a *= -1; c *= -1; } return new Param(a, b, c); } /** * 計算兩條直線的交點 * @param pm1 * @param pm2 * @return */ public static Point2D getIntersectPoint(Param pm1, Param pm2){ return getIntersectPoint(pm1.a, pm1.b, pm1.c, pm2.a, pm2.b, pm2.c); } public static Point2D getIntersectPoint(double a1, double b1, double c1, double a2, double b2, double c2){ Point2D p = null; double m = a1 * b2 - a2 * b1; if (m == 0) { return null; } double x = (c2 * b1 - c1 * b2) / m; double y = (c1 * a2 - c2 * a1) / m; p = new Point2D.Double(x, y); return p; }
輸出的結果為:
- 求出該直線方程為: y=2.0x + -0.0
- 求出該直線方程為: y=-2.6666666666666665x + 153.33333333333334
- 他們的交點為: (32.857142857142854,65.71428571428571)
- 他們的交點為: (32.857142857142854,65.71428571428571)
轉: http://263229365.iteye.com/blog/1155745