Skip to content

Vectors

Stuart Aitken edited this page Jun 15, 2020 · 15 revisions

The Vector Class

Source: Vector.cs

namespace: UniversityPhysics.Maths


Properties

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

Constructors

Default

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

Vector v1 = new Vector(); 

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

2D Vector

Creates a new vector taking X and Y values as parameters.

Note: The Z value of the vector is set automatically to 0. There are no purely 2D Vectors in University Physics. There are too many cases where 2D and 3D vectors find themselves overlapping, for example the cross product of two 2D vectors is intrinsically 3D.

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

Abs()

Calculates the absolute value (magnitude) of the vector

returns: double

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

double magnitude = A.Abs();

// magnitude = 1.4142... (√2)

ToString()

Returns a string representation of the X, Y and Z values of the vector

returns: string

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

string result = A.ToString();

// result = "X: 1, Y: 1, Z: 0";

Operators

Addition operator +

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 operator -

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 operator *

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 operator /

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

Equal operator ==

Tests for numerical equality between two vectors

returns: bool

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

bool result = A == B;

// result = True;

Not Equal operator !=

Tests for numerical inequality between two vectors

returns: bool

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

bool result = A != B;

// result = True;