In matlab, how can I decide whether a line intersects with a rectangle or not ?

For example the upper line is not intersecting with the rectangle and the two lower ones do,
Then: How can I decide the existence of the intersection from knowing two points of the line and another two points from the rectangle ?
My work:
Way 1: I am trying to solve the two equations of the line and the diagonal of the rectangle then check the solution point if it's within the rectangle or not.