using System.Drawing; using System.Text.Json.Serialization; namespace SOTF_Struct.Generic { public class Position { private double _x; private double _y; private double _z; public Position() { } public Position(double x, double y, double z) { this.x = x; this.y = y; this.z = z; } public Position(double[] pos) { this.x = pos[0]; this.y = pos[1]; this.z = pos[2]; } public Point ToPoint() { return new Point((int)this.x, (int)this.z); } [JsonIgnore(Condition = JsonIgnoreCondition.Never)] public double x { get => this._x; set { if (value > 4000) this._x = value % 2048; else if (value < 4000) this._x = value % -4000; else this._x = value; } } [JsonIgnore(Condition = JsonIgnoreCondition.Never)] public double y { get => this._y; set { if (value > 4000) this._y = value % 2048; else if (value < 4000) this._y = value % -4000; else this._y = value; } } [JsonIgnore(Condition = JsonIgnoreCondition.Never)] public double z { get => this._z; set { if (value > 4000) this._z = value % 2048; else if (value < 4000) this._z = value % -4000; else this._z = value; } } public Position Infront(Rotation rot, double Distance) { return new Position(this.x + (Math.Sin(rot.y * Math.PI) * (Distance / 10)), this.y+0.2, this.z + (Math.Cos(rot.y * Math.PI)* (Distance / 10))); } public Position Infront(double[] rot, double Distance) { return new Position(this.x + (Math.Sin(rot[1] * Math.PI) * (Distance / 10)), this.y + 0.2, this.z + (Math.Cos(rot[1] * Math.PI) * (Distance / 10))); } public override bool Equals(object? obj) { if (obj.GetType() == typeof(Position)) return ((Position)obj).x == this.x && ((Position)obj).y == this.y && ((Position)obj).z == this.z; if (obj.GetType() == typeof(float[])) return ((float[])obj).Length == 3 && ((float[])obj)[0] == this.x && ((float[])obj)[1] == this.y && ((float[])obj)[2] == this.z; if (obj.GetType() == typeof(double[])) return ((double[])obj).Length == 3 && ((double[])obj)[0] == this.x && ((double[])obj)[1] == this.y && ((double[])obj)[2] == this.z; return false; } public override int GetHashCode() { return (int)Math.Round((Math.Round(this.x,1) * 90000) + (Math.Round(this.z,1) * 40) + (((double)this.y * 10) % 39), 0); } public double DistanceTo(Position pos) { double deltax = this.x - pos.x; double deltay = this.y - pos.y; double deltaz = this.z - pos.z; return (double)Math.Sqrt( (deltax * deltax) + (deltay * deltay) + (deltaz * deltaz) ); } public double DistanceTo(float[] pos) { if (pos.Length == 2) { double deltax = this.x - pos[0]; double deltaz = this.z - pos[1]; return (double)Math.Sqrt( (deltax * deltax) + (deltaz * deltaz) ); } else if(pos.Length>2) { double deltax = this.x - pos[0]; double deltay = this.y - pos[1]; double deltaz = this.z - pos[2]; return (double)Math.Sqrt( (deltax * deltax) + (deltay * deltay) + (deltaz * deltaz) ); } return 0; } public double DistanceTo(double[] pos) { if (pos.Length == 2) { double deltax = this.x - pos[0]; double deltaz = this.z - pos[1]; return (double)Math.Sqrt( (deltax * deltax) + (deltaz * deltaz) ); } else if (pos.Length > 2) { double deltax = this.x - pos[0]; double deltay = this.y - pos[1]; double deltaz = this.z - pos[2]; return (double)Math.Sqrt( (deltax * deltax) + (deltay * deltay) + (deltaz * deltaz) ); } return 0; } public double DistanceTo(double x, double z) { double deltax = this.x - x; double deltaz = this.z - z; return (double)Math.Sqrt( (deltax * deltax) + (deltaz * deltaz) ); } public double DistanceTo(Point p) { double deltax = this.x - p.X; double deltaz = this.z - p.Y; return (double)Math.Sqrt( (deltax * deltax) + (deltaz * deltaz) ); } public double DistanceTo(PointF p) { double deltax = this.x - p.X; double deltaz = this.z - p.Y; return (double)Math.Sqrt( (deltax * deltax) + (deltaz * deltaz) ); } public double DistanceTo(double x, double y, double z) { double deltax = this.x - x; double deltay = this.y - y; double deltaz = this.z - z; return (double)Math.Sqrt( (deltax * deltax) + (deltay * deltay) + (deltaz * deltaz) ); } public static double operator %(Position a, Position b) => a.DistanceTo(b); public static double operator %(Position a, double[] b) => a.DistanceTo(b); public static double operator %(Position a, float[] b) => a.DistanceTo(b); public static Position operator +(Position a, Position b) => new Position( a.x+b.x, a.y+b.y, a.z+b.z ); public static Position operator +(Position a, double[] b) => new Position( a.x - b[0], a.y - (b.Length == 3 ? b[1] : 0), a.z - (b.Length == 3 ? b[2] : b[1]) ); public static Position operator +(Position a, double b) => new Position( a.x + b, a.y, a.z + b ); public static Position operator +(Position a, float[] b) => new Position( a.x - b[0], a.y - (b.Length == 3 ? b[1] : 0), a.z - (b.Length == 3 ? b[2] : b[1]) ); public static Position operator -(Position a, Position b) => new Position( a.x - b.x, a.y - b.y, a.z - b.z ); public static Position operator -(Position a, double[] b) => new Position( a.x - b[0], a.y - (b.Length == 3? b[1] : 0), a.z - (b.Length == 3? b[2] : b[1]) ); public static Position operator -(Position a, double b) => new Position( a.x - b, a.y, a.z - b ); public static Position operator -(Position a, float[] b) => new Position( a.x - b[0], a.y - (b.Length == 3 ? b[1] : 0), a.z - (b.Length == 3 ? b[2] : b[1]) ); //public static float operator -(float[] a, Position b) => b.DistanceTo(a); public override string ToString() { return "["+this.x.ToString("0.000") + " x " + this.y.ToString("0.000") + " x " + this.z.ToString("0.000") +"]"; } } }