[RESOLVED] Finding a point on a line
I'm really stuck at this problem :(
I have a line with coordinates (X1, Y1)-(X2, Y2) and a point C on that line. I know X coordinate of this point, but i need to know Y coordinate of this point. :confused:
Here is a sample image:
http://img529.imageshack.us/img529/4417/line.png
Please help me! :afrog:
Re: Finding a point on a line
The slope of a line can be found given any two points on the line. To calculate the slope, divide the difference in the Y-values by the difference in X-values.
Slope = (Y2 - Y1)/(X2 - X1)
Slope = (Y3 - Y1)/(X3 - X1)
This obviously implies that (Y3 - Y1)/(X3 - X1) = (Y2 - Y1)/(X2 - X1)
Solving for Y3, you get: Y3 = Y1 + (X3 - X1)(Y2 - Y1)/(X2 - X1)
Re: Finding a point on a line
Just as an edge case, you might have a vertical line in which case the above formula will throw a division by zero, since the question isn't well defined. Of course it depends on the problem, just wanted to bring it up in case it would cause a bug later.