Vector2L.cs 3.5 KB

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