# Find x value on a line segment with given Y

I need to find the x value of the point on a given line segment where both end point is known and Y value of the point to be find is given.

That is $P_1(x_1,y_1)$, $P_2(x_2,y_2)$ is given.

And I want to find the point F on the line segment where y coordinates of F is known, how to find the x co-ordinates.

       P1(x,y)
|
|
|
| F(?,y)
|
|
|
P2(x,y)


I have tried using equation of line but getting wrong result.

 m = (y2-y1)/(x2-x1)
X = ((Y-y1)/m)+x1

• is this a straight line? – Dr. Sonnhard Graubner May 26 '17 at 11:39
• @Dr.SonnhardGraubner Yes – Parcly Taxel May 26 '17 at 11:40

Let $v$ be the vector from $\mathrm{P1}$ to $\mathrm{P2}$. $$v = (x_2 - x_1, y_2 - y_1)$$ Lets scale the vector such that $\mathrm{P1} + kv = \mathrm{F}$. This is possible since the three points are colinear.

Write out the above equation for the y-coordiante: $y_1 + k(y_2-y_1) = y_f$. If we isolate $k$ we get $k = \frac{y_f - y_1}{y_2 - y_1}$.

If we write out the same equation for the x-coordinate, we get: $x_1 + k(x_2 - x_1) = x_f$. Since we know everything on the left side, we can combine it: $$x_f = x_1 + \frac{(y_f - y_1)\cdot(x_2 - x_1)}{y_2 - y_1}$$.

This fails if $y_1 = y_2$, since we then divide by zero. When this happens, one of the following is the problem:

1. $\mathrm{P1} = \mathrm{P2}$, in which case there is no unique line through the points.
2. $\mathrm{P1}$ and $\mathrm{P2}$ lie on a line parallel to the x-axis, in which case any point on that line has the same y-coordinate, so you can’t conclude which point it is anyway.

Edit: This appears to be the same as your proposed incorrect formula, but it is correct. To convince yourself, place two points named A and B in geogebra and define a point as P = (x(A) + ((2 - y(A)) (x(B) - x(A))) / (y(B) - y(A)), 2). That point is this formula for $y_f = 2$.

• You are write, I was doing this calculation using c++ on opencv image processing library, the variable I used in equation was wrong, now it solved, thanks for the answer. – Haris May 26 '17 at 12:13