//// 版权所有: Anders06 于2008年1月14日
Vector3f
using System;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;
namespace MathLibrary
{
public class Vector3f
{
Variable Members#region Variable Members
private float m_x = 0.0f;
private float m_y = 0.0f;
private float m_z = 0.0f;
private static Vector3f AXIS_X = new Vector3f(1.0f, 0.0f, 0.0f);
private static Vector3f AXIS_Y = new Vector3f(0.0f, 1.0f, 0.0f);
private static Vector3f AXIS_Z = new Vector3f(0.0f, 0.0f, 1.0f);
private static Vector3f AXIS_MX = new Vector3f(-1.0f, 0.0f, 0.0f);
private static Vector3f AXIS_MY = new Vector3f(0.0f, -1.0f, 0.0f);
private static Vector3f AXIS_MZ = new Vector3f(0.0f, 0.0f, -1.0f);
private static Vector3f ZERO_VECTOR = new Vector3f(0.0f, 0.0f, 0.0f);
#endregion
Construction#region Construction
public Vector3f()
{
}
public Vector3f(float x, float y, float z)
{
this.m_x = x; this.m_y = y; this.m_z = z;
}
public Vector3f(Vector3f vec)
{
this.m_x = vec.m_x;
this.m_y = vec.m_y;
this.m_z = vec.m_z;
}
#endregion
Properties#region Properties
public static Vector3f AxisX
{
get { return AXIS_X; }
}
public static Vector3f AxisY
{
get { return AxisY; }
}
public static Vector3f AxisZ
{
get { return AXIS_Z; }
}
public static Vector3f AxisMX
{
get { return AXIS_MX; }
}
public static Vector3f AxisMY
{
get { return AXIS_MY;}
}
public static Vector3f AxisMZ
{
get { return AXIS_MZ; }
}
public static Vector3f ZeroVector
{
get { return ZERO_VECTOR; }
}
public float X
{
get { return m_x; }
set { m_x = value; }
}
public float Y
{
get { return m_y; }
set { m_y = value; }
}
public float Z
{
get { return m_z; }
set { m_z = value; }
}
/**//// <summary>
/// 模
/// </summary>
public float Length
{
get { return (float)System.Math.Sqrt(LengthSqrd); }
}
public float LengthSqrd
{
get { return m_x * m_x + m_y * m_y + m_z * m_z; }
}
#endregion
Operator Overload#region Operator Overload
public static Vector3f operator +(Vector3f lVec, Vector3f rVec)
{
return new Vector3f(lVec.X + rVec.X, lVec.Y + rVec.Y, lVec.Z + rVec.Z);
}
public static Vector3f operator -(Vector3f vec)
{
return new Vector3f(-vec.X, -vec.Y, -vec.Z);
}
public static Vector3f operator -(Vector3f lVec, Vector3f rVec)
{
return new Vector3f(lVec.X - rVec.X, lVec.Y - rVec.Y, lVec.Z - rVec.Z);
}
public static Vector3f operator *(Vector3f vec, float f)
{
return new Vector3f(vec.X * f, vec.Y * f, vec.Z * f);
}
public static Vector3f operator /(Vector3f vec, float f)
{
Debug.Assert(f != 0.0);
f = 1 / f;
return new Vector3f(vec.X * f, vec.Y * f, vec.Z * f);
}
public static bool operator ==(Vector3f lVec, Vector3f rVec)
{
return (lVec.X == rVec.X && lVec.Y == rVec.Y && lVec.Z == rVec.Z);
}
public static bool operator !=(Vector3f lVec, Vector3f rVec)
{
return !(lVec.X == rVec.X && lVec.Y == rVec.Y && lVec.Z == rVec.Z);
}
#endregion
Operations#region Operations
/**//// <summary>
/// 向量标准化
/// </summary>
/// <returns></returns>
public Vector3f Normal()
{
float f = Length;
if (f != 0.0f)
return new Vector3f(m_x / f, m_y / f, m_z / f);
else
return Vector3f.ZERO_VECTOR;
}
/**//// <summary>
/// 点乘
/// </summary>
/// <param name="vec"></param>
/// <returns></returns>
public Vector3f PointProduct(Vector3f vec)
{
return new Vector3f(m_x * vec.m_x, m_y * vec.m_y, m_z * vec.m_z);
}
/**//// <summary>
/// 叉乘
/// </summary>
/// <param name="vec"></param>
/// <returns></returns>
public Vector3f CrossProduct(Vector3f vec)
{
return new Vector3f(m_y * vec.Z - vec.Y * m_z, // x axis
m_z * vec.X - vec.Z * m_x, // y axis
m_x * vec.Y - vec.X * m_y // z axis
);
}
/**//// <summary>
/// 两点距离
/// </summary>
/// <param name="lVec"></param>
/// <param name="rVec"></param>
/// <returns></returns>
public float Distance(Vector3f lVec, Vector3f rVec)
{
float dx = lVec.X - rVec.X;
float dy = lVec.Y - rVec.Y;
float dz = lVec.Z - rVec.Z;
return (float)System.Math.Sqrt(dx * dx + dy * dy + dz * dz);
}
#endregion
Virtural Overrides#region Virtural Overrides
public override int GetHashCode()
{
return MixCodes(m_x.GetHashCode(), MixCodes(m_y.GetHashCode(), m_z.GetHashCode()));
}
public override bool Equals(object obj)
{
Vector3f otherVec = obj as Vector3f;
if (otherVec == null)
return false;
return (this.X == otherVec.X && this.Y == otherVec.Y && this.Z == otherVec.Z);
}
#endregion
Implement Detail#region Implement Detail
/**//// <summary>
/// 混合哈希因子
/// </summary>
/// <param name="code1"></param>
/// <param name="code2"></param>
/// <returns></returns>
private static int MixCodes(int code1, int code2)
{
int h = (code1 << 4) + code2;
int g = h & unchecked((int)0xF0000000);
if (g != 0)
h ^= g >> 24;
h &= ~g;
return h;
}
#endregion
}
}
Vector3f
using System;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;
namespace MathLibrary
{
public class Vector3f
{
Variable Members#region Variable Members
private float m_x = 0.0f;
private float m_y = 0.0f;
private float m_z = 0.0f;
private static Vector3f AXIS_X = new Vector3f(1.0f, 0.0f, 0.0f);
private static Vector3f AXIS_Y = new Vector3f(0.0f, 1.0f, 0.0f);
private static Vector3f AXIS_Z = new Vector3f(0.0f, 0.0f, 1.0f);
private static Vector3f AXIS_MX = new Vector3f(-1.0f, 0.0f, 0.0f);
private static Vector3f AXIS_MY = new Vector3f(0.0f, -1.0f, 0.0f);
private static Vector3f AXIS_MZ = new Vector3f(0.0f, 0.0f, -1.0f);
private static Vector3f ZERO_VECTOR = new Vector3f(0.0f, 0.0f, 0.0f);
#endregion
Construction#region Construction
public Vector3f()
{
}
public Vector3f(float x, float y, float z)
{
this.m_x = x; this.m_y = y; this.m_z = z;
}
public Vector3f(Vector3f vec)
{
this.m_x = vec.m_x;
this.m_y = vec.m_y;
this.m_z = vec.m_z;
}
#endregion
Properties#region Properties
public static Vector3f AxisX
{
get { return AXIS_X; }
}
public static Vector3f AxisY
{
get { return AxisY; }
}
public static Vector3f AxisZ
{
get { return AXIS_Z; }
}
public static Vector3f AxisMX
{
get { return AXIS_MX; }
}
public static Vector3f AxisMY
{
get { return AXIS_MY;}
}
public static Vector3f AxisMZ
{
get { return AXIS_MZ; }
}
public static Vector3f ZeroVector
{
get { return ZERO_VECTOR; }
}
public float X
{
get { return m_x; }
set { m_x = value; }
}
public float Y
{
get { return m_y; }
set { m_y = value; }
}
public float Z
{
get { return m_z; }
set { m_z = value; }
}
/**//// <summary>
/// 模
/// </summary>
public float Length
{
get { return (float)System.Math.Sqrt(LengthSqrd); }
}
public float LengthSqrd
{
get { return m_x * m_x + m_y * m_y + m_z * m_z; }
}
#endregion
Operator Overload#region Operator Overload
public static Vector3f operator +(Vector3f lVec, Vector3f rVec)
{
return new Vector3f(lVec.X + rVec.X, lVec.Y + rVec.Y, lVec.Z + rVec.Z);
}
public static Vector3f operator -(Vector3f vec)
{
return new Vector3f(-vec.X, -vec.Y, -vec.Z);
}
public static Vector3f operator -(Vector3f lVec, Vector3f rVec)
{
return new Vector3f(lVec.X - rVec.X, lVec.Y - rVec.Y, lVec.Z - rVec.Z);
}
public static Vector3f operator *(Vector3f vec, float f)
{
return new Vector3f(vec.X * f, vec.Y * f, vec.Z * f);
}
public static Vector3f operator /(Vector3f vec, float f)
{
Debug.Assert(f != 0.0);
f = 1 / f;
return new Vector3f(vec.X * f, vec.Y * f, vec.Z * f);
}
public static bool operator ==(Vector3f lVec, Vector3f rVec)
{
return (lVec.X == rVec.X && lVec.Y == rVec.Y && lVec.Z == rVec.Z);
}
public static bool operator !=(Vector3f lVec, Vector3f rVec)
{
return !(lVec.X == rVec.X && lVec.Y == rVec.Y && lVec.Z == rVec.Z);
}
#endregion
Operations#region Operations
/**//// <summary>
/// 向量标准化
/// </summary>
/// <returns></returns>
public Vector3f Normal()
{
float f = Length;
if (f != 0.0f)
return new Vector3f(m_x / f, m_y / f, m_z / f);
else
return Vector3f.ZERO_VECTOR;
}
/**//// <summary>
/// 点乘
/// </summary>
/// <param name="vec"></param>
/// <returns></returns>
public Vector3f PointProduct(Vector3f vec)
{
return new Vector3f(m_x * vec.m_x, m_y * vec.m_y, m_z * vec.m_z);
}
/**//// <summary>
/// 叉乘
/// </summary>
/// <param name="vec"></param>
/// <returns></returns>
public Vector3f CrossProduct(Vector3f vec)
{
return new Vector3f(m_y * vec.Z - vec.Y * m_z, // x axis
m_z * vec.X - vec.Z * m_x, // y axis
m_x * vec.Y - vec.X * m_y // z axis
);
}
/**//// <summary>
/// 两点距离
/// </summary>
/// <param name="lVec"></param>
/// <param name="rVec"></param>
/// <returns></returns>
public float Distance(Vector3f lVec, Vector3f rVec)
{
float dx = lVec.X - rVec.X;
float dy = lVec.Y - rVec.Y;
float dz = lVec.Z - rVec.Z;
return (float)System.Math.Sqrt(dx * dx + dy * dy + dz * dz);
}
#endregion
Virtural Overrides#region Virtural Overrides
public override int GetHashCode()
{
return MixCodes(m_x.GetHashCode(), MixCodes(m_y.GetHashCode(), m_z.GetHashCode()));
}
public override bool Equals(object obj)
{
Vector3f otherVec = obj as Vector3f;
if (otherVec == null)
return false;
return (this.X == otherVec.X && this.Y == otherVec.Y && this.Z == otherVec.Z);
}
#endregion
Implement Detail#region Implement Detail
/**//// <summary>
/// 混合哈希因子
/// </summary>
/// <param name="code1"></param>
/// <param name="code2"></param>
/// <returns></returns>
private static int MixCodes(int code1, int code2)
{
int h = (code1 << 4) + code2;
int g = h & unchecked((int)0xF0000000);
if (g != 0)
h ^= g >> 24;
h &= ~g;
return h;
}
#endregion
}
}