94 lines
2.9 KiB
C#

using System;
using System.Collections.Generic;
using System.Numerics;
public class MyVector
{
public float X { get; private set; }
public float Y { get; private set; }
public float Z { get; private set; }
// this W component has only been included to make it easier to do Matrix multiplications
public float W { get; private set; }
public MyVector(float pX, float pY, float pZ, float pW = 1)
{
this.W = pW;
this.X = pX;
this.Y = pY;
this.Z = pZ;
}
public MyVector Add(MyVector pVector)
{
Vector3 firstVector = new Vector3(X, Y, Z);
Vector3 secondVector = new Vector3(pVector.X, pVector.Y,pVector.Z);
Vector3 finishedVector = firstVector + secondVector;
return new MyVector(finishedVector.X, finishedVector.Y, finishedVector.Z);
}
public MyVector Subtract(MyVector pVector)
{
Vector3 firstVector = new Vector3(X, Y, Z);
Vector3 secondVector = new Vector3(pVector.X, pVector.Y, pVector.Z);
Vector3 finishedVector = firstVector - secondVector;
return new MyVector(finishedVector.X, finishedVector.Y, finishedVector.Z);
}
public MyVector Multiply(float pScalar)
{
Vector3 firstVector = new Vector3(X*pScalar, Y*pScalar, Z*pScalar);
return new MyVector(firstVector.X,firstVector.Y,firstVector.Z);
}
public MyVector Divide(float pScalar)
{
Vector3 firstVector = new Vector3(X / pScalar, Y / pScalar, Z / pScalar);
return new MyVector(firstVector.X, firstVector.Y, firstVector.Z);
}
public float Magnitude()
{
float squareSum = (float)Math.Pow(X, 2) + (float)Math.Pow(Y, 2) + (float)Math.Pow(Z, 2);
float SquareRoot = (float)Math.Sqrt(squareSum);
return SquareRoot;
}
public MyVector Normalise()
{
float mag = Magnitude();
return new MyVector(X / mag, Y / mag, Z / mag);
}
public float DotProduct(MyVector pVector)
{
float dotProduct = (X * pVector.X) + (Y * pVector.Y) + (Z * pVector.Z);
return dotProduct;
}
public MyVector Interpolate(MyVector pVector, float pInterpolation)
{
return new MyVector(
X + (pVector.X - X) * pInterpolation,
Y + (pVector.Y - Y) * pInterpolation,
Z + (pVector.Z - Z) * pInterpolation
);
}
public float AngleBetween(MyVector pVector)
{
float dot = DotProduct(pVector);
float magA = Magnitude();
float magB = pVector.Magnitude();
float cosAngle = dot / (magA * magB);
cosAngle = Math.Clamp(cosAngle, -1.0f, 1.0f);
return (float)Math.Acos(cosAngle);
}
public MyVector CrossProduct(MyVector pVector)
{
return new MyVector(
(Y * pVector.Z) - (Z * pVector.Y),
(Z * pVector.X) - (X * pVector.Z),
(X * pVector.Y) - (Y * pVector.X)
);
}
}