struct ThreeDViewer.Mathematics.Vector3¶
Overview¶
Represents a 3D vector. More…
struct Vector3: IComparable, IComparable< Vector3 >, IEquatable< Vector3 > { // fields static readonly Vector3 Origin = new(0, 0, 0); static readonly Vector3 XAxis = new(1, 0, 0); static readonly Vector3 YAxis = new(0, 1, 0); static readonly Vector3 ZAxis = new(0, 0, 1); static readonly Vector3 MinValue = new(double.MinValue, double.MinValue, double.MinValue); static readonly Vector3 MaxValue = new(double.MaxValue, double.MaxValue, double.MaxValue); static readonly Vector3 Epsilon = new(double.Epsilon, double.Epsilon, double.Epsilon); static readonly Vector3 Zero = Origin; static readonly Vector3 NaN = new(double.NaN, double.NaN, double.NaN); // properties double X; double Y; double Z; double[] Array; double Magnitude; double SqrMagnitude; Vector3 Normalized; double this[int index]; // methods Vector3(double x, double y, double z); Vector3(double[] xyz); Vector3(Vector3 v1); double Dot(Vector3 other); Vector3 Cross(Vector3 other); bool IsUnitVector(); bool IsUnitVector(double tolerance); Vector3 Normalize(); bool IsNaN(); Vector3 NormalizeOrDefault(); double Distance(Vector3 other); double SumComponents(); Vector3 SqrComponents(); override string ToString(); override bool Equals(object other); bool Equals(Vector3 other); bool Equals(object other, double tolerance); bool Equals(Vector3 other, double tolerance); override int GetHashCode(); int CompareTo(object other); int CompareTo(Vector3 other); int CompareTo(object other, double tolerance); bool IsBackFace(Vector3 lineOfSight); PointF ToPointF(); Vector4 ToVector4(); static Vector3 operator + (Vector3 v1, Vector3 v2); static Vector3 operator - (Vector3 v1, Vector3 v2); static Vector3 operator - (Vector3 v1); static Vector3 operator + (Vector3 v1); static bool operator < (Vector3 v1, Vector3 v2); static bool operator <= (Vector3 v1, Vector3 v2); static bool operator > (Vector3 v1, Vector3 v2); static bool operator >= (Vector3 v1, Vector3 v2); static bool operator == (Vector3 v1, Vector3 v2); static bool operator != (Vector3 v1, Vector3 v2); static Vector3 operator * (Vector3 v1, double s2); static Vector3 operator * (double s1, Vector3 v2); static Vector3 operator/ (Vector3 v1, double s2); static Vector3 operator * (Vector3 v1, Vector3 v2); static double Dot(Vector3 v1, Vector3 v2); static Vector3 Cross(Vector3 v1, Vector3 v2); static bool IsUnitVector(Vector3 v1); static bool IsUnitVector(Vector3 v1, double tolerance); static Vector3 Normalize(Vector3 v1); static bool IsNaN(Vector3 v1); static Vector3 NormalizeOrDefault(Vector3 v1); static double Angle(Vector3 v1, Vector3 v2); static double Abs(Vector3 v1); static double Distance(Vector3 v1, Vector3 v2); static double SumComponents(Vector3 v1); static Vector3 PowComponents(Vector3 v1, double power); static Vector3 SqrtComponents(Vector3 v1); static Vector3 SqrComponents(Vector3 v1); static bool IsBackFace(Vector3 normal, Vector3 lineOfSight); };
Detailed Documentation¶
Represents a 3D vector.
Based on this tutorial : https://www.codeproject.com/articles/17425/a-vector-type-for-c.
Fields¶
static readonly Vector3 Origin = new(0, 0, 0)
An origin vector. Is equal to Vector(0, 0, 0). Same as Zero.
static readonly Vector3 XAxis = new(1, 0, 0)
Represents the X axis. Is equal to Vector(1, 0, 0).
static readonly Vector3 YAxis = new(0, 1, 0)
Represents the Y axis. Is equal to Vector(0, 1, 0).
static readonly Vector3 ZAxis = new(0, 0, 1)
Represents the Z axis. Is equal to Vector(0, 0, 1).
static readonly Vector3 MinValue = new(double.MinValue, double.MinValue, double.MinValue)
A vector with every components equal to double.MinValue.
static readonly Vector3 MaxValue = new(double.MaxValue, double.MaxValue, double.MaxValue)
A vector with every components equal to double.MaxValue.
static readonly Vector3 Epsilon = new(double.Epsilon, double.Epsilon, double.Epsilon)
A vector with every components equal to double.Epsilon.
static readonly Vector3 Zero = Origin
A zero vector. Is equal to Vector(0, 0, 0). Same as Origin.
static readonly Vector3 NaN = new(double.NaN, double.NaN, double.NaN)
A vector with every components equal to double.NaN.
Properties¶
double X
Gets the X component of the vector.
double Y
Gets the Y component of the vector.
double Z
Gets the Z component of the vector.
double[] Array
Gets the vector as an array.
double Magnitude
Gets the magnitude (aka. length or absolute value) of the vector.
double SqrMagnitude
Gets the squared magnitude of this vector, can be used for better performance than Magnitude.
Vector3 Normalized
Gets this vector but normalized.
double this[int index]
An index accessor for a vector, mapping index [0] -> X, [1] -> Y and [2] -> Z.
Parameters:
index |
The array index referring to a component within the vector (i.e. x, y, z). |
Returns:
Returns X if 0, Y if 1 and Z if 2.
Methods¶
Vector3(double x, double y, double z)
Initializes a new instance of the Vector3 struct.
Parameters:
x |
X component of the vector. |
y |
Y component of the vector. |
z |
Z component of the vector. |
Vector3(double[] xyz)
Initializes a new instance of the Vector3 struct.
Parameters:
xyz |
Array with the X, Y and Z components fo the vector. |
System.ArgumentException |
Thrown if the array argument does not contain exactly three components. |
Vector3(Vector3 v1)
Initializes a new instance of the Vector3 struct.
Parameters:
v1 |
Vector3 representing the new values for the vector. |
double Dot(Vector3 other)
Determines the dot product of two vectors.
Parameters:
other |
The vector to multiply by. |
Returns:
Returns a scalar representing the dot product of the two vectors.
Vector3 Cross(Vector3 other)
Determine the cross product of two Vectors. Determine the vector product. Determine the normal vector (Vector3 90° to the plane).
Parameters:
other |
The vector to multiply by. |
Returns:
Vector3 representig the cross product of the two vectors.
bool IsUnitVector()
Checks if the vector is a unit vector. Checks if the vector has be normalized. Checks if the vector has a magnitude of 1.
Returns:
Returns true if the vector is a unit vector.
bool IsUnitVector(double tolerance)
Checks if the vector is a unit vector within a tolerance. Checks if the vector has been normalized within a tolerance. Checks if the vector has a magnitude of 1 within a tolerance.
Parameters:
tolerance |
The tolerance to use when comparing the magnitude. |
Returns:
Returns true if the vector is a unit vector.
Vector3 Normalize()
Gets the normalized unit vector with a magnitude of one.
Exceptions will be thrown if the vector being normalized has a magnitude of 0 or of NaN.
Parameters:
NormalizeVectorException |
Thrown when the normalisation of a zero magnitude vector is attempted. |
NormalizeVectorException |
Thrown when the normalisation of a NaN magnitude vector is attempted. |
Returns:
Returns the normalized vector.
bool IsNaN()
Checks if any component of a vector is Not A Number (NaN).
Returns:
Returns true if the vector has NaN components.
Vector3 NormalizeOrDefault()
Gets the normalized unit vector with a magnitude of one.
Returns:
Returns Vector (0, 0, 0) if the magnitude is zero, Vector (NaN, NaN, NaN) if magnitude is NaN, or normalized vector.
double Distance(Vector3 other)
Computes the distance between two vectors.
Parameters:
other |
The vector to find the distance to. |
Returns:
Returns the distance between two vectors.
double SumComponents()
Sums the components of the vector.
Returns:
The sums of the vector’s X, Y and Z components.
Vector3 SqrComponents()
The vectors’s components squared.
Returns:
The squared vectors.
bool Equals(object other, double tolerance)
Comparator within a tolerance.
Parameters:
other |
The other object to compare to. |
tolerance |
The tolerance to use when comparing the vector components. |
Returns:
True if two objects are Vector3s and are equal within a tolerance.
bool Equals(Vector3 other, double tolerance)
Comparator within a tolerance.
Parameters:
other |
The other vector to compare to. |
tolerance |
The tolerance to use when comparing the vector components. |
Returns:
True if two vectors are equal within a tolerance.
int CompareTo(object other, double tolerance)
Compares the magnitude of this instance against the magnitude of the supplied vector.
Comparing two vectors has no meaning, we are comparing the magnitude of two vectors for convinience. It would be more accurate to compare the magnitudes explicitly using v1.Magnitude.CompareTo(v2.Magnitude).
Parameters:
other |
The vector to compare this instance with. |
tolerance |
Tolerence to use when comparing the two vectors. |
Returns:
-1: The magnitude of this instance is less than the others magnitude. 0: The magnitude of this instance equals the magnitude of the other. 1: The magnitude of this instance is greater than the magnitude of the other.
bool IsBackFace(Vector3 lineOfSight)
Checks if a face normal vector represents back face. Checks if a face is visible, given the line of sight.
Parameters:
lineOfSight |
The unit vector representing the direction of sight from a virtual camera. |
Returns:
True if the vector (as a normal) represents a back-face.
PointF ToPointF()
Converts this vector to a PointF. Only keeps the X and Y components of this vector.
Returns:
A point with the X and Y components of this vector.
Vector4 ToVector4()
Converts this vector to a Vector4. The Vector4.W component will be equal to 1.
Returns:
static double Dot(Vector3 v1, Vector3 v2)
Determines the dot product of two vectors.
Parameters:
v1 |
The vector to multiply. |
v2 |
The vector to multiply by. |
Returns:
Returns a scalar representing the dot product of the two vectors.
static Vector3 Cross(Vector3 v1, Vector3 v2)
Determine the cross product of two Vectors. Determine the vector product. Determine the normal vector (Vector3 90° to the plane).
Parameters:
v1 |
The vector to multiply. |
v2 |
The vector to multiply by. |
Returns:
Vector3 representig the cross product of the two vectors.
static bool IsUnitVector(Vector3 v1)
Checks if the vector is a unit vector. Checks if the vector has be normalized. Checks if the vector has a magnitude of 1.
Parameters:
v1 |
The vector to be checked for normalization. |
Returns:
Returns true if the vector is a unit vector.
static bool IsUnitVector(Vector3 v1, double tolerance)
Checks if the vector is a unit vector within a tolerance. Checks if the vector has been normalized within a tolerance. Checks if the vector has a magnitude of 1 within a tolerance.
Parameters:
v1 |
The vector to be checked for normalization. |
tolerance |
The tolerance to use when comparing the magnitude. |
Returns:
Returns true if the vector is a unit vector.
static Vector3 Normalize(Vector3 v1)
Gets the normalized unit vector with a magnitude of one.
Exceptions will be thrown if the vector being normalized has a magnitude of 0 or of NaN.
Parameters:
v1 |
The vector to be normalized. |
NormalizeVectorException |
Thrown when the normalisation of a zero magnitude vector is attempted. |
NormalizeVectorException |
Thrown when the normalisation of a NaN magnitude vector is attempted. |
Returns:
Returns the normalized vector.
static bool IsNaN(Vector3 v1)
Checks if any component of a vector is Not A Number (NaN).
Parameters:
v1 |
The vector checked for NaN components. |
Returns:
Returns true if the vector has NaN components.
static Vector3 NormalizeOrDefault(Vector3 v1)
Gets the normalized unit vector with a magnitude of one.
Parameters:
v1 |
The vector to be normalized. |
Returns:
Returns Vector (0,0,0) if the magnitude is zero, Vector (NaN, NaN, NaN) if magnitude is NaN, or normalized vector.
static double Angle(Vector3 v1, Vector3 v2)
Computes the angle between two vectors.
Parameters:
v1 |
The vector to discern the angle from. |
v2 |
The vector to discern the angle to. |
Returns:
Returns the angle between the two vectors.
static double Abs(Vector3 v1)
Finds the absolute value of a vector. Finds the magnitude of a vector.
Parameters:
v1 |
The vector to get the magnitude from. |
Returns:
Returns a double representig the magnitude of the vector.
static double Distance(Vector3 v1, Vector3 v2)
Computes the distance between two vectors.
Parameters:
v1 |
The vector to find the distance from. |
v2 |
The vector to find the distance to. |
Returns:
Returns the distance between two vectors.
static double SumComponents(Vector3 v1)
Sums the components of the vector.
Parameters:
v1 |
The vector whose scalar components to sum. |
Returns:
The sums of the vector’s X, Y and Z components.
static Vector3 PowComponents(Vector3 v1, double power)
The individual multiplication to a power of the vectors’s components.
Parameters:
v1 |
The vector whose scalar components to multiply by a power. |
power |
The power by which to multiply the components. |
Returns:
The multiplied vector.
static Vector3 SqrtComponents(Vector3 v1)
The individual square root of a vectors’s components.
Parameters:
v1 |
The vector whose scalar components to square root. |
Returns:
The rooted vector.
static Vector3 SqrComponents(Vector3 v1)
The vectors’s components squared.
Parameters:
v1 |
The vector whose scalar components are to square. |
Returns:
The squared vectors.
static bool IsBackFace(Vector3 normal, Vector3 lineOfSight)
Checks if a face normal vector represents back face. Checks if a face is visible, given the line of sight.
Parameters:
normal |
The vector representing the face normal Vector3. |
lineOfSight |
The unit vector representing the direction of sight from a virtual camera. |
Returns:
True if the vector (as a normal) represents a back-face.