线段之间的交叉点的计算 [英] Calculation of intersections between line segments

查看:435
本文介绍了线段之间的交叉点的计算的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有很多关于这里线段之间的交叉点在stackowerflow问题,这里是多一个!很抱歉,但我需要帮助,以了解如何计算交叉点。我已看过一些这里的问题,看着在其他网站上的几个例子,但我仍然感到困惑,并没有得到它!我不喜欢复制和粘贴code,而不是如何工作的。

到目前为止,我知道,我会比较喜欢斧头,AY,BX,通过,CX,CY,DX,DY各线段上的点。难道我的人解释什么我要计算,会是什么计算的结果是,如果有一个交集?

这是的例子code我见过一个。我想我不需要交叉点,只是为了知道是否该线相交不是。

 公共静态点lineIntersect(INT X1,Y1 INT,INT×2,INT Y2,INT×3,INT Y3,INT×4,INT Y4){
  双DE​​NOM =(Y4  -  Y3)*(X2  -  X1) - (X4  -  X3)*(Y2  -  Y1);
  如果(DENOM == 0.0){//线是平行的。
     返回null;
  }
  双UA =((4个 - ×3)*(Y1  -  Y3) - (Y4  -  Y3)*(X1  - ×3))/ DENOM;
  双UB =((2次 -  1次)*(Y1  -  Y3) - (Y2  -  Y1)*(X1  - ×3))/ DENOM;
    如果(UA> = 0.0&功放;&放大器; UA< = 1.0F和放大器;&安培; UB> = 0.0&功放;&安培; UB< = 1.0F){
        //获取交点。
        返回新点((int)的(X1 + UA *(×2  - ×1)),(int)的体(y1 + UA *(Y2  -  Y1)));
    }

  返回null;
  }
 

难道我还需要计算就像这个code例如一些中间值?

对于通过点线(X0,Y0)和(x1,y1)上,让XM =(X0 +​​ X1)/ 2,YM =(Y0 + Y1)/ 2(中位数线段)。
则a =(Y1-Y0)和b =(X0-X1)。
如果你评估C = A(X-XM)+ B(Y-YM),C = 0(X,Y)上线,和标志(C)告诉您哪一方的一个点上
 

解决方案

第一块code您展示基于向量跨产品,已经在这里解释<一个href="http://stackoverflow.com/questions/563198/how-do-you-detect-where-two-line-segments-intersect">How你发现,其中两条线段相交?在一个伟大的细节。

IMO,了解它的一个更简单的方法是通过求解方程系统。首先看一下线一般,然后从中切段。下面我用符号给定段((X1,X2),(Y1,Y2))((X3,X4),(Y3, Y4))

  1. 检查是否有任何的线是垂直的( X1 == X2 X3 == X4 )。

    一个。如果两者都垂直和 X1!= X3 ,再没有交集。

    乙。如果两者都垂直和 X1 == X3 ,检查(Y1,Y2) (Y3,Y4)重叠。

    ℃。如果只有一个是垂直的(例如,第一个),然后建立第二行的公式(如下所述),找到点,两线相交(代 X1 到第二线的方程),并检查是否该点是两个区段内(类似于步骤5)。

    Ð。如果没有,请继续。

  2. 使用点坐标建立线方程的形式 Y = A * X + B (如这里)。

      A1 =(Y2-Y1)/(X2-X1)
    B1 = Y1  -  A1 * X1
    A2 =(Y4-Y3)/(X4-X3)
    B2 = Y3  -  A2 * X3
     

  3. 检查线平行(相同的斜率 A )。如果是,检查他们是否有相同的拦截 B 。如果是的话,检查是否1D段(X1,X2)(X3,X4)重叠。如果是的话,你的部分做重叠。当线是平行的情况下,可以是不明确的。如果它们重叠,你可以把它作为一个路口(它甚至可以是一个点,如果它们的末端接触),还是不行。注:如果您使用的是花车这将是一个有点麻烦,我想你会想忽略这一点。如果你只有整数检查,如果 A1 = A2 等价于:

      IF((Y2,Y1)*(X4-X3)==(X2-X1)*(Y4-Y3))
     

  4. 如果线不平行。交点相当于方程系统的溶液重新presenting的两行。说真的, Y = Y =的A1 * X + B1 A2 * X + B2 相交基本上意味着这两个等式成立。解决这个系统通过均衡的两个右两侧,它会给你的交点。

    :路口(画它,你就会明白为什么)。事实上,你只需要 X 坐标

      X0 =  - (B1-B2)/(A1-A2)
     

  5. 最后一步是检查交点 X0 在于这两个领域内。也就是说,分(X1,X2)&LT; X0&LT; MAX(X1,X2)分(X3,X4)&LT; X0&LT; MAX(X3,X4)。如果是的话,你就行相交!

There's a lot of questions about intersections between line segments here at stackowerflow and here is one more! Sorry, but I need help to understand how to calculate intersections. I have read several of the questions here and looked at several examples on other websites, but I'm still confused and don't get it! I don't like to copy and paste code without how things work.

So far I know that I'm going to compare the points of each line segments like Ax, Ay, Bx, By, Cx, Cy, Dx, Dy. Could someone explain for me what I'm going to calculate, what would the result of the calculating be if there is an intersection?

This is one of the example code I seen. I guess I don't need the intersecting point, just to know if the lines intersect or not.

   public static Point lineIntersect(int x1, int y1, int x2, int y2, int x3, int y3, int x4, int y4) {
  double denom = (y4 - y3) * (x2 - x1) - (x4 - x3) * (y2 - y1);
  if (denom == 0.0) { // Lines are parallel.
     return null;
  }
  double ua = ((x4 - x3) * (y1 - y3) - (y4 - y3) * (x1 - x3))/denom;
  double ub = ((x2 - x1) * (y1 - y3) - (y2 - y1) * (x1 - x3))/denom;
    if (ua >= 0.0f && ua <= 1.0f && ub >= 0.0f && ub <= 1.0f) {
        // Get the intersection point.
        return new Point((int) (x1 + ua*(x2 - x1)), (int) (y1 + ua*(y2 - y1)));
    }

  return null;
  }

Do I also need to calculate some median value like in this code example?

For lines through points (x0,y0) and (x1,y1), let xm = (x0+x1)/2, ym = (y0+y1)/2 (median of line segment). 
Then a = (y1-y0) and b = (x0-x1). 
If you evaluate c = a(x-xm)+b(y-ym), c=0 for (x,y) on the line, and the sign(c) tells you which side a point is on

解决方案

The first piece of code you show is based on vector cross-product, which has been explained here How do you detect where two line segments intersect? in a great detail.

IMO, an easier way of understanding it is through solving a system of equations. Firstly look at lines generally and then cut segments from them. Below I use notations for given segments ((x1, x2), (y1, y2)) and ((x3, x4), (y3, y4)).

  1. Check if any of the lines is vertical (x1 == x2 or x3 == x4).

    a. If both are vertical and x1 != x3, then no intersection.

    b. If both are vertical and x1 == x3, check if (y1, y2) and (y3, y4) overlap.

    c. If only one is vertical (say, first one), then build the equation of the second line (as outlined below), find the point where two lines intersect (by substituting x1 into the equation of the second line) and check if this point is within both segments (similar to step 5).

    d. If not, proceed.

  2. Use the points coordinates to build lines equations in form y = a*x + b (like here).

    a1 = (y2-y1)/(x2-x1)
    b1 = y1 - a1*x1 
    a2 = (y4-y3)/(x4-x3)
    b2 = y3 - a2*x3
    

  3. Check if lines are parallel (same slope a). If yes, check if they have same intercept b. If yes, check if 1D segments (x1, x2) and (x3, x4) overlap. If yes, your segments do overlap. The case when lines are parallel can be ambiguous. If they overlap, you can consider it as an intersection (it even can be one point if their ends are touching), or not. Note: if you are working with floats it would be a bit trickier, I reckon you would want to ignore this. In case you have only integers checking if a1 = a2 is equivalent to:

    if((y2-y1)*(x4-x3) == (x2-x1)*(y4-y3))
    

  4. If lines are not parallel. The point of intersection is equivalent to a solution of a system of equations representing the two lines. Really, y = a1*x + b1 and y = a2*x + b2 intersecting basically means that both of these equations hold. Solve this system by equating the two right sides and it will give you the intersection point. In fact, you need only x coordinate of the intersection (draw it and you'll see why):

    x0 = -(b1-b2)/(a1-a2)
    

  5. Last step is to check if the intersection point x0 lies within both segments. That is, min(x1, x2) < x0 < max(x1, x2) and min(x3, x4) < x0 < max(x3, x4). If yes, your lines do intersect!

这篇关于线段之间的交叉点的计算的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆