Vector2D.lua

Vector2D.lua (9.1.0.39617; unchanged since 9.0.1.36230)
  1. -- Protecting from addons since we use this in secure code.
  2. local cos = math.cos;
  3. local sin = math.sin;
  4. local atan2 = math.atan2;
  5. local sqrt = math.sqrt;
  6. function Vector2D_ScaleBy(scalar, x, y)
  7. return x * scalar, y * scalar;
  8. end
  9. function Vector2D_DivideBy(divisor, x, y)
  10. return x / divisor, y / divisor;
  11. end
  12. function Vector2D_Add(leftX, leftY, rightX, rightY)
  13. return leftX + rightX, leftY + rightY;
  14. end
  15. function Vector2D_Subtract(leftX, leftY, rightX, rightY)
  16. return leftX - rightX, leftY - rightY;
  17. end
  18. function Vector2D_Cross(leftX, leftY, rightX, rightY)
  19. return leftX * rightY - leftY * rightX;
  20. end
  21. function Vector2D_Dot(leftX, leftY, rightX, rightY)
  22. return leftX * rightX + leftY * rightY;
  23. end
  24. function Vector2D_GetLengthSquared(x, y)
  25. return Vector2D_Dot(x, y, x, y);
  26. end
  27. function Vector2D_GetLength(x, y)
  28. return sqrt(Vector2D_GetLengthSquared(x, y));
  29. end
  30. function Vector2D_Normalize(x, y)
  31. return Vector2D_DivideBy(Vector2D_GetLength(x, y), x, y);
  32. end
  33. function Vector2D_CalculateAngleBetween(leftX, leftY, rightX, rightY)
  34. return atan2(Vector2D_Cross(leftX, leftY, rightX, rightY), Vector2D_Dot(leftX, leftY, rightX, rightY));
  35. end
  36. function Vector2D_RotateDirection(rotationRadians, x, y)
  37. local cosValue = cos(rotationRadians);
  38. local sinValue = sin(rotationRadians);
  39. return x * cosValue - y * sinValue, x * sinValue + y * cosValue;
  40. end
  41. Vector2DMixin = {};
  42. function CreateVector2D(x, y)
  43. local vector = CreateFromMixins(Vector2DMixin);
  44. vector:OnLoad(x, y);
  45. return vector;
  46. end
  47. function AreVector2DEqual(left, right)
  48. if left and right then
  49. return left:IsEqualTo(right);
  50. end
  51. return left == right;
  52. end
  53. function Vector2DMixin:OnLoad(x, y)
  54. self:SetXY(x, y);
  55. end
  56. function Vector2DMixin:IsEqualTo(otherVector)
  57. return self.x == otherVector.x
  58. and self.y == otherVector.y;
  59. end
  60. function Vector2DMixin:GetXY()
  61. return self.x, self.y;
  62. end
  63. function Vector2DMixin:SetXY(x, y)
  64. self.x = x;
  65. self.y = y;
  66. end
  67. function Vector2DMixin:ScaleBy(scalar)
  68. self:SetXY(Vector2D_ScaleBy(scalar, self:GetXY()));
  69. end
  70. function Vector2DMixin:DivideBy(scalar)
  71. self:SetXY(Vector2D_DivideBy(scalar, self:GetXY()));
  72. end
  73. function Vector2DMixin:Add(other)
  74. self:SetXY(Vector2D_Add(self.x, self.y, other:GetXY()));
  75. end
  76. function Vector2DMixin:Subtract(other)
  77. self:SetXY(Vector2D_Subtract(self.x, self.y, other:GetXY()));
  78. end
  79. function Vector2DMixin:Cross(other)
  80. self:SetXY(Vector2D_Cross(self.x, self.y, other:GetXY()));
  81. end
  82. function Vector2DMixin:Dot(other)
  83. return Vector2D_Dot(self.x, self.y, other:GetXY());
  84. end
  85. function Vector2DMixin:IsZero()
  86. return self.x == 0 and self.y == 0;
  87. end
  88. function Vector2DMixin:GetLengthSquared()
  89. return Vector2D_GetLengthSquared(self:GetXY());
  90. end
  91. function Vector2DMixin:GetLength()
  92. return Vector2D_GetLength(self:GetXY());
  93. end
  94. function Vector2DMixin:Normalize()
  95. self:SetXY(Vector2D_Normalize(self:GetXY()));
  96. end
  97. function Vector2DMixin:RotateDirection(rotationRadians)
  98. self:SetXY(Vector2D_RotateDirection(rotationRadians, self:GetXY()));
  99. end
  100. function Vector2DMixin:Clone()
  101. return CreateVector2D(self:GetXY());
  102. end