/// <summary>
/// 向量类
/// </summary>
public class Vector
{
public double x, y, z;
public Vector()
{
}
public Vector(double _x, double _y, double _z)
{
x = _x;
y = _y;
z = _z;
}
/// <summary>
/// 求向量长度
/// </summary>
/// <returns></returns>
public double magnitude()
{
return Math.Sqrt(x * x+ y * y+z * z);
}
/// <summary>
/// 求单位向量
/// </summary>
/// <returns></returns>
public Vector normal()
{
double k = 1 / magnitude();
Vector d = new Vector(k * x, k * y, k * z);
return d;
}
/// <summary>
/// 向量和
/// </summary>
/// <param name="v1">向量1</param>
/// <param name="v2">向量2</param>
/// <returns>新向量</returns>
public static Vector operator +(Vector v1, Vector v2)
{
Vector d = new Vector(v1.x + v2.x, v1.y + v2.y, v1.z + v2.z);
return d;
}
/// <summary>
/// 求反向量
/// </summary>
/// <param name="v">向量</param>
/// <returns>新向量</returns>
public static Vector operator -(Vector v)
{
Vector d = new Vector(-v.x, -v.y, -v.z);
return d;
}
/// <summary>
/// 求向量
/// </summary>
/// <param name="v1"></param>
/// <param name="v2"></param>
/// <returns></returns>
public static Vector operator -(Vector v1, Vector v2)
{
Vector d = new Vector(v1.x - v2.x, v1.y - v2.y, v1.z - v2.z);
return d;
}
public static Vector operator *(Vector v, double k)
{
Vector d = new Vector(k * v.x, k * v.y, k * v.z);
return d;
}
public static Vector operator *(double k, Vector v)
{
Vector d = new Vector(k * v.x, k * v.y, k * v.z);
return d;
}
public static Vector operator /(Vector v, double k)
{
Vector d=new Vector(v.x / k, v.y / k, v.z / k) ;
d.x =v.x / k;
d.y =v.y / k;
d.z =v.z / k;
return d;
}
public static double operator ^(Vector v1, Vector v2)
{
return v2.x + v2.x + v1.y + v2.y + v1.z + v2.z;
}
public static Vector operator *(Vector v1, Vector v2)
{
Vector d = new Vector(v1.y * v2.z - v2.z * v2.y,
v1.z * v2.x - v1.x * v2.z,
v1.x * v2.y - v1.y * v2.x);
return d;
}
}