Vector2Int.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. namespace Island.StandardLib.Math
  6. {
  7. /// <summary>
  8. /// 表示一个二维向量,精度为 <see cref="int"/>
  9. /// </summary>
  10. public struct Vector2Int
  11. {
  12. public static Vector2Int Zero = new Vector2Int(0, 0);
  13. /// <summary>
  14. /// X分量
  15. /// </summary>
  16. public int X;
  17. /// <summary>
  18. /// Y分量
  19. /// </summary>
  20. public int Y;
  21. /// <summary>
  22. /// 初始化
  23. /// </summary>
  24. /// <param name="x">X分量</param>
  25. /// <param name="y">Y分量</param>
  26. public Vector2Int(int x, int y)
  27. {
  28. X = x;
  29. Y = y;
  30. }
  31. /// <summary>
  32. /// 判断对象是否相等
  33. /// </summary>
  34. /// <param name="obj">对象</param>
  35. /// <returns></returns>
  36. public override bool Equals(object obj)
  37. {
  38. if (obj == null) return false;
  39. if (!(obj is Vector2Int)) return false;
  40. Vector2Int dest = (Vector2Int)obj;
  41. return dest.X == X && dest.Y == Y;
  42. }
  43. public override int GetHashCode()
  44. {
  45. return X.GetHashCode() ^ Y.GetHashCode();
  46. }
  47. /// <summary>
  48. /// 使用通信数据初始化
  49. /// </summary>
  50. /// <param name="xyz">X:Y:Z</param>
  51. public Vector2Int(string xy)
  52. {
  53. string[] l = xy.Split(':');
  54. if (l.Length == 2)
  55. {
  56. if (int.TryParse(l[0], out int x) &&
  57. int.TryParse(l[1], out int y))
  58. {
  59. X = x;
  60. Y = y;
  61. }
  62. else
  63. {
  64. X = Y = 0;
  65. }
  66. }
  67. else X = Y = 0;
  68. }
  69. /// <summary>
  70. /// 向量加法
  71. /// </summary>
  72. /// <param name="vec"></param>
  73. /// <returns></returns>
  74. public Vector2Int ADD(Vector2Int vec)
  75. {
  76. return new Vector2Int(X + vec.X, Y + vec.Y);
  77. }
  78. /// <summary>
  79. /// 向量减法
  80. /// </summary>
  81. /// <param name="vec"></param>
  82. /// <returns></returns>
  83. public Vector2Int RED(Vector2Int vec)
  84. {
  85. return new Vector2Int(X - vec.X, Y - vec.Y);
  86. }
  87. /// <summary>
  88. /// 向量数乘
  89. /// </summary>
  90. /// <param name="vecdx"></param>
  91. /// <returns></returns>
  92. public Vector2Int MUL(int vecdx)
  93. {
  94. return new Vector2Int(vecdx * X, Y * vecdx);
  95. }
  96. /// <summary>
  97. /// 如果向量表示点的坐标,计算这个点和指定参数点之间的距离
  98. /// </summary>
  99. /// <param name="vec">指定点</param>
  100. /// <returns></returns>
  101. public int DistanceOf(Vector2Int vec)
  102. {
  103. int x1 = (int)System.Math.Pow(X - vec.X, 2);
  104. int x2 = (int)System.Math.Pow(Y - vec.Y, 2);
  105. return (int)System.Math.Pow(x1 + x2, 0.5d);
  106. }
  107. /// <summary>
  108. /// 获取通信数据表示
  109. /// </summary>
  110. /// <returns></returns>
  111. public string ToXY()
  112. {
  113. return X + ":" + Y;
  114. }
  115. /// <summary>
  116. /// 获取数对表示
  117. /// </summary>
  118. /// <returns></returns>
  119. public override string ToString()
  120. {
  121. return "(" + X + ", " + Y + ")";
  122. }
  123. public static bool operator ==(Vector2Int a, Vector2Int b)
  124. {
  125. return a.Equals(b);
  126. }
  127. public static bool operator !=(Vector2Int a, Vector2Int b)
  128. {
  129. return !a.Equals(b);
  130. }
  131. }
  132. }