line parallel to x-axis and arbitrary intersection test

107 Views Asked by At

I am going through code snippets that calculate the x-intersection point between the line parallel to the x-ais and an arbitrary line between points (x1,y1) and (x2,y2). The code snippet does the following:

double t = (x2 - x1)/(y2 - y1);

xDbl = (static_cast(yPos) - y1) * t + x1;

I am having difficulty to understand the calculation of t, is it rise over run ? Some mathematical reference to the above snippet would be very helpful.

Thanks

2

There are 2 best solutions below

0
On BEST ANSWER

Line passing through two points $(x_1, y_1)$ and $(x_2, y_2)$ is given by:

$$y-y_1 = \dfrac{y_1-y_2}{x_1 - x_2}\times (x-x_1)\ \ \ \ \ \ \ ... \rm (i)$$

for point of intersection with $y = k$, put $y = k$ in above equation.

$$k-y_1 = \dfrac{y_1-y_2}{x_1 - x_2}\times (x-x_1)$$

Solving for $x$ gives the x-coordinate. Substitute the value in $\rm Eqn \ (i)$ to find y coordinate.

0
On

You can define the line between $(x_{1},y_{1})$ and $(x_{2},y_{2})$ like this:

$y=mx+b$ where $m= \frac{y_{2}-y_{1}}{x_{2}-x_{1}}$. and $b=y_{1}-mx_{1}$

So you have the value for the intersection $y_{0}$ (Since is a parallel to the x-axis $y=y_{0}$ for all $x$ values), you only need to calculate for $x_{0}$:

$x_{0}=\frac{y_{0}-b}{m}=\frac{(x_{2}-x_{1})(y_{0}-y_{1})}{(y_{2}-y_{1})}+x_{1}$

The parameter $t$:

You can also see the line like this:

$f(t)=(x_{1},y_{1})+t(x_{2}-x_{1},y_{2}-y_{1})$ with $t$ in $[0,1]$

Where

$f(0)=(x_{1},y_{1})$

$f(1)=(x_{2},y_{2})$

Again if you have the value of $y_{0}$, then:

$y_{1}+t(y_{2}-y_{1})=y_{0}$

find the value of $t$ and:

$x_{1}+t(x_{2}-x_{1})=x_{0}$