math.size.ts 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /**
  2. * Interface for the size containing width and height
  3. */
  4. export interface ISize {
  5. /**
  6. * Width
  7. */
  8. width: number;
  9. /**
  10. * Heighht
  11. */
  12. height: number;
  13. }
  14. /**
  15. * Size containing widht and height
  16. */
  17. export class Size implements ISize {
  18. /**
  19. * Width
  20. */
  21. public width: number;
  22. /**
  23. * Height
  24. */
  25. public height: number;
  26. /**
  27. * Creates a Size object from the given width and height (floats).
  28. * @param width width of the new size
  29. * @param height height of the new size
  30. */
  31. public constructor(width: number, height: number) {
  32. this.width = width;
  33. this.height = height;
  34. }
  35. /**
  36. * Returns a string with the Size width and height
  37. * @returns a string with the Size width and height
  38. */
  39. public toString(): string {
  40. return `{W: ${this.width}, H: ${this.height}}`;
  41. }
  42. /**
  43. * "Size"
  44. * @returns the string "Size"
  45. */
  46. public getClassName(): string {
  47. return "Size";
  48. }
  49. /**
  50. * Returns the Size hash code.
  51. * @returns a hash code for a unique width and height
  52. */
  53. public getHashCode(): number {
  54. let hash = this.width | 0;
  55. hash = (hash * 397) ^ (this.height | 0);
  56. return hash;
  57. }
  58. /**
  59. * Updates the current size from the given one.
  60. * @param src the given size
  61. */
  62. public copyFrom(src: Size) {
  63. this.width = src.width;
  64. this.height = src.height;
  65. }
  66. /**
  67. * Updates in place the current Size from the given floats.
  68. * @param width width of the new size
  69. * @param height height of the new size
  70. * @returns the updated Size.
  71. */
  72. public copyFromFloats(width: number, height: number): Size {
  73. this.width = width;
  74. this.height = height;
  75. return this;
  76. }
  77. /**
  78. * Updates in place the current Size from the given floats.
  79. * @param width width to set
  80. * @param height height to set
  81. * @returns the updated Size.
  82. */
  83. public set(width: number, height: number): Size {
  84. return this.copyFromFloats(width, height);
  85. }
  86. /**
  87. * Multiplies the width and height by numbers
  88. * @param w factor to multiple the width by
  89. * @param h factor to multiple the height by
  90. * @returns a new Size set with the multiplication result of the current Size and the given floats.
  91. */
  92. public multiplyByFloats(w: number, h: number): Size {
  93. return new Size(this.width * w, this.height * h);
  94. }
  95. /**
  96. * Clones the size
  97. * @returns a new Size copied from the given one.
  98. */
  99. public clone(): Size {
  100. return new Size(this.width, this.height);
  101. }
  102. /**
  103. * True if the current Size and the given one width and height are strictly equal.
  104. * @param other the other size to compare against
  105. * @returns True if the current Size and the given one width and height are strictly equal.
  106. */
  107. public equals(other: Size): boolean {
  108. if (!other) {
  109. return false;
  110. }
  111. return (this.width === other.width) && (this.height === other.height);
  112. }
  113. /**
  114. * The surface of the Size : width * height (float).
  115. */
  116. public get surface(): number {
  117. return this.width * this.height;
  118. }
  119. /**
  120. * Create a new size of zero
  121. * @returns a new Size set to (0.0, 0.0)
  122. */
  123. public static Zero(): Size {
  124. return new Size(0.0, 0.0);
  125. }
  126. /**
  127. * Sums the width and height of two sizes
  128. * @param otherSize size to add to this size
  129. * @returns a new Size set as the addition result of the current Size and the given one.
  130. */
  131. public add(otherSize: Size): Size {
  132. let r = new Size(this.width + otherSize.width, this.height + otherSize.height);
  133. return r;
  134. }
  135. /**
  136. * Subtracts the width and height of two
  137. * @param otherSize size to subtract to this size
  138. * @returns a new Size set as the subtraction result of the given one from the current Size.
  139. */
  140. public subtract(otherSize: Size): Size {
  141. let r = new Size(this.width - otherSize.width, this.height - otherSize.height);
  142. return r;
  143. }
  144. /**
  145. * Creates a new Size set at the linear interpolation "amount" between "start" and "end"
  146. * @param start starting size to lerp between
  147. * @param end end size to lerp between
  148. * @param amount amount to lerp between the start and end values
  149. * @returns a new Size set at the linear interpolation "amount" between "start" and "end"
  150. */
  151. public static Lerp(start: Size, end: Size, amount: number): Size {
  152. var w = start.width + ((end.width - start.width) * amount);
  153. var h = start.height + ((end.height - start.height) * amount);
  154. return new Size(w, h);
  155. }
  156. }