0
\$\begingroup\$

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.

\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

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
\$\endgroup\$
3
  • \$\begingroup\$ oercentqhe? Did you mean percentage? \$\endgroup\$
    – Pikalek
    Commented Aug 8, 2017 at 20:44
  • \$\begingroup\$ 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 \$\endgroup\$
    – Pins
    Commented Aug 9, 2017 at 11:05
  • \$\begingroup\$ @Pins I mistyped that, it should've been that one, thanks for noting \$\endgroup\$
    – Bálint
    Commented Aug 9, 2017 at 14:50

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .