如何在0到360度之间均匀地随机旋转Vector2?

时间:2011-09-01 17:20:58

标签: java vector

我有Vector2

Vector2 v = new Vector2(1,0);

如何随机旋转此向量(约(0,0)),在[0,360]度范围内均匀分布?

1 个答案:

答案 0 :(得分:2)

要逆时针旋转矢量v

Vector2 v = new Vector2( 1,0 );

Random rnd = new Random();
double rotationAngle = 2.0 * Math.PI * rnd.nextDouble();

Vector2 vRotated = new Vector2( 
   (v.x)*Math.cos(rotationAngle) + (v.y)*Math.sin(rotationAngle),
   (v.y)*Math.cos(rotationAngle) - (v.x)*Math.sin(rotationAngle)
);

转型数学来自here