How to find a third point given both (2 points on

2019-08-05 08:49发布

问题:

"How to find a third point given both (2 points on a line) and (distance from third point to first point)?" Language: Visual Basic (2012)

The third point is on the same line as the second, and may be either closer to the first point or it may be closer to the second. This is a function that will handle both (from arrays of data).

Strangely I cannot seem to grasp the distance part of this question. Over reading many other questions on finding points from other points, I am unable to find anything clear enough for me to be able to reverse engineer to include a parameter for distance.

I need to be able to use distance to find a point. The function I am writing is basically a much more advanced form of:

Function GetThirdPoint(CenterPoint As Point, SecondPoint As Point, Range As Integer)
  Return [Equations here] 'Return third point
End Function

回答1:

Let's first point coordinates are P1=(x1,y1), second point P2=(x2,y2).
Then length of P1P2 vector is (use Math.Hypot function if available)

Len = Sqrt((x2-x1)*(x2-x1) + (y2-y1)*(y2-y1))

Normalized (unit-length) direction vector is

dx = (x2-x1) / Len
dy = (y2-y1) / Len

P3 coordinates for case when P1P3 and P1P2 vectors have the same direction:

x3 = x1 + Distance * dx
y3 = y1 + Distance * dy

for opposite direction:

x3 = x1 - Distance * dx
y3 = y1 - Distance * dy


回答2:

The general equation of a line is:

A*x + B*y + G = 0 where A, B must not be both equal to 0. (1)

You can find A, B, G easily because you know two points of the line (point one and two). The distance is:

D = sqrt( (x1 - x3)(x1 - x3) + (y1 - y3)(y1 - y3) ) (2)

Third point is on the line so from (1):

A*x3 + B*y3 + G = 0 (3)

From (2) and (3), you can find the solution. Because (2) is second degree you will find two solutions.