Vector3D.cs 4.5 KB

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