dfb4abfb50426abd44dda2f2e631bf1305d2833a425d6fec18efad8bf7be02d36eeac13f8b9ef7af101c9ae5500f3c62a6170ee779e49abe84a228889c751c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. import { MatrixArray } from './matrix';
  2. export interface PointLike {
  3. x: number
  4. y: number
  5. }
  6. export default class Point {
  7. x: number
  8. y: number
  9. constructor(x?: number, y?: number) {
  10. this.x = x || 0;
  11. this.y = y || 0;
  12. }
  13. /**
  14. * Copy from another point
  15. */
  16. copy(other: PointLike) {
  17. this.x = other.x;
  18. this.y = other.y;
  19. return this;
  20. }
  21. /**
  22. * Clone a point
  23. */
  24. clone() {
  25. return new Point(this.x, this.y);
  26. }
  27. /**
  28. * Set x and y
  29. */
  30. set(x: number, y: number) {
  31. this.x = x;
  32. this.y = y;
  33. return this;
  34. }
  35. /**
  36. * If equal to another point
  37. */
  38. equal(other: PointLike) {
  39. return other.x === this.x && other.y === this.y;
  40. }
  41. /**
  42. * Add another point
  43. */
  44. add(other: PointLike) {
  45. this.x += other.x;
  46. this.y += other.y;
  47. return this;
  48. }
  49. scale(scalar: number) {
  50. this.x *= scalar;
  51. this.y *= scalar;
  52. }
  53. scaleAndAdd(other: PointLike, scalar: number) {
  54. this.x += other.x * scalar;
  55. this.y += other.y * scalar;
  56. }
  57. /**
  58. * Sub another point
  59. */
  60. sub(other: PointLike) {
  61. this.x -= other.x;
  62. this.y -= other.y;
  63. return this;
  64. }
  65. /**
  66. * Dot product with other point
  67. */
  68. dot(other: PointLike) {
  69. return this.x * other.x + this.y * other.y;
  70. }
  71. /**
  72. * Get length of point
  73. */
  74. len() {
  75. return Math.sqrt(this.x * this.x + this.y * this.y);
  76. }
  77. /**
  78. * Get squared length
  79. */
  80. lenSquare() {
  81. return this.x * this.x + this.y * this.y;
  82. }
  83. /**
  84. * Normalize
  85. */
  86. normalize() {
  87. const len = this.len();
  88. this.x /= len;
  89. this.y /= len;
  90. return this;
  91. }
  92. /**
  93. * Distance to another point
  94. */
  95. distance(other: PointLike) {
  96. const dx = this.x - other.x;
  97. const dy = this.y - other.y;
  98. return Math.sqrt(dx * dx + dy * dy);
  99. }
  100. /**
  101. * Square distance to another point
  102. */
  103. distanceSquare(other: Point) {
  104. const dx = this.x - other.x;
  105. const dy = this.y - other.y;
  106. return dx * dx + dy * dy;
  107. }
  108. /**
  109. * Negate
  110. */
  111. negate() {
  112. this.x = -this.x;
  113. this.y = -this.y;
  114. return this;
  115. }
  116. /**
  117. * Apply a transform matrix array.
  118. */
  119. transform(m: MatrixArray) {
  120. if (!m) {
  121. return;
  122. }
  123. const x = this.x;
  124. const y = this.y;
  125. this.x = m[0] * x + m[2] * y + m[4];
  126. this.y = m[1] * x + m[3] * y + m[5];
  127. return this;
  128. }
  129. toArray(out: number[]) {
  130. out[0] = this.x;
  131. out[1] = this.y;
  132. return out;
  133. }
  134. fromArray(input: number[]) {
  135. this.x = input[0];
  136. this.y = input[1];
  137. }
  138. static set(p: PointLike, x: number, y: number) {
  139. p.x = x;
  140. p.y = y;
  141. }
  142. static copy(p: PointLike, p2: PointLike) {
  143. p.x = p2.x;
  144. p.y = p2.y;
  145. }
  146. static len(p: PointLike) {
  147. return Math.sqrt(p.x * p.x + p.y * p.y);
  148. }
  149. static lenSquare(p: PointLike) {
  150. return p.x * p.x + p.y * p.y;
  151. }
  152. static dot(p0: PointLike, p1: PointLike) {
  153. return p0.x * p1.x + p0.y * p1.y;
  154. }
  155. static add(out: PointLike, p0: PointLike, p1: PointLike) {
  156. out.x = p0.x + p1.x;
  157. out.y = p0.y + p1.y;
  158. }
  159. static sub(out: PointLike, p0: PointLike, p1: PointLike) {
  160. out.x = p0.x - p1.x;
  161. out.y = p0.y - p1.y;
  162. }
  163. static scale(out: PointLike, p0: PointLike, scalar: number) {
  164. out.x = p0.x * scalar;
  165. out.y = p0.y * scalar;
  166. }
  167. static scaleAndAdd(out: PointLike, p0: PointLike, p1: PointLike, scalar: number) {
  168. out.x = p0.x + p1.x * scalar;
  169. out.y = p0.y + p1.y * scalar;
  170. }
  171. static lerp(out: PointLike, p0: PointLike, p1: PointLike, t: number) {
  172. const onet = 1 - t;
  173. out.x = onet * p0.x + t * p1.x;
  174. out.y = onet * p0.y + t * p1.y;
  175. }
  176. }