Finding root for the segment - found the formula but it doesn't work for some values - wrong formula?

103 Views Asked by At

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?

2

There are 2 best solutions below

1
On BEST ANSWER

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}}$$

4
On

$b$ should be

\begin{equation*} b=-\frac{y_{2}-y_{1}}{x_{2}-x_{1}}x_{1}+y_{1}=\frac{-x_{1}y_{2}+x_{1}y_{1}}{% x_{2}-x_{1}}+y_{1}, \end{equation*}

because from

\begin{equation*} (x_{2}-x_{1})(y-y_{1})=(y_{2}-y_{1})(x-x_{1}) \end{equation*}

we get

\begin{eqnarray*} y-y_{1} &=&\frac{y_{2}-y_{1}}{x_{2}-x_{1}}(x-x_{1}) \\ &\Leftrightarrow &y=\frac{y_{2}-y_{1}}{x_{2}-x_{1}}(x-x_{1})+y_{1} \\ &\Leftrightarrow &y=\frac{y_{2}-y_{1}}{x_{2}-x_{1}}x-\frac{y_{2}-y_{1}}{ x_{2}-x_{1}}x_{1}+y_{1} \\ &\Leftrightarrow &y=ax+b,\qquad a=\frac{y_{2}-y_{1}}{x_{2}-x_{1}},b=-\frac{ y_{2}-y_{1}}{x_{2}-x_{1}}x_{1}+y_{1}. \end{eqnarray*}

ADDED in response to OP's comment. For $(x_{1},y_{1})=(4528,498),(x_{2},y_{2})=(5527,-1500)$ we have $a=-2$ and $b=9554$. So

\begin{equation*} y=-2x+9554 \end{equation*}

and

\begin{equation*} -2x+9554=0\Leftrightarrow x=4777. \end{equation*}