Create vector from distance and vector

旧街凉风 提交于 2019-12-23 06:06:27

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!