Instead of taking $x,y$ as the variables to solve for, write $(x,y)$ $= (x_1,y_1)+t(x_2-x_1,y_2-y_1) $ $= (x_3,y_3)+u(x_4-x_3,y_4-y_3)$ and solve for $t$ and $u$. Then it doesn't matter a bit whether your line segments are horizontal or vertical or whatever, and you can check for being within the segments just by looking at whether $0 < t < 1$ and $0 < u < 1$.
Note that if the line segments are parallel you'll get zero in your denominator, and if they're almost parallel you'll get something very small there; you may want to take care about that unless something in your setup guarantees that the line segments aren't close to being parallel.
one line segment from $(x_1,y_1)$ to $(x_2,y_2)$
another line segment from $(x_3,y_3)$ to $(x_4,y_4)$
the set of points on the first line segment is $$A = \{ (x_1 + (x_2-x_1)u,y_1 + (y_2-y_1)u) \in \mathbb R^2 \mid u \in [0,1] \}$$
the set of points on the second line segment is $$B = \{ (x_3 + (x_4-x_3)t,y_3 + (y_4-y_3)t) \in \mathbb R^2 \mid t \in [0,1] \}$$
we want to find $$A \cap B$$ which means finding $u \in [0,1]$, $t \in [0,1]$ such that $$(x_1 + (x_2-x_1)u,y_1 + (y_2-y_1)u)=(x_3 + (x_4-x_3)t,y_3 + (y_4-y_3)t)$$
split into components
$$x_1 + (x_2-x_1)u=x_3 + (x_4-x_3)t$$
$$y_1 + (y_2-y_1)u=y_3 + (y_4-y_3)t$$
solve for $u$ by eliminating $t$
$$\frac{(x_1-x_3) + (x_2-x_1)u}{(x_4-x_3)}=\frac{(y_1-y_3) + (y_2-y_1)u}{(y_4-y_3)}$$
$$\frac{(y_4-y_3)(x_1-x_3) - (x_4-x_3)(y_1-y_3)}{(x_4-x_3)(y_2-y_1) - (y_4-y_3)(x_2-x_1)} = u$$
now you can find the intersection of two lines by calculating this $u$ and checking its between 0 and 1, then calculating t (which is easy once you know u) and checking it's also between 0 and 1.
Best Answer
Since you received Willemien's answer, let us consider the problem from an algebraic point of view.
Let us note $(x_1,y_1)$ and $(x_2,y_2)$ the coordinates of the two points limiting the first segment and $(x_3,y_3)$ and $(x_4,y_4)$ the coordinates of the two points limiting the second segment. So, the equations of the first and second lines are respectively $$y=\frac{ {y_1}-{y_2}}{{x_1}-{x_2}}x+\frac{{x_1} {y_2}-{x_2} {y_1}}{{x_1}-{x_2}}$$ $$y=\frac{ {y_3}-{y_4}}{{x_3}-{x_4}}x+\frac{{x_3} {y_4}-{x_4} {y_3}}{{x_3}-{x_4}}$$ Assuming that they are not parallel, these two lines intersect at a point $x_*$ such that $$x_*=\frac{{x_1} ({x_3} ({y_2}-{y_4})+{x_4} ({y_3}-{y_2}))+{x_2} ({x_3} ({y_4}-{y_1})+{x_4} ({y_1}-{y_3}))}{({x_1}-{x_2}) ({y_3}-{y_4})+({x_4}-{x_3}) ({y_1}-{y_2})}$$ and this value must be such that $x_1 \leq x_* \leq x_2$ and $x_3 \leq x_* \leq x_4$ in order the segments intercept.