Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Algorithm for intersection of 2 lines?

I have 2 lines. Both lines containing their 2 points of X and Y. This means they both have length.

I see 2 formulas, one using determinants and one using normal algebra. Which would be the most efficient to calculate and what does the formula looks like?

I'm having a hard time using matrices in code.

This is what I have so far, can it be more efficient?

public static Vector3 Intersect(Vector3 line1V1, Vector3 line1V2, Vector3 line2V1, Vector3 line2V2) {     //Line1     float A1 = line1V2.Y - line1V1.Y;     float B1 = line1V1.X - line1V2.X;     float C1 = A1*line1V1.X + B1*line1V1.Y;      //Line2     float A2 = line2V2.Y - line2V1.Y;     float B2 = line2V1.X - line2V2.X;     float C2 = A2 * line2V1.X + B2 * line2V1.Y;      float det = A1*B2 - A2*B1;     if (det == 0)     {         return null;//parallel lines     }     else     {         float x = (B2*C1 - B1*C2)/det;         float y = (A1 * C2 - A2 * C1) / det;         return new Vector3(x,y,0);     } } 
like image 701
Shawn Mclean Avatar asked Dec 28 '10 03:12

Shawn Mclean


People also ask

What is the formula for the intersection of two lines?

The point of intersection formula is used to find the point of intersection of two lines, meaning the meeting point of two lines. These two lines can be represented by the equation a1x+b1y+c1=0 a 1 x + b 1 y + c 1 = 0 and a2x+b2y+c2=0 a 2 x + b 2 y + c 2 = 0 , respectively.

Which algorithm is removing the calculation of finding intersection point *?

Sweep Line Algorithm: We can solve this problem in O(nLogn) time using Sweep Line Algorithm.

How do you find the intersection of two lines in space?

To obtain the position vector of the point of intersection, substitute the value of (or ) in (i) and (ii). Example : Show that the line x – 1 2 = y – 2 3 = z – 3 4 and x – 4 5 = y – 1 2 = z intersect. Finf their point of intersection. Solving first two of these equations, we get: = -1 and = -1.


1 Answers

Assuming you have two lines of the form Ax + By = C, you can find it pretty easily:

float delta = A1 * B2 - A2 * B1;  if (delta == 0)      throw new ArgumentException("Lines are parallel");  float x = (B2 * C1 - B1 * C2) / delta; float y = (A1 * C2 - A2 * C1) / delta; 

Pulled from here

like image 96
Brian Genisio Avatar answered Oct 14 '22 04:10

Brian Genisio