Given a vector $$\vec{v} = \begin{pmatrix} x \\ y \\ z \end{pmatrix} $$ and a general rotation axis $$ \vec{k} = \begin{pmatrix} k_x \\ k_y \\ k_z \end{pmatrix} $$
then the resulting vector after a rotation by $\theta$ is
$$\vec{u} = \vec{v}\cos\theta + \left(\vec{k} \times \vec{v} \right) \sin \theta + \vec{k}
\left( \vec{k} \cdot \vec{v} \right) (1-\cos \theta) $$
where $\times$ is the vector cross product and $\cdot$ the dot product.
If you are looking for the angle to rotate, then look up angle between two vectors. The the rotation axis is defined by the cross product of the original vector and the target vector.
I am not sure how familiar you are with C#
but there is the code to do what I think you are asking.
class Program
{
static void Main(string[] args)
{
// create a random vector within the -10 to 10 range
Vector3 a=10*Vector3.Random();
// target vector
Vector3 b=new Vector3() { x=1, y=1, z=0 };
// get rotation angle
double θ=a.AngleTo(b);
// get rotation axis
Vector3 k=a.Cross(b).Normalized();
// get rotated vector c
Vector3 c=a.RotateAbout(k, θ);
// If c is || to b then c×b=0
Debug.Assert(c.Cross(b).Magnitude<1e-8);
}
}
[DebuggerDisplay("({x},{y},{z})")]
public struct Vector3
{
static Random rnd=new Random();
public double x, y, z;
public static Vector3 Random()
{
return new Vector3()
{
x=-1+2*rnd.NextDouble(),
y=-1+2*rnd.NextDouble(),
z=-1+2*rnd.NextDouble()
};
}
public double Magnitude
{
get { return Math.Sqrt(x*x+y*y+z*z); }
}
public Vector3 Normalized()
{
double m=Magnitude;
if (m>0)
{
return new Vector3()
{
x=x/m,
y=y/m,
z=z/=m
};
}
return this;
}
public double Dot(Vector3 other)
{
return x*other.x+y*other.y+z*other.z;
}
public Vector3 Cross(Vector3 other)
{
return new Vector3()
{
x=y*other.z-z*other.y,
y=z*other.x-x*other.z,
z=x*other.y-y*other.x
};
}
public static Vector3 operator*(double factor, Vector3 vector)
{
return new Vector3()
{
x=factor*vector.x,
y=factor*vector.y,
z=factor*vector.z
};
}
public static Vector3 operator+(Vector3 a, Vector3 b)
{
return new Vector3()
{
x=a.x+b.x,
y=a.y+b.y,
z=a.z+b.z
};
}
public double AngleTo(Vector3 other)
{
// |a·b| = |a| |b| COS(θ)
// |a×b| = |a| |b| SIN(θ)
double a=Magnitude;
double b=other.Magnitude;
double sin=this.Cross(other).Magnitude/(a*b);
double cos=this.Dot(other)/(a*b);
return Math.Atan2(sin, cos);
}
public Vector3 RotateAbout(Vector3 k, double θ)
{
// Assume k is normalized.
double sin=Math.Sin(θ), cos=Math.Cos(θ);
return cos*this+(-sin)*Cross(k)+(1-cos)*Dot(k)*k;
}
}
float
type and then convert back toint
in the end. – John Alexiou Sep 13 '13 at 01:13