Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to adjust the distance of one 3D point from another 3D point by a given distance

I am working with point3D and vector3D classes and I need some help adjusting a point by a given distance.

  • Point A - point residing at coordinate 0,0,0.
  • Point B - point residing at coordinate 1,1,1.
  • Vector AB - vector AB which tells me the length between the two points A and B is distance = 1.73205078.

enter image description here

Code:

Point3D A = new Point3D { X = 0, Y = 0, Z = 0 };
Point3D B = new Point3D { X = 1, Y = 1, Z = 1 };
Vector3D AtoB = A - B;
Double distanceBetweenAandB = AtoB.Length; // the distance will be 1.73205078 here.

I would like to adjust point B. I would like to reduce the distance between point A and point B to 0.5 instead of 1 (adjusting to position C as shown in the diagram). I am trying to work out how to do this.

Point A (0,0,0) is known, point B (1,1,1) is known and the distance to adjust by is known (0.5). How do I calculate?

Pseudo code:

Point3D A = new Point3D { X = 0, Y = 0, Z = 0 };
Point3D B = new Point3D { X = 1, Y = 1, Z = 1 };
Double distanceToAdjust = 0.5;

Point3D newCoordinate = B - distanceToAdjust; // this doesnt work!

Adjusted point B shown in diagram below:

enter image description here

I am using my own defined Point3D class and Vector3D class.

like image 504
Rob McCabe Avatar asked Jan 08 '14 16:01

Rob McCabe


1 Answers

Let's assume your given parameters for your points, and create a 3rd, which we'll call newCoordinate, and that point A will be your reference:

Point3D A = new Point3D { X = 0, Y = 0, Z = 0 };
Point3D B = new Point3D { X = 1, Y = 1, Z = 1 };

Double distanceToAdjust = 0.5;

Point3D newCoordinate = new Point3D { 
                                        A.X + ((B.X - A.X) * distanceToAdjust),
                                        A.Y + ((B.Y - A.Y) * distanceToAdjust),
                                        A.Z + ((B.Z - A.Z) * distanceToAdjust)
                                    }

Here we see the original points:

Original plotted points

Assuming this values, newCoordinate would sit at X=0.5, Y=0.5, Z=0.5. Nice graph follows:

enter image description here

There it is, sitting right in between the two original points.

As a simulation, if you change A and B and assume this values instead:

Point3D A = new Point3D { X = -8, Y = 4, Z = 3 };
Point3D B = new Point3D { X = 3, Y = 2, Z = 1 };

Then newCoordinate position would be X=-2.5, Y=3, Z=2.

enter image description here

Now, same points, but using distanceToAdjust = 1.2:

enter image description here

Keep this two things in mind:

  • Changes in distance always need a reference point. In my sample, I assumed A; that's why it appears as the first portion of each newCoordinate parameter initialization.
  • distanceToAdjust was taken as a multiplier factor.

Addendum: The nifty tool I used to help visualization can be found here.

like image 70
OnoSendai Avatar answered Sep 30 '22 23:09

OnoSendai