开发者

Solve for D given A,B,C and length of C-D, parallel to A-B [closed]

Closed. This question is off-topic. It is not currently accepting answers.

Want to improve this question? Update the question so it's on-to开发者_运维百科pic for Stack Overflow.

Closed 12 years ago.

Improve this question

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.


Introduce the vector v = A - B. This direction will be the same as the direction between C and D. Hence D = C + λ v, and we only need to determine λ. The distance between C and D is known, d. But the distance is d = | D - C | = | C + λ v - C | = | λ | v, where v = | v | is the length of v. Thus | λ | = d / v so that λ = ± d / v.

FYI, the length | u | of a vector u = (x, y) is given by | u | = sqrt(x^2 + y^2), by the Pythagorean theorem.


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.


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)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜