| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238 |
- 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") +"]";
- }
- }
- }
|