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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.