Vector3D.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. using System;
  2. namespace BBIWARG.Utility
  3. {
  4. /// <summary>
  5. /// Class with represents a vector or a point in 3 dimensional space.
  6. /// </summary>
  7. public class Vector3D
  8. {
  9. /// <summary>
  10. /// X component as integer
  11. /// </summary>
  12. public int IntX { get { return (int)X; } }
  13. /// <summary>
  14. /// Y component as Integer
  15. /// </summary>
  16. public int IntY { get { return (int)Y; } }
  17. /// <summary>
  18. /// Z component as Integer
  19. /// </summary>
  20. public int IntZ { get { return (int)Z; } }
  21. /// <summary>
  22. /// length of the vector, computed with euclidean distance (2.Norm)
  23. /// </summary>
  24. public float Length { get { return (float)Math.Sqrt(X * X + Y * Y + Z * Z); } }
  25. /// <summary>
  26. /// X (first) component
  27. /// </summary>
  28. public float X { get; private set; }
  29. /// <summary>
  30. /// Y (second) component
  31. /// </summary>
  32. public float Y { get; private set; }
  33. /// <summary>
  34. /// Z (third) component
  35. /// </summary>
  36. public float Z { get; private set; }
  37. /// <summary>
  38. /// Standard constructor which sets the 3 components.
  39. /// </summary>
  40. /// <param name="x">first component</param>
  41. /// <param name="y">second component</param>
  42. /// <param name="z">third component</param>
  43. public Vector3D(float x, float y, float z)
  44. {
  45. X = x;
  46. Y = y;
  47. Z = z;
  48. }
  49. /// <summary>
  50. /// Subtracts two vectors (component-by-component).
  51. /// </summary>
  52. /// <param name="v1">the minuend</param>
  53. /// <param name="v2">the subtrahend</param>
  54. /// <returns>the difference of the two vectors</returns>
  55. public static Vector3D operator -(Vector3D v1, Vector3D v2)
  56. {
  57. return new Vector3D(v1.X - v2.X, v1.Y - v2.Y, v1.Z - v2.Z);
  58. }
  59. /// <summary>
  60. /// Multiplies this vector with a scalar value.
  61. /// </summary>
  62. /// <param name="scalar">the value</param>
  63. /// <param name="v">this vector</param>
  64. /// <returns>the multiplied vector</returns>
  65. public static Vector3D operator *(float scalar, Vector3D v)
  66. {
  67. return new Vector3D(scalar * v.X, scalar * v.Y, scalar * v.Z);
  68. }
  69. /// <summary>
  70. /// Multiplies this vector with a scalar value.
  71. /// </summary>
  72. /// <param name="v">this vector</param>
  73. /// <param name="scalar">the value</param>
  74. /// <returns>the multiplied vector</returns>
  75. public static Vector3D operator *(Vector3D v, float scalar)
  76. {
  77. return new Vector3D(scalar * v.X, scalar * v.Y, scalar * v.Z);
  78. }
  79. /// <summary>
  80. /// Divides this vector with a scalar value.
  81. /// </summary>
  82. /// <param name="v">this vector</param>
  83. /// <param name="scalar">the value</param>
  84. /// <returns>the divided vector</returns>
  85. public static Vector3D operator /(Vector3D v, float scalar)
  86. {
  87. return new Vector3D(v.X / scalar, v.Y / scalar, v.Z / scalar);
  88. }
  89. /// <summary>
  90. /// Divides on vector with another vector component-by-component.
  91. /// </summary>
  92. /// <param name="v1">the dividend</param>
  93. /// <param name="v2">the divisor</param>
  94. /// <returns>the component divided vector</returns>
  95. public static Vector3D operator /(Vector3D v1, Vector3D v2)
  96. {
  97. return new Vector3D(v1.X / v2.X, v1.Y / v2.Y, v1.Z / v2.Z);
  98. }
  99. /// <summary>
  100. /// Adds two vectors (component-by-component).
  101. /// </summary>
  102. /// <param name="v1">first addend</param>
  103. /// <param name="v2">second addend</param>
  104. /// <returns>sum of the vectors</returns>
  105. public static Vector3D operator +(Vector3D v1, Vector3D v2)
  106. {
  107. return new Vector3D(v1.X + v2.X, v1.Y + v2.Y, v1.Z + v2.Z);
  108. }
  109. /// <summary>
  110. /// Computes the distance from the point this vector describes to another 3D point (vector).
  111. /// The distance is the euclidean distance (2. Norm).
  112. /// </summary>
  113. /// <param name="point">the other point</param>
  114. /// <returns>euclidean distance between this and point</returns>
  115. public float getDistanceTo(Vector3D point)
  116. {
  117. return (this - point).Length;
  118. }
  119. }
  120. }