Using Python 2.7.3 and Pygame 1.9.1.
I have a line and I only know the co-ordinates of endpoints A and B.
I want to calculate what are the co-ordinates AB, given a value on x or y axis.
For Example
Here I know
(x,y) of A , B&C
Also,
C is on X-axis or Y axis.
My Question
How do I calculate the position of the Co-ordinates(x,y) of point D
The equation of the straight line is:
What you want is either
y(x)orx(y), and you have the two endpoints(x1, y1)and(x2, y2). Replace them in the straight-line equation and set up a linear system:Subtraction yields:
and q is obviously:
so, now you have your
y = f(x)representing the straight line connecting your two points.Obviously, a vertical line cannot be represented in this form (
m->+inf), and, if you are trying to trace a line on pixels evaluating this function for every x, you’ll get vertical “holes”.In both these cases, you should use the
x = f(y)form (that you can obtain following these same steps, but starting from the equationx = py + r).That being said, you can get the
yofDknowing itsxby just putting suchxin the equation of the straight line (y = f(x)) determined above; the same holds (with the inverse relation,x = f(y)) if you know theyand want to determine thex.