Position.cs 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. using System.Drawing;
  2. using System.Text.Json.Serialization;
  3. namespace SOTF_Struct.Generic {
  4. public class Position {
  5. private double _x;
  6. private double _y;
  7. private double _z;
  8. public Position() {
  9. }
  10. public Position(double x, double y, double z) {
  11. this.x = x;
  12. this.y = y;
  13. this.z = z;
  14. }
  15. public Position(double[] pos) {
  16. this.x = pos[0];
  17. this.y = pos[1];
  18. this.z = pos[2];
  19. }
  20. public Point ToPoint() {
  21. return new Point((int)this.x, (int)this.z);
  22. }
  23. [JsonIgnore(Condition = JsonIgnoreCondition.Never)]
  24. public double x {
  25. get => this._x;
  26. set {
  27. if (value > 4000)
  28. this._x = value % 2048;
  29. else if (value < 4000)
  30. this._x = value % -4000;
  31. else
  32. this._x = value;
  33. }
  34. }
  35. [JsonIgnore(Condition = JsonIgnoreCondition.Never)]
  36. public double y {
  37. get => this._y;
  38. set {
  39. if (value > 4000)
  40. this._y = value % 2048;
  41. else if (value < 4000)
  42. this._y = value % -4000;
  43. else
  44. this._y = value;
  45. }
  46. }
  47. [JsonIgnore(Condition = JsonIgnoreCondition.Never)]
  48. public double z {
  49. get => this._z;
  50. set {
  51. if (value > 4000)
  52. this._z = value % 2048;
  53. else if (value < 4000)
  54. this._z = value % -4000;
  55. else
  56. this._z = value;
  57. }
  58. }
  59. public Position Infront(Rotation rot, double Distance) {
  60. 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)));
  61. }
  62. public Position Infront(double[] rot, double Distance) {
  63. 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)));
  64. }
  65. public override bool Equals(object? obj) {
  66. if (obj.GetType() == typeof(Position))
  67. return ((Position)obj).x == this.x && ((Position)obj).y == this.y && ((Position)obj).z == this.z;
  68. if (obj.GetType() == typeof(float[]))
  69. return ((float[])obj).Length == 3 && ((float[])obj)[0] == this.x && ((float[])obj)[1] == this.y && ((float[])obj)[2] == this.z;
  70. if (obj.GetType() == typeof(double[]))
  71. return ((double[])obj).Length == 3 && ((double[])obj)[0] == this.x && ((double[])obj)[1] == this.y && ((double[])obj)[2] == this.z;
  72. return false;
  73. }
  74. public override int GetHashCode() {
  75. return (int)Math.Round((Math.Round(this.x,1) * 90000) + (Math.Round(this.z,1) * 40) + (((double)this.y * 10) % 39), 0);
  76. }
  77. public double DistanceTo(Position pos) {
  78. double deltax = this.x - pos.x;
  79. double deltay = this.y - pos.y;
  80. double deltaz = this.z - pos.z;
  81. return (double)Math.Sqrt(
  82. (deltax * deltax) +
  83. (deltay * deltay) +
  84. (deltaz * deltaz)
  85. );
  86. }
  87. public double DistanceTo(float[] pos) {
  88. if (pos.Length == 2) {
  89. double deltax = this.x - pos[0];
  90. double deltaz = this.z - pos[1];
  91. return (double)Math.Sqrt(
  92. (deltax * deltax) +
  93. (deltaz * deltaz)
  94. );
  95. } else if(pos.Length>2) {
  96. double deltax = this.x - pos[0];
  97. double deltay = this.y - pos[1];
  98. double deltaz = this.z - pos[2];
  99. return (double)Math.Sqrt(
  100. (deltax * deltax) +
  101. (deltay * deltay) +
  102. (deltaz * deltaz)
  103. );
  104. }
  105. return 0;
  106. }
  107. public double DistanceTo(double[] pos) {
  108. if (pos.Length == 2) {
  109. double deltax = this.x - pos[0];
  110. double deltaz = this.z - pos[1];
  111. return (double)Math.Sqrt(
  112. (deltax * deltax) +
  113. (deltaz * deltaz)
  114. );
  115. } else if (pos.Length > 2) {
  116. double deltax = this.x - pos[0];
  117. double deltay = this.y - pos[1];
  118. double deltaz = this.z - pos[2];
  119. return (double)Math.Sqrt(
  120. (deltax * deltax) +
  121. (deltay * deltay) +
  122. (deltaz * deltaz)
  123. );
  124. }
  125. return 0;
  126. }
  127. public double DistanceTo(double x, double z) {
  128. double deltax = this.x - x;
  129. double deltaz = this.z - z;
  130. return (double)Math.Sqrt(
  131. (deltax * deltax) +
  132. (deltaz * deltaz)
  133. );
  134. }
  135. public double DistanceTo(Point p) {
  136. double deltax = this.x - p.X;
  137. double deltaz = this.z - p.Y;
  138. return (double)Math.Sqrt(
  139. (deltax * deltax) +
  140. (deltaz * deltaz)
  141. );
  142. }
  143. public double DistanceTo(PointF p) {
  144. double deltax = this.x - p.X;
  145. double deltaz = this.z - p.Y;
  146. return (double)Math.Sqrt(
  147. (deltax * deltax) +
  148. (deltaz * deltaz)
  149. );
  150. }
  151. public double DistanceTo(double x, double y, double z) {
  152. double deltax = this.x - x;
  153. double deltay = this.y - y;
  154. double deltaz = this.z - z;
  155. return (double)Math.Sqrt(
  156. (deltax * deltax) +
  157. (deltay * deltay) +
  158. (deltaz * deltaz)
  159. );
  160. }
  161. public static double operator %(Position a, Position b) => a.DistanceTo(b);
  162. public static double operator %(Position a, double[] b) => a.DistanceTo(b);
  163. public static double operator %(Position a, float[] b) => a.DistanceTo(b);
  164. public static Position operator +(Position a, Position b) => new Position(
  165. a.x+b.x,
  166. a.y+b.y,
  167. a.z+b.z
  168. );
  169. public static Position operator +(Position a, double[] b) => new Position(
  170. a.x - b[0],
  171. a.y - (b.Length == 3 ? b[1] : 0),
  172. a.z - (b.Length == 3 ? b[2] : b[1])
  173. );
  174. public static Position operator +(Position a, double b) => new Position(
  175. a.x + b,
  176. a.y,
  177. a.z + b
  178. );
  179. public static Position operator +(Position a, float[] b) => new Position(
  180. a.x - b[0],
  181. a.y - (b.Length == 3 ? b[1] : 0),
  182. a.z - (b.Length == 3 ? b[2] : b[1])
  183. );
  184. public static Position operator -(Position a, Position b) => new Position(
  185. a.x - b.x,
  186. a.y - b.y,
  187. a.z - b.z
  188. );
  189. public static Position operator -(Position a, double[] b) => new Position(
  190. a.x - b[0],
  191. a.y - (b.Length == 3? b[1] : 0),
  192. a.z - (b.Length == 3? b[2] : b[1])
  193. );
  194. public static Position operator -(Position a, double b) => new Position(
  195. a.x - b,
  196. a.y,
  197. a.z - b
  198. );
  199. public static Position operator -(Position a, float[] b) => new Position(
  200. a.x - b[0],
  201. a.y - (b.Length == 3 ? b[1] : 0),
  202. a.z - (b.Length == 3 ? b[2] : b[1])
  203. );
  204. //public static float operator -(float[] a, Position b) => b.DistanceTo(a);
  205. public override string ToString() {
  206. return "["+this.x.ToString("0.000") + " x " + this.y.ToString("0.000") + " x " + this.z.ToString("0.000") +"]";
  207. }
  208. }
  209. }