Vector3.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. using Island.StandardLib.Storage;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. namespace Island.StandardLib.Math
  7. {
  8. public struct Vector3 : IStorable
  9. {
  10. public float x { get; set; }
  11. public float y { get; set; }
  12. public float z { get; set; }
  13. public static Vector3 Zero = new Vector3(0f, 0f, 0f);
  14. public Vector3(float x, float y, float z)
  15. {
  16. this.x = x;
  17. this.y = y;
  18. this.z = z;
  19. }
  20. public static Vector3 Make(float x, float y, float z)
  21. {
  22. return new Vector3(x, y, z);
  23. }
  24. /// <summary>
  25. /// 使用通信数据初始化
  26. /// </summary>
  27. /// <param name="xyz">X:Y:Z</param>
  28. public Vector3(string xyz)
  29. {
  30. string[] l = xyz.Split(':');
  31. if (l.Length == 3)
  32. {
  33. if (float.TryParse(l[0], out float x) &&
  34. float.TryParse(l[1], out float y) &&
  35. float.TryParse(l[2], out float z))
  36. {
  37. this.x = x;
  38. this.y = y;
  39. this.z = z;
  40. }
  41. else
  42. {
  43. this.x = this.y = this.z = 0;
  44. }
  45. }
  46. else x = y = z = 0;
  47. }
  48. public Vector2 GetVectorXZ()
  49. {
  50. return new Vector2(x, z);
  51. }
  52. /// <summary>
  53. /// 向量加法
  54. /// </summary>
  55. /// <param name="vec"></param>
  56. /// <returns></returns>
  57. public Vector3 ADD(Vector3 vec)
  58. {
  59. return new Vector3(x + vec.x, y + vec.y, z + vec.z);
  60. }
  61. /// <summary>
  62. /// 向量减法
  63. /// </summary>
  64. /// <param name="vec"></param>
  65. /// <returns></returns>
  66. public Vector3 RED(Vector3 vec)
  67. {
  68. return new Vector3(x - vec.x, y - vec.y, z - vec.z);
  69. }
  70. /// <summary>
  71. /// 向量数乘
  72. /// </summary>
  73. /// <param name="vecdx"></param>
  74. /// <returns></returns>
  75. public Vector3 MUL(float vecdx)
  76. {
  77. return new Vector3(vecdx * x, y * vecdx, z * vecdx);
  78. }
  79. /// <summary>
  80. /// 向量点乘 (内积)
  81. /// </summary>
  82. /// <param name="vec"></param>
  83. /// <returns></returns>
  84. public float DOT(Vector3 vec)
  85. {
  86. return x * vec.x + y * vec.y + z * vec.z;
  87. }
  88. /// <summary>
  89. /// 向量叉乘 (外积)
  90. /// </summary>
  91. /// <param name="vec"></param>
  92. /// <returns></returns>
  93. public Vector3 FMUL(Vector3 vec)
  94. {
  95. return new Vector3(
  96. y * vec.z - vec.y * z,
  97. -1 * (x * vec.z - vec.x * z),
  98. x * vec.y - vec.x * y);
  99. }
  100. /// <summary>
  101. /// 如果向量表示点的坐标,计算这个点和指定参数点之间的距离
  102. /// </summary>
  103. /// <param name="vec">指定点</param>
  104. /// <returns></returns>
  105. public float DistanceOf(Vector3 vec)
  106. {
  107. float x1 = (float)System.Math.Pow(x - vec.x, 2);
  108. float x2 = (float)System.Math.Pow(y - vec.y, 2);
  109. float x3 = (float)System.Math.Pow(z - vec.z, 2);
  110. return (float)System.Math.Pow(x1 + x2 + x3, 0.5d);
  111. }
  112. /// <summary>
  113. /// 如果向量表示点的坐标,计算这个点和指定参数点之间的以x, z为平面的距离
  114. /// </summary>
  115. /// <param name="vec">指定点</param>
  116. /// <returns></returns>
  117. public float DistanceOf2D(Vector3 vec)
  118. {
  119. float x1 = (float)System.Math.Pow(x - vec.x, 2);
  120. float x3 = (float)System.Math.Pow(z - vec.z, 2);
  121. return (float)System.Math.Pow(x1 + x3, 0.5d);
  122. }
  123. public Vector3 ToCameraRotation()
  124. {
  125. Vector3 vec = new Vector3();
  126. vec.x = x;
  127. vec.z = z;
  128. vec.y = y + 180f;
  129. while (vec.y > 360f)
  130. vec.y -= 360f;
  131. return vec;
  132. }
  133. public static Vector3 Random(float xyzrange)
  134. {
  135. return Random(xyzrange, xyzrange, xyzrange);
  136. }
  137. public static Vector3 Random(float xrange, float yrange, float zrange)
  138. {
  139. Random rd = new Random();
  140. float fx = (float)(rd.NextDouble() - 0.5) * xrange * 2f,
  141. fy = (float)(rd.NextDouble() - 0.5) * yrange * 2f,
  142. fz = (float)(rd.NextDouble() - 0.5) * zrange * 2f;
  143. return new Vector3(fx, fy, fz);
  144. }
  145. public string ToXYZ()
  146. {
  147. return x + ":" + y + ":" + z;
  148. }
  149. public override string ToString()
  150. {
  151. return "(" + x + ", " + y + ", " + z + ")";
  152. }
  153. public bool IsMathNull
  154. {
  155. get
  156. {
  157. return x < 0 || y < 0 || z < 0;
  158. }
  159. }
  160. public override bool Equals(object obj)
  161. {
  162. if (!(obj is Vector3)) return false;
  163. Vector3 d = (Vector3)obj;
  164. return x == d.x && y == d.y && z == d.z;
  165. }
  166. public override int GetHashCode()
  167. {
  168. return x.GetHashCode() ^ y.GetHashCode() ^ z.GetHashCode();
  169. }
  170. public void WriteToData(DataStorage data)
  171. {
  172. data.Write(x);
  173. data.Write(y);
  174. data.Write(z);
  175. }
  176. public void ReadFromData(DataStorage data)
  177. {
  178. data.Read(out float x);
  179. data.Read(out float y);
  180. data.Read(out float z);
  181. this.x = x;
  182. this.y = y;
  183. this.z = z;
  184. }
  185. public static bool operator ==(Vector3 a, Vector3 b) => a.Equals(b);
  186. public static bool operator !=(Vector3 a, Vector3 b) => !a.Equals(b);
  187. public static Vector3 operator *(Vector3 a, float b) => a.MUL(b);
  188. public static Vector3 operator *(float a, Vector3 b) => b.MUL(a);
  189. public static Vector3 operator +(Vector3 a, Vector3 b) => a.ADD(b);
  190. public static Vector3 operator -(Vector3 a, Vector3 b) => a.RED(b);
  191. }
  192. }