两条线段是否相交,计算交点公式
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