Tim
Tim

Reputation: 8919

Determine the x,y,z coordinates of any point on a line defined by two 3D points using distance from origin-point as the third parameter?

If we have two points in 3D space, does C# have any high-level libraries that would return the x,y,z coordinates of a point that is a specified distance along the line defined by the two points, using one of the points as origin?

     extrapolatedPoint = FindPoint( origin3dPoint, second3dPoint, distanceFromOrigin)

P.S. The line defined by the points isn't a line-segment with the two points as endpoints, but a line that passes through the points.

P.P.S. Adding an image to show the practical purpose. I'm trying to write an app that would calculate lumber lengths as well as the compound cut angles for a splay-leg "trapezoidal" tool stand. The user must specify a) the height of the stand b) the dimensions of the stand's rectangular footprint c) the dimensions of the stand's smaller table surface and d) the nominal size of the lumber being used (e.g. two by four). I need to extrapolate the 3D point at a location 3/4 of the length in order to calculate the lengths of the cross-braces there.

splay leg tool stand

Upvotes: 0

Views: 545

Answers (1)

MBo
MBo

Reputation: 80287

Calculate unit direction vector (perhaps there are ready-to-use functions in C# math library - I see Vector.Normalize if it's suitable)

dx = P2.x - P1.x
dy = P2.y - P1.y
dz = P2.z - P1.z
Len = sqrt(dx*dx+dy*dy+dz*dz)
dx /= Len 
dy /= Len 
dz /= Len 

Now you can define point at line with distance D from the first point (in direction of P2!) as

x  = P1.x + D * dx
y  = P1.y + D * dy
z  = P1.z + D * dz

Note that there are two points with distance D from the origin on the line, so I emphasized direction.

Edit:
In comments author wants to her point at 3/4 (or another value) of P1-P2 way. In this case length is not needed, just make linear interpolation

 t = 3.0/4
 x = P1.x * (1 - t) + P2.x * t
 y = P1.y * (1 - t) + P2.y * t
 z = P1.z * (1 - t) + P2.z * t

or use C# ready-to-use function System.Numerics.Vector3.Lerp

extrapolatedPoint = Lerp(P1, P2, 0.75)

Upvotes: 1

Related Questions