math.isovector.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. import { Logger } from "../Misc/logger.js";
  2. import { Vector3 } from "./math.vector.js";
  3. /**
  4. * Class representing an isovector a vector containing 2 INTEGER coordinates
  5. * x axis is horizontal
  6. * y axis is 60 deg counter clockwise from positive y axis
  7. * @internal
  8. */
  9. // eslint-disable-next-line @typescript-eslint/naming-convention
  10. export class _IsoVector {
  11. /**
  12. * Creates a new isovector from the given x and y coordinates
  13. * @param x defines the first coordinate, must be an integer
  14. * @param y defines the second coordinate, must be an integer
  15. */
  16. constructor(
  17. /** defines the first coordinate */
  18. x = 0,
  19. /** defines the second coordinate */
  20. y = 0) {
  21. this.x = x;
  22. this.y = y;
  23. if (x !== Math.floor(x)) {
  24. x === Math.floor(x);
  25. Logger.Warn("x is not an integer, floor(x) used");
  26. }
  27. if (y !== Math.floor(y)) {
  28. y === Math.floor(y);
  29. Logger.Warn("y is not an integer, floor(y) used");
  30. }
  31. }
  32. // Operators
  33. /**
  34. * Gets a new IsoVector copied from the IsoVector
  35. * @returns a new IsoVector
  36. */
  37. clone() {
  38. return new _IsoVector(this.x, this.y);
  39. }
  40. /**
  41. * Rotates one IsoVector 60 degrees counter clockwise about another
  42. * Please note that this is an in place operation
  43. * @param other an IsoVector a center of rotation
  44. * @returns the rotated IsoVector
  45. */
  46. rotate60About(other) {
  47. //other IsoVector
  48. const x = this.x;
  49. this.x = other.x + other.y - this.y;
  50. this.y = x + this.y - other.x;
  51. return this;
  52. }
  53. /**
  54. * Rotates one IsoVector 60 degrees clockwise about another
  55. * Please note that this is an in place operation
  56. * @param other an IsoVector as center of rotation
  57. * @returns the rotated IsoVector
  58. */
  59. rotateNeg60About(other) {
  60. const x = this.x;
  61. this.x = x + this.y - other.y;
  62. this.y = other.x + other.y - x;
  63. return this;
  64. }
  65. /**
  66. * For an equilateral triangle OAB with O at isovector (0, 0) and A at isovector (m, n)
  67. * Rotates one IsoVector 120 degrees counter clockwise about the center of the triangle
  68. * Please note that this is an in place operation
  69. * @param m integer a measure a Primary triangle of order (m, n) m > n
  70. * @param n >= 0 integer a measure for a Primary triangle of order (m, n)
  71. * @returns the rotated IsoVector
  72. */
  73. rotate120(m, n) {
  74. //m, n integers
  75. if (m !== Math.floor(m)) {
  76. m === Math.floor(m);
  77. Logger.Warn("m not an integer only floor(m) used");
  78. }
  79. if (n !== Math.floor(n)) {
  80. n === Math.floor(n);
  81. Logger.Warn("n not an integer only floor(n) used");
  82. }
  83. const x = this.x;
  84. this.x = m - x - this.y;
  85. this.y = n + x;
  86. return this;
  87. }
  88. /**
  89. * For an equilateral triangle OAB with O at isovector (0, 0) and A at isovector (m, n)
  90. * Rotates one IsoVector 120 degrees clockwise about the center of the triangle
  91. * Please note that this is an in place operation
  92. * @param m integer a measure a Primary triangle of order (m, n) m > n
  93. * @param n >= 0 integer a measure for a Primary triangle of order (m, n)
  94. * @returns the rotated IsoVector
  95. */
  96. rotateNeg120(m, n) {
  97. //m, n integers
  98. if (m !== Math.floor(m)) {
  99. m === Math.floor(m);
  100. Logger.Warn("m is not an integer, floor(m) used");
  101. }
  102. if (n !== Math.floor(n)) {
  103. n === Math.floor(n);
  104. Logger.Warn("n is not an integer, floor(n) used");
  105. }
  106. const x = this.x;
  107. this.x = this.y - n;
  108. this.y = m + n - x - this.y;
  109. return this;
  110. }
  111. /**
  112. * Transforms an IsoVector to one in Cartesian 3D space based on an isovector
  113. * @param origin an IsoVector
  114. * @param isoGridSize
  115. * @returns Point as a Vector3
  116. */
  117. toCartesianOrigin(origin, isoGridSize) {
  118. const point = Vector3.Zero();
  119. point.x = origin.x + 2 * this.x * isoGridSize + this.y * isoGridSize;
  120. point.y = origin.y + Math.sqrt(3) * this.y * isoGridSize;
  121. return point;
  122. }
  123. // Statics
  124. /**
  125. * Gets a new IsoVector(0, 0)
  126. * @returns a new IsoVector
  127. */
  128. static Zero() {
  129. return new _IsoVector(0, 0);
  130. }
  131. }
  132. //# sourceMappingURL=math.isovector.js.map