I'm trying to figure out how to do this. Essentially I have points A and B which I know the location of. I then have point C and point D which I only know the coordinates of C. I know the length of C-D and know that C-D must be parallel to A-B. How could I generally solve for D given A,B,C and length of C-D.
alt text http://img706.imageshack.us/img706/4494/imgclr.png
D = C ± (B-A) / |B-A| * |C-D|
If B=A there is no solution as the line AB degenerates to a point and parallelety of a line to a point is not defined.
Explanation
(B-A) / |B-A| is a direction vector of unit length. Multiplication by the length |C-D| results in the proper offset vector.
Edits: changed + to ± to provide both solutions. Added trivial case B=A.
This answer is similar to some others but I think explains the maths more and should allow you to incorporate it into a program more easily.
You can find the gradient of the "known" line by doing (Ay-By)/(Ax-Bx)
(where Ay
is the y co-ordinate of A
, etc.). Lets just call this M
since it is entirely calculable.
If the two lines are parallel then you can work out the gradient of the other line in the same way:
Gradient = (Cy-Dy)/(Cx-Dx) = M
Which rearranges to (Cy-Dy) = M*(Cx-Dx)
We also know that C->D
is a given length (lets call it L). So we can say
(Cy-Dy)^2+(Cx-Dx)^2 = L^2
Using our gradient equation we can substitute to get:
(M^2+1)(Cx-Dx)^2 = L^2
Given we know what M, L and Dx are we can easily solve this:
Cx = ((L^2)/(M^2+1))^0.5 + Dx
then we can use this value of Cx
along with either equation (Gradient is probably easiest) to get Cy
.
Of note is that the last equation has a square root which can be positive or negative so you will get two possible values of Cx
and thus two possible values of Cy
. This is the equivalent of moving in the two opposite directions on the parallel line from D
.
Edit:
As noted in comments this will fail if the line is vertical (ie Ax-Bx = 0
). You would need to special case this but in this case the answer becomes a trivial case of just adding or subtracting your length from the value of Cy.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With