Jump To …

PlanktonXYZ.cs

using System;

namespace Plankton
{
#

PlanktonXYZ

Represents a vector in Euclidean space.

    public struct PlanktonXYZ
    {
        #region members
        internal float _x;
        internal float _y;
        internal float _z;
        #endregion

        #region constructors
#

.ctor

Constructs a new vector from 3 single precision numbers.

Parameters

  • x: X component of vector.
  • y: Y component of vector.
  • z: Z component of vector.
        public PlanktonXYZ(float x, float y, float z)
        {
            _x = x;
            _y = y;
            _z = z;
        }
        #endregion

        #region static properties
#

Zero

Gets the value of the vector with components 0,0,0.

        public static PlanktonXYZ Zero
        {
            get { return new PlanktonXYZ(); }
        }
#

XAxis

Gets the value of the vector with components 1,0,0.

        public static PlanktonXYZ XAxis
        {
            get { return new PlanktonXYZ(1f, 0f, 0f); }
        }
#

YAxis

Gets the value of the vector with components 0,1,0.

        public static PlanktonXYZ YAxis
        {
            get { return new PlanktonXYZ(0f, 1f, 0f); }
        }
#

ZAxis

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
#

X

Gets or sets the X (first) component of this vector.

        public float X { get { return _x; } set { _x = value; } }
#

Y

Gets or sets the Y (second) component of this vector.

        public float Y { get { return _y; } set { _y = value; } }
#

Z

Gets or sets the Z (third) component of this vector.

        public float Z { get { return _z; } set { _z = value; } }
        #endregion
#

GetHashCode

Computes a hash number that represents the current vector.

Returns

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();
        }
#

operator

Sums up two vectors.

Parameters

  • v1: A vector.
  • v2: A second vector.

Returns

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);
        }
#

operator

Subtracts one vector from another.

Parameters

  • v1: A vector.
  • v2: A second vector.

Returns

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);
        }
#

operator

Multiplies a vector by a number, having the effect of scaling it.

Parameters

  • vector: A vector.
  • t: A number.

Returns

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);
        }
#

CrossProduct

Computes the cross product (or vector product, or exterior product) of two vectors. This operation is not commutative.

Parameters

  • a: First vector.
  • b: Second vector.

Returns

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);
        }
#

Length

Get the length of a vector

Returns

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);
        }
#

operator

Determines whether two vectors have equal values.

Parameters

  • a: The first vector.
  • b: The second vector.

Returns

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);
        }
#

operator

Determines whether two vectors have different values.

Parameters

  • a: The first vector.
  • b: The second vector.

Returns

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);
        }
#

Equals

Determines whether the specified System.Object is a Vector3f and has the same values as the present vector.

Parameters

  • obj: The specified object.

Returns

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);
        }
#

Equals

Determines whether the specified vector has the same values as the present vector.

Parameters

  • vector: The specified vector.

Returns

true if vector has the same components as this; otherwise false.

        public bool Equals(PlanktonXYZ vector)
        {
            return this == vector;
        }
    }
}