### The code

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
/// <summary> /// Gets the coordinates of the intersection point of two lines. /// </summary> /// <param name="A1">A point on the first line.</param> /// <param name="A2">Another point on the first line.</param> /// <param name="B1">A point on the second line.</param> /// <param name="B2">Another point on the second line.</param> /// <param name="found">Is set to false of there are no solution. true otherwise.</param> /// <returns>The intersection point coordinates. Returns Vector2.zero if there is no solution.</returns> public Vector2 GetIntersectionPointCoordinates(Vector2 A1, Vector2 A2, Vector2 B1, Vector2 B2, out bool found) { float tmp = (B2.x - B1.x) * (A2.y - A1.y) - (B2.y - B1.y) * (A2.x - A1.x); if (tmp == 0) { // No solution! found = false; return Vector2.zero; } float mu = ((A1.x - B1.x) * (A2.y - A1.y) - (A1.y - B1.y) * (A2.x - A1.x)) / tmp; found = true; return new Vector2( B1.x + (B2.x - B1.x) * mu, B1.y + (B2.y - B1.y) * mu ); } |

### The maths behind

To get the point where two lines intersect, we will do some maths. To the mathematicians who will come across this post, I’m truly sorry for the heart attacks or strokes you may experience by reading this post.

Ok, let’s take a look on the figure:

First, sorry, I was too lazy to make a proper figure using computer tools so I just put a scan. XD

Next, here are the definitions:

- & : the two lines,
- : the arbitrary starting points of the two lines,
- : the arbitrary points which tells the direction of the two lines,
- : the intersection point,
- : the origin point.

Kewl. Now, what we want is the intersection point between those two lines. In other words, we want to know the position which starts from either lines’ arbitrary starting point, added by the direction of the line multiplied by a scalar value. So in our figure 1, the position is:

- the position added by the components of multiplied by an unknown which I named
- the position added by the components of multiplied by an unknown which I named

In this case, it is clear that so it will be easy to check if our final formula is correct. 🙂

At the point of intersection, we know that:

… which gives us:

But we can’t use this statement exactly like this to solve our equation as we cannot multiply and divide vectors in this raw format. Also we need to reduce the unkowns count to 1. So we will separate our equation into two equations with the magic of matrices (which I don’t understand well at the moment), one for the x component and one for the y:

To make our equation more readable, we will use some shorthands:

… so:

From this point, we can reduce the unknown count. In my case, I have chosen to keep instead of :

And finally, you have to check if . This will happen if your two lines are parallel or if there is one line defined as a point such as or ; no solution exists in those cases.

#### Test 1

Kewl! Now let’s try it with the *Figure 1*:

We can get the position:

#### Test 2

Ok done! Now let’s try with another figure:

We can get the position:

Nice explanation. Thanks for the blog, i was scratching my head for a while before reading the blog.