How can I uniformly randomly rotate a Vector2 between 0 and 360 degrees?
I have a Vector2
:
Vector2 v = new Vector2(1,0开发者_StackOverflow中文版);
How can I randomly rotate this vector (about (0,0)), with a uniform distribution in the range [0,360) degrees?
To randomly rotate a vector v
, counter clockwise:
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)
);
The transformation mathematics comes from here
精彩评论