struct ThreeDViewer.Mathematics.Vector4

Overview

Represents a 3D position vector with homogeneous coordinates. More…

struct Vector4: IEquatable< Vector4 >
{
    // fields

    static readonly int SizeInBytes = Unsafe.SizeOf<Vector4>();
    static readonly Vector4 Origin = new(0, 0, 0, 0);
    static readonly Vector4 XAxis = new(1, 0, 0, 0);
    static readonly Vector4 YAxis = new(0, 1, 0, 0);
    static readonly Vector4 ZAxis = new(0, 0, 1, 0);
    static readonly Vector4 WAxis = new(0, 0, 0, 1);
    static readonly Vector4 MinValue = new(double.MinValue, double.MinValue, double.MinValue, double.MinValue);
    static readonly Vector4 MaxValue = new(double.MaxValue, double.MaxValue, double.MaxValue, double.MaxValue);
    static readonly Vector4 Epsilon = new(double.Epsilon, double.Epsilon, double.Epsilon, double.Epsilon);
    static readonly Vector4 One = new(1, 1, 1, 1);
    static readonly Vector4 Zero = Origin;
    static readonly Vector4 NaN = new(double.NaN, double.NaN, double.NaN, double.NaN);

    // properties

    double X;
    double Y;
    double Z;
    double W;
    double[] Array;
    double Magnitude;
    double MagnitudeSquared;
    Vector4 Normalized;
    double this[int index];

    // methods

    Vector4(double x, double y, double z, double w);
    Vector4(double[] xyzw);
    Vector4(Vector4 v1);
    Vector4(Vector3 v1);
    Vector4(Vector3 v1, double w);
    double Dot(Vector4 other);
    Vector4 SquareComponents();
    double SumComponents();
    Vector4 Normalize();
    Vector3 ToPhysicalCoords();
    PointF ToPointF();
    bool Equals(Vector4 other);
    override bool Equals(object obj);
    override int GetHashCode();
    static Vector4 operator + (Vector4 v1, Vector4 v2);
    static Vector4 operator - (Vector4 v1, Vector4 v2);
    static Vector4 operator - (Vector4 v1);
    static Vector4 operator + (Vector4 v1);
    static bool operator < (Vector4 v1, Vector4 v2);
    static bool operator <= (Vector4 v1, Vector4 v2);
    static bool operator > (Vector4 v1, Vector4 v2);
    static bool operator >= (Vector4 v1, Vector4 v2);
    static bool operator == (Vector4 v1, Vector4 v2);
    static bool operator != (Vector4 v1, Vector4 v2);
    static Vector4 operator * (Vector4 v1, double s2);
    static Vector4 operator * (double s1, Vector4 v2);
    static Vector4 operator/ (Vector4 v1, double s2);
    static Vector4 operator * (Vector4 v1, Matrix4 m2);
};

Detailed Documentation

Represents a 3D position vector with homogeneous coordinates.

Based on this tutorial : https://www.codeproject.com/articles/17425/a-vector-type-for-c.

Fields

static readonly int SizeInBytes = Unsafe.SizeOf<Vector4>()

Size of the Vector4 struct in bytes.

static readonly Vector4 Origin = new(0, 0, 0, 0)

An origin vector. Is equal to Vector(0, 0, 0, 0). Same as Zero.

static readonly Vector4 XAxis = new(1, 0, 0, 0)

Represents the X axis. Is equal to Vector(1, 0, 0, 0).

static readonly Vector4 YAxis = new(0, 1, 0, 0)

Represents the Y axis. Is equal to Vector(0, 1, 0, 0).

static readonly Vector4 ZAxis = new(0, 0, 1, 0)

Represents the Z axis. Is equal to Vector(0, 0, 1, 0).

static readonly Vector4 WAxis = new(0, 0, 0, 1)

Represents the W axis. Is equal to Vector(0, 0, 0, 1).

static readonly Vector4 MinValue = new(double.MinValue, double.MinValue, double.MinValue, double.MinValue)

A vector with every components equal to double.MinValue.

static readonly Vector4 MaxValue = new(double.MaxValue, double.MaxValue, double.MaxValue, double.MaxValue)

A vector with every components equal to double.MaxValue.

static readonly Vector4 Epsilon = new(double.Epsilon, double.Epsilon, double.Epsilon, double.Epsilon)

A vector with every components equal to double.Epsilon.

static readonly Vector4 One = new(1, 1, 1, 1)

An instance with all components set to 1.

static readonly Vector4 Zero = Origin

A zero vector. Is equal to Vector(0, 0, 0, 0). Same as Origin.

static readonly Vector4 NaN = new(double.NaN, 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 W

Gets the W 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 MagnitudeSquared

Gets the squared magnitude of this vector, can be used for better performance than Magnitude.

Vector4 Normalized

Gets this vector but normalized.

double this[int index]

An index accessor for a vector, mapping index [0] -> X, [1] -> Y, [2] -> Z and [3] -> W.

Parameters:

index

The array index referring to a component within the vector (i.e. x, y, z, w).

Returns:

Returns X if 0, Y if 1, Z if 2 and W if 3.

Methods

Vector4(double x, double y, double z, double w)

Initializes a new instance of the Vector4 struct.

Parameters:

x

X component of the vector.

y

Y component of the vector.

z

Z component of the vector.

w

W component of the vector.

Vector4(double[] xyzw)

Initializes a new instance of the Vector4 struct.

Parameters:

xyzw

Array with the X, Y, Z and W components fo the vector.

ArgumentException

Thrown if the array argument does not contain exactly four components.

Vector4(Vector4 v1)

Initializes a new instance of the Vector4 struct.

Parameters:

v1

Vector4 representing the new values for the vector.

Vector4(Vector3 v1)

Initializes a new instance of the Vector4 struct.

Parameters:

v1

Vector3 to create the Vector4 from.

Vector4(Vector3 v1, double w)

Initializes a new instance of the Vector4 struct.

Parameters:

v1

The vector 3 that contains the X, Y and Z components of that vector.

w

The W element of the new vector.

double Dot(Vector4 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.

Vector4 SquareComponents()

Squares the vector’s components.

Returns:

The vector with his components squared.

double SumComponents()

Sums the components of the vector.

Returns:

Returns the sum of the components of the vectors.

Vector4 Normalize()

Normalizes the vector.

Returns:

Returns the normalized vector.

Vector3 ToPhysicalCoords()

Converts the Vector4 to physical coords in a Vector3. Divides the X, Y and Z components by W. If W = 0, it just gives the X, Y and Z components in a Vector3.

Returns:

Returns the physical coordinates of this vector.

PointF ToPointF()

Converts this vector to a PointF. Firsts gets his physical coordinates, then only keeps the X and Y components.

Returns:

A PointF containing the physical X and Y coordinates of the vector.

static Vector4 operator * (Vector4 v1, Matrix4 m2)

Multiplies a vector with a matrix.

Parameters:

v1

The vector to multiply by the matrix.

m2

The matrix to multiply the vector by.

Returns:

The vector that results from the multiplication.