Get a random unit vector at a given angle with another vector.
Source:R/core.R
rand_vector_at_angle.Rd
Get a random unit vector which is at angle
radians of vector u
.
Note that u
is expected to be a unit vector itself.
Note
This function is stochastic. For reproducibility set a PRNG seed with set.seed.
Examples
u <- c(1.0, 0, 0.5, -0.5) # Define a 4D vector
u <- u / norm(u, "2") # Normalize the vector
v <- rand_vector_at_angle(u, pi / 4) # Get a vector at 45 degrees
arad <- acos((u %*% v) / norm(u,"2") * norm(v, "2")) # Get angle in radians
arad * 180 / pi # Convert to degrees, should be close to 45 degrees
#> [,1]
#> [1,] 45