Vector4.cs 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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 Vector4
  8. {
  9. public static Vector4 Zero = new Vector4(0, 0, 0, 0);
  10. public float X { get; set; }
  11. public float Y { get; set; }
  12. public float Z { get; set; }
  13. public float W { get; set; }
  14. public Vector4(float x, float y, float z, float w)
  15. {
  16. X = x;
  17. Y = y;
  18. Z = z;
  19. W = w;
  20. }
  21. /// <summary>
  22. /// 使用通信数据初始化
  23. /// </summary>
  24. /// <param name="xyz">X:Y:Z</param>
  25. public Vector4(string xyzw)
  26. {
  27. string[] l = xyzw.Split(':');
  28. if (l.Length == 3)
  29. {
  30. if (float.TryParse(l[0], out float x) &&
  31. float.TryParse(l[1], out float y) &&
  32. float.TryParse(l[2], out float z) &&
  33. float.TryParse(l[2], out float w))
  34. {
  35. X = x;
  36. Y = y;
  37. Z = z;
  38. W = w;
  39. }
  40. else
  41. {
  42. X = Y = Z = W = 0;
  43. }
  44. }
  45. else X = Y = Z = W = 0;
  46. }
  47. public string ToXYZW()
  48. {
  49. return X + ":" + Y + ":" + Z + ":" + W;
  50. }
  51. public override string ToString()
  52. {
  53. return "(" + X + ", " + Y + ", " + Z + ", " + W + ")";
  54. }
  55. public override bool Equals(object obj)
  56. {
  57. if (!(obj is Vector4)) return false;
  58. Vector4 d = (Vector4)obj;
  59. return X == d.X && Y == d.Y && Z == d.Z && W == d.W;
  60. }
  61. public override int GetHashCode()
  62. {
  63. return X.GetHashCode() ^ Y.GetHashCode() ^ Z.GetHashCode() ^ W.GetHashCode();
  64. }
  65. public static bool operator ==(Vector4 a, Vector4 b)
  66. {
  67. return a.Equals(b);
  68. }
  69. public static bool operator !=(Vector4 a, Vector4 b)
  70. {
  71. return !a.Equals(b);
  72. }
  73. }
  74. }