Skip to content

Vectors

Stuart Aitken edited this page Oct 6, 2019 · 15 revisions

Vectors

Properties

double X { get; set; }
double Y { get; set; }
double Z { get; set; }

Constructors

Default

Creates new vector positioned at origin (0, 0, 0)

Vector v1 = new Vector(); 

// ===> (0, 0, 0)

2D Vector

Creates a new vector taking X and Y values as parameters

Vector v2 = new Vector(12, 100.45); 

// v2 ===> (12, 100.45, 0)

3D Vector

Creates a new vector taking X, Y and Z values as parameters

Vector v3 = new Vector(12, 100.45 0.6);

// ===> (12, 100.45, 0.6)

Methods

Dot()

Computes the dot product between this Vector and any other Vector

returns: double

Vector A = new Vector(1, 1, 0);
Vector B = new Vector(0, 2, 2);

double dotProduct = A.Dot(B);

// ===> dotProduct = 2.0

Cross()

Computes the cross product between this Vector and any other Vector

returns: Vector

Vector A = new Vector(1, 1, 0);
Vector B = new Vector(0, 2, 2);

Vector cross = A.Cross(B);

// cross = new Vector(2, -2, 2);

NormaliseSelf()

Converts this vector into its normalised form

returns: void

Vector A = new Vector(1, 1, 0);

A.NormaliseSelf();

// A = Vector(0.70710..., 0.70710..., 0)'

Normalised()

Returns the normalised form of this Vector

returns: Vector

Vector A = new Vector(1, 1, 0);

Vector normalised = A.Normalised();

// normalised = Vector(0.70710..., 0.70710..., 0)

AngleBetween()

Calculates the angle between this Vector and any other Vector

Defaults to angle in radians. Can specify degrees in second parameter

returns: double

Vector A = new Vector(1, 1, 0);
Vector B = new Vector(0, 2, 2);


//Calculate angle in radians
double angleRads = A.AngleBetween(B);
// angleRads = 1.04666... (π/3)


//Calculate angle in degrees
double angleDegrees = A.AngleBetween(B, AngleType.Degrees);
// angleDegrees = 60

Operators

Addition

Mathematical addition of two vectors

returns: Vector

Vector A = new Vector(1, 1, 0);
Vector B = new Vector(0, 2, 2);

Vector result = A + B;

// result = Vector(1, 3, 2);

Subtraction

Mathematical subtraction of two vectors

returns: Vector

Vector A = new Vector(1, 1, 0);
Vector B = new Vector(0, 2, 2);

Vector result = A - B;

// result = Vector(1, -1, -2);

Multiplication

Numerical Multiplication

Multiplies a vector by a given numerical value

returns: Vector

Vector A = new Vector(1, 1, 0);
var x = 2; // int, double, float

Vector result = A * x;
// result = Vector(2, 2, 0);

Vector result2 = x * A;
// result2 = Vector(2, 2, 0);

Vector Multiplication

Returns the Cross Product between two vectors.

returns: Vector

Vector A = new Vector(1, 1, 0);
Vector B = new Vector(0, 2, 2);

Vector result = A * B;
// result = new Vector(2, -2, 2);

Vector result2 = B * A;
// result2 = new Vector(-2, 2, -2);

Division

Mathematical division of the X, Y and Z values of a vector

returns: Vector

Vector A = new Vector(1, 1, 0);
var x = 2; // int, double, float

Vector result = A / x;

// result = Vector(0.5, 0.5, 0);
Clone this wiki locally