2017-10-03 19:17:35 -04:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Text.RegularExpressions;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
namespace SharedLibrary.Helpers
|
|
|
|
|
{
|
|
|
|
|
public class Vector3
|
|
|
|
|
{
|
2018-03-22 14:50:09 -04:00
|
|
|
|
public float X { get; protected set; }
|
|
|
|
|
public float Y { get; protected set; }
|
|
|
|
|
public float Z { get; protected set; }
|
2017-10-03 19:17:35 -04:00
|
|
|
|
|
|
|
|
|
public Vector3(float x, float y, float z)
|
|
|
|
|
{
|
|
|
|
|
X = x;
|
|
|
|
|
Y = y;
|
|
|
|
|
Z = z;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override string ToString()
|
|
|
|
|
{
|
|
|
|
|
return $"({X}, {Y}, {Z})";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static Vector3 Parse(string s)
|
|
|
|
|
{
|
|
|
|
|
bool valid = Regex.Match(s, @"\(-?[0-9]+.?[0-9]*,\ -?[0-9]+.?[0-9]*,\ -?[0-9]+.?[0-9]*\)").Success;
|
|
|
|
|
if (!valid)
|
|
|
|
|
throw new FormatException("Vector3 is not in correct format");
|
|
|
|
|
|
|
|
|
|
string removeParenthesis = s.Substring(1, s.Length - 2);
|
|
|
|
|
string[] eachPoint = removeParenthesis.Split(',');
|
|
|
|
|
return new Vector3(float.Parse(eachPoint[0]), float.Parse(eachPoint[1]), float.Parse(eachPoint[2]));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static double Distance(Vector3 a, Vector3 b)
|
|
|
|
|
{
|
2018-03-26 00:51:25 -04:00
|
|
|
|
return Math.Sqrt(Math.Pow(b.X - a.X, 2) + Math.Pow(b.Y - a.Y, 2) + Math.Pow(b.Z - a.Z, 2));
|
2017-10-03 19:17:35 -04:00
|
|
|
|
}
|
2018-03-13 17:30:22 -04:00
|
|
|
|
|
2018-03-22 14:50:09 -04:00
|
|
|
|
public static Vector3 Subtract(Vector3 a, Vector3 b) => new Vector3(b.X - a.X, b.Y - a.Y, b.Z - a.Z);
|
|
|
|
|
|
2018-03-13 17:30:22 -04:00
|
|
|
|
public double DotProduct(Vector3 a) => (a.X * this.X) + (a.Y * this.Y) + (a.Z * this.Z);
|
|
|
|
|
|
|
|
|
|
public double Magnitude() => Math.Sqrt((X * X) + (Y * Y) + (Z * Z));
|
|
|
|
|
|
|
|
|
|
public double AngleBetween(Vector3 a) => Math.Acos(this.DotProduct(a) / (a.Magnitude() * this.Magnitude()));
|
|
|
|
|
|
2017-10-03 19:17:35 -04:00
|
|
|
|
}
|
|
|
|
|
}
|