# Intersection point of two lines with given formula

I have following formula to check the intersection of two line segments:

Edge is defined like point2-point1 vector.
bool intersects(const Line2D& line2)
{
float s, t;
s = (-edge.y * (point1.x - line2.point1.x) + edge.x * (point1.y - line2.point1.y)) / (-line2.edge.x * edge.y + edge.x * line2.edge.y);
t = (line2.edge.x * (point1.y - line2.point1.y) - line2.edge.y * (point1.x - line2.point1.x)) / (-line2.edge.x * edge.y + edge.x * line2.edge.y);

if (s >= 0 && s <= 1 && t >= 0 && t <= 1) {
return true;
}
return false;
}


However I´m trying to figure out how to implement the "find the point of intersection" option for this as most of the answers with the same theme are "math based". Thanks for your help.

## 1 Answer

s and t are the percentage of the line before the intersection point.

If t is between 0 and 1, them the intersection point is

x = point1.x + (point2.x - point1.x) * t
y = point1.y + (point2.y - point1.y) * t

• oercentqhe? Did you mean percentage? – Pikalek Aug 8 '17 at 20:44
• I had a little problems at some point getting the right intersection point but after changing the second line to : y = point1.y + (point2.y - point1.y)*t it works good – Pins Aug 9 '17 at 11:05
• @Pins I mistyped that, it should've been that one, thanks for noting – Bálint Aug 9 '17 at 14:50