I am working with point3D and vector3D classes and I need some help adjusting a point by a given distance.
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:
I am using my own defined Point3D class and Vector3D class.
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:
Assuming this values, newCoordinate
would sit at X=0.5, Y=0.5, Z=0.5. Nice graph follows:
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.
Now, same points, but using distanceToAdjust = 1.2
:
Keep this two things in mind:
newCoordinate
parameter initialization.Addendum: The nifty tool I used to help visualization can be found here.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With