Página 1
Lines in 2D
Página 2
Índice
See this article in english 
Página 3
Intersection of segments in 2D

Lo sentimos - la página solicitada no está disponible en castellano. Mostrando la versión : en

Intersection of lines in 2D

Alexander Hristov

Intersection of Lines in y=ax+b form

If you have the equations in y = ax+b format for both lines (i.e. : none of them is vertical), the lines intersect only if the slopes are different ( a1 ≠ a2), and the intersection is:

 

Intersection of Lines in ax+by +c = 0 form

If you have the equations in the more general case : ax+by+c = 0, the lines intersect only if a1b2≠a2b1. If both lines are vertical then by definition they do not intersect, so it can be assumed that at least one of the lines is non-vertical (for example, a1≠0). In this case, the intersection is:

 

Intersection of Lines defined by four points.

If you have four points : (x1,y1) and (x2,y2) belonging to the first line and (x3,y3) and (x4,y4) belonging to the second line, the intersection is given by solving the following equations:

and

where xi,yi is the intersection point. Now I assume you came here for the plain results and not for an explanation or derivation of them, so, getting to the point:

The lines intersect if D ≡ (x1-x2)(y3-y4) - (y1-y2)(x3-x4) ≠0. The intersection point is

Intersection of two lines

where "D" is the above quantity.

The following Java function returns the intersection of two lines defined by four points. Use freely, but please give credit. LGPL license.

 

intersection-lines.java
 
0001  /**
0002   * Computes the intersection between two lines. The calculated point is approximate, 
0003   * since integers are used. If you need a more precise result, use doubles
0004   * everywhere. 
0005   * (c) 2007 Alexander Hristov. Use Freely (LGPL license). http://www.ahristov.com
0006   *
0007   * @param x1 Point 1 of Line 1
0008   * @param y1 Point 1 of Line 1
0009   * @param x2 Point 2 of Line 1
0010   * @param y2 Point 2 of Line 1
0011   * @param x3 Point 1 of Line 2
0012   * @param y3 Point 1 of Line 2
0013   * @param x4 Point 2 of Line 2
0014   * @param y4 Point 2 of Line 2
0015   * @return Point where the segments intersect, or null if they don't
0016   */
0017  public Point intersection(
0018    int x1,int y1,int x2,int y2, 
0019    int x3, int y3, int x4,int y4
0020  ) {
0021    int d = (x1-x2)*(y3-y4) - (y1-y2)*(x3-x4);
0022    if (d == 0) return null;
0023    
0024    int xi = ((x3-x4)*(x1*y2-y1*x2)-(x1-x2)*(x3*y4-y3*x4))/d;
0025    int yi = ((y3-y4)*(x1*y2-y1*x2)-(y1-y2)*(x3*y4-y3*x4))/d;
0026    
0027    return new Point(xi,yi);
0028  }
 

Código Fuente



 

Comentarios

14/05/2012 a las 14:35 Enviado por Benjol
Great, but "Intersection of Lines in ax+by +c = 0 form" second equation should be: x = -c1/a1 - b1y/a1 (the c was missing).
13/05/2012 a las 13:02 Enviado por anonymous
very nice and thanks
12/04/2012 a las 05:01 Enviado por anonymous
This is a great algorithm--helped me a ton. Could you possibly give a better description of the explanation/derivation for the final piece? Oddly enough, I actually would like to know how you get those systems and equations. Thanks in advance!
10/04/2011 a las 16:50 Enviado por cekone
Thanks
18/11/2010 a las 20:32 Enviado por anonymous
thx
26/09/2010 a las 18:21 Enviado por Sv. Kostadinov
10X and greetings from Bulgaria!
09/09/2010 a las 09:05 Enviado por Pepsi
thanks
01/04/2009 a las 13:26 Enviado por Shinu.M
Fantastic. thanks a lot buddy
13/03/2009 a las 14:45 Enviado por willian
hola solo una duda ... este ejemplo que das funciona tambien para manejo de pixeles??? es decir en una imagen tengo dos lineas...en este caso tendria que leer pixel por pixel ? lo que quiero es encontrar el punto en una imagen
03/02/2009 a las 13:40 Enviado por Robert
Nice, but convert to double precision because many multiplications on int scale might overflow.
23/12/2008 a las 04:42 Enviado por Ibrahim
Thank you for the clarity of the code, keep up the good work.
16/07/2008 a las 11:35 Enviado por anonymous
very good!! thanx a lot...
16/01/2008 a las 20:41 Enviado por anonymous
Awesome, easy to read, implement, understand and it works! I have looked around the net for easy algorithms that does the trick and the only easy one I found produced wrong intersection point! Thank you alot for this snippet!
12/07/2007 a las 12:30 Enviado por anonymous
thanks for your contributions, have a great day.

 

Añadir Comentario

Nombre (opcional)
EMail (opcional, no se muestra)

Texto