I have the segment, defined as $(x_1, y_1)$, $(x_2, y_2)$.
I know that $y_1\ge 0$ and $y_2 < 0$.
I want to compute the root point for that segment.
I decided to do it that way:
we know that:
$(x_2-x_1)(y-y_1) = (y_2-y_1)(x-x_1)$
after some operations:
$y = \frac{(y_2-y_1)}{(x_2-x_1)}x + \frac{-x_1\cdot y_2 + x_2\cdot y_1}{x_2-x_1}$
So simply, I get the line as $y = ax + b$ where:
$a = \frac{(y_2-y_1)}{(x_2-x_1)}$
$b = \frac{-x_1\cdot y_2 + x_2\cdot y_1}{x_2-x_1}$
To compute root point, I just compute $f(0)$, which is equal to $b$ in my example (for $y_1 \ge 0$ and $y_2 < 0$):
$y_0 = \frac{-x_1\cdot y_2 + x_2\cdot y_1}{x_2-x_1}$
I tested my solution for:
$(x_1, y_1) = (1, 2)$
$(x_2, y_2) = (4, -1)$
RESULT: 3
And it's ok. But for:
$(x_1, y_1) = (4528, 498)$
$(x_2, y_2) = (5527, -1500)$
RESULT: 9554
Which is WRONG. The root point of segment from $x_1$ to $x_2$ ($y_1 \ge 0$ and $y_2 < 0$) cannot be after $x_2$.
So is my formula wrong?
You made a mistake in the reasoning. Your equation is $y=a x + b$. Since you want $y=0$ and you look for $x$, then $$x=-\frac{b}{a}=\frac{{x_2} {y_1}-{x_1} {y_2}}{{y_1}-{y_2}}$$