问题
How can I create vector from another vector and a distance?
For example, with a vector3d(1,1,2)
and distance 12, create another vector that has the same direction but is longer by 12 units.
回答1:
You want to find the unit vector in the direction you want; in XNA this is given by Vector3.Normalize
. Then you can scalar multiply that unit vector (which has, by definition, length = 1) by the final distance.
Ex.:
var originalVector = new Vector3(1, 1, 2);
var finalLength = originalVector.Length + 12; // "longer by 12", this could be whatever you want
originalVector.Normalize(); // make it a unit vector
var finalVector = originalVector * finalLength;
回答2:
Asik's answer is 100% correct from the mathematical standpoint. If you want the enlarged vector to be in the original one use
var originalVector = originalVector * finalLength;
来源:https://stackoverflow.com/questions/24268216/create-vector-from-distance-and-vector