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. Thanks
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.
Knowing the position of A & B, you can easily find the length and the slope of line AB.
To place D you need to know the length and the slope of the line CD. You already know the length, and he slope of CD is the same as Slope of AB since they are parellel.
T(x) is a translation on the point x
If T(a) = c then T(b) = d
Basically, work out the movement required to get from a to c and apply the same function to b.
Edit: Although technically, from the information you gave us, you could only calculate two different positions for d, not one. Knowing the length is not enough - d could be to either side of c.
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.
There are two formulas that apply here.
The first is slope (rise over run), which = (Yb-Ya) / (Xb-Xa) as well as (Yd-Yc) / (Xd-Xc) since the line segments are parallel.
The second is the pythagorean theorem, L^2 = (Xd-Xc)^2 + (Yd-Yc)^2, where L is the given length of C-D.
Representing the slope as m and solving the equations for point D's X and Y values yields (I think) these two formulas:
Xd = Xc + ( L^2/(1+m^2) )^0.5
Yd = Yc + m (Xd - Xc)