using System;
namespace Plankton
{
///
/// Represents a vector in Euclidean space.
///
public struct PlanktonXYZ
{
#region members
internal float _x;
internal float _y;
internal float _z;
#endregion
#region constructors
///
/// Constructs a new vector from 3 single precision numbers.
///
/// X component of vector.
/// Y component of vector.
/// Z component of vector.
public PlanktonXYZ(float x, float y, float z)
{
_x = x;
_y = y;
_z = z;
}
#endregion
#region static properties
///
/// Gets the value of the vector with components 0,0,0.
///
public static PlanktonXYZ Zero
{
get { return new PlanktonXYZ(); }
}
///
/// Gets the value of the vector with components 1,0,0.
///
public static PlanktonXYZ XAxis
{
get { return new PlanktonXYZ(1f, 0f, 0f); }
}
///
/// Gets the value of the vector with components 0,1,0.
///
public static PlanktonXYZ YAxis
{
get { return new PlanktonXYZ(0f, 1f, 0f); }
}
///
/// Gets the value of the vector with components 0,0,1.
///
public static PlanktonXYZ ZAxis
{
get { return new PlanktonXYZ(0f, 0f, 1f); }
}
#endregion static properties
#region properties
///
/// Gets or sets the X (first) component of this vector.
///
public float X { get { return _x; } set { _x = value; } }
///
/// Gets or sets the Y (second) component of this vector.
///
public float Y { get { return _y; } set { _y = value; } }
///
/// Gets or sets the Z (third) component of this vector.
///
public float Z { get { return _z; } set { _z = value; } }
#endregion
///
/// Computes a hash number that represents the current vector.
///
/// A hash code that is not unique for each vector.
public override int GetHashCode()
{
// MSDN docs recommend XOR'ing the internal values to get a hash code
return _x.GetHashCode() ^ _y.GetHashCode() ^ _z.GetHashCode();
}
///
/// Sums up two vectors.
///
/// A vector.
/// A second vector.
/// A new vector that results from the componentwise addition of the two vectors.
public static PlanktonXYZ operator +(PlanktonXYZ v1, PlanktonXYZ v2)
{
return new PlanktonXYZ(v1._x + v2._x, v1._y + v2._y, v1._z + v2._z);
}
///
/// Subtracts one vector from another.
///
/// A vector.
/// A second vector.
/// The first vector minus the second vector
public static PlanktonXYZ operator -(PlanktonXYZ v1, PlanktonXYZ v2)
{
return new PlanktonXYZ(v1._x - v2._x, v1._y - v2._y, v1._z - v2._z);
}
///
/// Multiplies a vector by a number, having the effect of scaling it.
///
/// A vector.
/// A number.
/// A new vector that is the original vector coordinatewise multiplied by t.
public static PlanktonXYZ operator *(PlanktonXYZ vector, float t)
{
return new PlanktonXYZ(vector._x * t, vector._y * t, vector._z * t);
}
///
/// Computes the cross product (or vector product, or exterior product) of two vectors.
/// This operation is not commutative.
///
/// First vector.
/// Second vector.
/// A new vector that is perpendicular to both a and b,
/// has Length == a.Length * b.Length and
/// with a result that is oriented following the right hand rule.
///
public static PlanktonXYZ CrossProduct(PlanktonXYZ a, PlanktonXYZ b)
{
return new PlanktonXYZ(a._y * b._z - b._y * a._z, a._z * b._x - b._z * a._x, a._x * b._y - b._x * a._y);
}
///
/// Get the length of a vector
///
/// The length
public float Length
{
get { return (float)Math.Sqrt(this._x * this._x + this._y * this._y + this._z * this._z); }
}
public override string ToString()
{
return string.Format("{0}, {1}, {2}", this._x, this._y, this._z);
}
///
/// Determines whether two vectors have equal values.
///
/// The first vector.
/// The second vector.
/// true if the components of the two vectors are exactly equal; otherwise false.
public static bool operator ==(PlanktonXYZ a, PlanktonXYZ b)
{
return (a._x == b._x && a._y == b._y && a._z == b._z);
}
///
/// Determines whether two vectors have different values.
///
/// The first vector.
/// The second vector.
/// true if the two vectors differ in any component; false otherwise.
public static bool operator !=(PlanktonXYZ a, PlanktonXYZ b)
{
return (a._x != b._x || a._y != b._y || a._z != b._z);
}
///
/// Determines whether the specified System.Object is a Vector3f and has the same values as the present vector.
///
/// The specified object.
/// true if obj is Vector3f and has the same components as this; otherwise false.
public override bool Equals(object obj)
{
return (obj is PlanktonXYZ && this == (PlanktonXYZ)obj);
}
///
/// Determines whether the specified vector has the same values as the present vector.
///
/// The specified vector.
/// true if vector has the same components as this; otherwise false.
public bool Equals(PlanktonXYZ vector)
{
return this == vector;
}
}
}