views:

229

answers:

6

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

alt text

+9  A: 

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.

Peter G.
Correct. Also `C-(B-A)/|B-A|*|C-D|` is a solution (walking in the opposite direction from `C`).
phimuemue
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.

James Curran
+1  A: 
Andreas Rejbrand
A: 

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.

rmx
+3  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.

Chris
This is a more explicit solution. It is however only valid for the special case R^2.
Peter G.
@Peter G.: This is true. The diagram above was only in R^2 though. ;-) I mainly put this answer down because if the OP claimed to have NULL maths skills I wasn't sure if he'd be able to translate the vector based solutions into code necessarily.
Chris
Well, most PNG images are flat... But the content of the image could very well by a picture in R^3.
Andreas Rejbrand
This will fail when AB is a vertical line, but you can make a special case for that. Using vector math is more elegant :)
Tom Sirgedas
@Andreas Rejbrand: Yeah, I know. The little smiley after it was because I wasn't entirely serious. @Tom Sirgedas: Yeah, that was a schoolboy error wasn't it... I'll make a note in my answer. Cheers. And yeah, vector maths is better as a pure maths solution but I'd have no idea how to implement that in C#. Its possible C or C++ do built in vector Maths but I thought I'd just give an alternative in case it was useful. :)
Chris
A: 

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)

KMW