Source: model/Point.js

  1. /**
  2. * @file Class Point
  3. * @version March 12, 2017
  4. *
  5. * @author Olivier Pirson --- http://www.opimedia.be/
  6. * @license GPLv3 --- Copyright (C) 2017 Olivier Pirson
  7. */
  8. /**
  9. * A point,
  10. * represented by its cartesian coordinates.
  11. */
  12. class Point {
  13. /**
  14. * Constructs a point of cartesian coordinates (x=a, y=b).
  15. *
  16. * @param {number} a
  17. * @param {number} b
  18. */
  19. constructor(a, b) {
  20. assert(typeof a === "number", a);
  21. assert(typeof b === "number", b);
  22. this._x = a;
  23. this._y = b;
  24. }
  25. /**
  26. * Returns the horizontal coordinate of the cartesian coordinates.
  27. *
  28. * @returns {number}
  29. */
  30. get x() {
  31. if (this._x === null) {
  32. this._x = this.r*Math.cos(this.phi);
  33. }
  34. return this._x;
  35. }
  36. /**
  37. * Returns the vertical coordinate of the cartesian coordinates.
  38. *
  39. * @returns {number}
  40. */
  41. get y() {
  42. if (this._y === null) {
  43. this._y = this.r*Math.sin(this.phi);
  44. }
  45. return this._y;
  46. }
  47. /**
  48. * Returns a new point that is the sum.
  49. *
  50. * @param {Point} other
  51. *
  52. * @returns {Point}
  53. */
  54. add(other) {
  55. assert(other instanceof Point, other);
  56. return new Point(this.x + other.x, this.y + other.y);
  57. }
  58. /**
  59. * Compare two points before with x coordinate
  60. * and maybe after with y coordinate.
  61. *
  62. * @param {Point} other
  63. *
  64. * @returns {number} -1, 0 or 1
  65. */
  66. compare(other) {
  67. assert(other instanceof Point, other);
  68. const compX = compare(this.x, other.x);
  69. return (compX !== 0
  70. ? compX
  71. : compare(this.y, other.y));
  72. }
  73. /**
  74. * Returns the distance between the two points.
  75. *
  76. * @param {Point} other
  77. *
  78. * @returns {number} >= 0
  79. */
  80. distance(other) {
  81. assert(other instanceof Point, other);
  82. return Math.sqrt(this.distanceSqr(other));
  83. }
  84. /**
  85. * Returns the square of the distance between the two points.
  86. *
  87. * @param {Point} other
  88. *
  89. * @returns {number} >= 0
  90. */
  91. distanceSqr(other) {
  92. assert(other instanceof Point, other);
  93. const diffX = this._x - other._x;
  94. const diffY = this._y - other._y;
  95. return diffX*diffX + diffY*diffY;
  96. }
  97. /**
  98. * Returns the distance to (0, 0).
  99. *
  100. * @returns {number} >= 0
  101. */
  102. distanceTo0() {
  103. return Math.sqrt(this.distanceTo0Sqr());
  104. }
  105. /**
  106. * Returns the square of the distance to (0, 0).
  107. *
  108. * @returns {number} >= 0
  109. */
  110. distanceTo0Sqr() {
  111. return this._x*this._x + this._y*this._y;
  112. }
  113. /**
  114. * Returns true iff the point is equals to the point other,
  115. * namely iff they are same cartesian coordinates.
  116. *
  117. * @param {Point} other
  118. *
  119. * @returns {boolean}
  120. */
  121. isEquals(other) {
  122. assert(other instanceof Point, other);
  123. return (this.x === other.x) && (this.y === other.y);
  124. }
  125. /**
  126. * Returns a new point multiply by the scalar n.
  127. *
  128. * @returns {Point}
  129. */
  130. mulScalar(n) {
  131. assert(typeof n === "number", n);
  132. return new Point(this.x*n, this.y*n);
  133. }
  134. /**
  135. * Returns a new point that is the difference.
  136. *
  137. * @param {Point} other
  138. *
  139. * @returns {Point}
  140. */
  141. sub(other) {
  142. assert(other instanceof Point, other);
  143. return new Point(this.x - other.x, this.y - other.y);
  144. }
  145. /**
  146. * Returns a string cartesian representation of the point.
  147. *
  148. * Each number is represented with at most precision figures after the decimal point.
  149. *
  150. * @returns {String}
  151. */
  152. toString(precision=2) {
  153. assert(Number.isInteger(precision), precision);
  154. assert(precision >= 0, precision);
  155. function format(n) {
  156. return (Number.isInteger(n)
  157. ? n.toString()
  158. : n.toFixed(precision));
  159. }
  160. return "(" + format(this.x) + ", " + format(this.y) + ")";
  161. }
  162. }