point.d.ts 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. import { Rectangle } from './rectangle';
  2. import { Geometry } from './geometry';
  3. export declare class Point extends Geometry implements Point.PointLike {
  4. x: number;
  5. y: number;
  6. constructor();
  7. constructor(x?: number, y?: number);
  8. /**
  9. * Rounds the point to the given precision.
  10. */
  11. round(precision?: number): this;
  12. add(x: number, y: number): this;
  13. add(p: Point.PointLike | Point.PointData): this;
  14. update(x: number, y: number): this;
  15. update(p: Point.PointLike | Point.PointData): this;
  16. translate(dx: number, dy: number): this;
  17. translate(p: Point.PointLike | Point.PointData): this;
  18. /**
  19. * Rotate the point by `degree` around `center`.
  20. */
  21. rotate(degree: number, center?: Point.PointLike | Point.PointData): this;
  22. /**
  23. * Scale point by `sx` and `sy` around the given `origin`. If origin is
  24. * not specified, the point is scaled around `0, 0`.
  25. */
  26. scale(sx: number, sy: number, origin?: Point.PointLike | Point.PointData): this;
  27. /**
  28. * Chooses the point closest to this point from among `points`. If `points`
  29. * is an empty array, `null` is returned.
  30. */
  31. closest(points: (Point.PointLike | Point.PointData)[]): Point | null;
  32. /**
  33. * Returns the distance between the point and another point `p`.
  34. */
  35. distance(p: Point.PointLike | Point.PointData): number;
  36. /**
  37. * Returns the squared distance between the point and another point `p`.
  38. *
  39. * Useful for distance comparisons in which real distance is not necessary
  40. * (saves one `Math.sqrt()` operation).
  41. */
  42. squaredDistance(p: Point.PointLike | Point.PointData): number;
  43. manhattanDistance(p: Point.PointLike | Point.PointData): number;
  44. /**
  45. * Returns the magnitude of the point vector.
  46. *
  47. * @see http://en.wikipedia.org/wiki/Magnitude_(mathematics)
  48. */
  49. magnitude(): number;
  50. /**
  51. * Returns the angle(in degrees) between vector from this point to `p` and
  52. * the x-axis.
  53. */
  54. theta(p?: Point.PointLike | Point.PointData): number;
  55. /**
  56. * Returns the angle(in degrees) between vector from this point to `p1` and
  57. * the vector from this point to `p2`.
  58. *
  59. * The ordering of points `p1` and `p2` is important.
  60. *
  61. * The function returns a value between `0` and `180` when the angle (in the
  62. * direction from `p1` to `p2`) is clockwise, and a value between `180` and
  63. * `360` when the angle is counterclockwise.
  64. *
  65. * Returns `NaN` if either of the points `p1` and `p2` is equal with this point.
  66. */
  67. angleBetween(p1: Point.PointLike | Point.PointData, p2: Point.PointLike | Point.PointData): number;
  68. /**
  69. * Returns the angle(in degrees) between the line from `(0,0)` and this point
  70. * and the line from `(0,0)` to `p`.
  71. *
  72. * The function returns a value between `0` and `180` when the angle (in the
  73. * direction from this point to `p`) is clockwise, and a value between `180`
  74. * and `360` when the angle is counterclockwise. Returns `NaN` if called from
  75. * point `(0,0)` or if `p` is `(0,0)`.
  76. */
  77. vectorAngle(p: Point.PointLike | Point.PointData): number;
  78. /**
  79. * Converts rectangular to polar coordinates.
  80. */
  81. toPolar(origin?: Point.PointLike | Point.PointData): this;
  82. /**
  83. * Returns the change in angle(in degrees) that is the result of moving the
  84. * point from its previous position to its current position.
  85. *
  86. * More specifically, this function computes the angle between the line from
  87. * the ref point to the previous position of this point(i.e. current position
  88. * `-dx`, `-dy`) and the line from the `ref` point to the current position of
  89. * this point.
  90. *
  91. * The function returns a positive value between `0` and `180` when the angle
  92. * (in the direction from previous position of this point to its current
  93. * position) is clockwise, and a negative value between `0` and `-180` when
  94. * the angle is counterclockwise.
  95. *
  96. * The function returns `0` if the previous and current positions of this
  97. * point are the same (i.e. both `dx` and `dy` are `0`).
  98. */
  99. changeInAngle(dx: number, dy: number, ref?: Point.PointLike | Point.PointData): number;
  100. /**
  101. * If the point lies outside the rectangle `rect`, adjust the point so that
  102. * it becomes the nearest point on the boundary of `rect`.
  103. */
  104. adhereToRect(rect: Rectangle.RectangleLike): this;
  105. /**
  106. * Returns the bearing(cardinal direction) between me and the given point.
  107. *
  108. * @see https://en.wikipedia.org/wiki/Cardinal_direction
  109. */
  110. bearing(p: Point.PointLike | Point.PointData): Point.Bearing;
  111. /**
  112. * Returns the cross product of the vector from me to `p1` and the vector
  113. * from me to `p2`.
  114. *
  115. * The left-hand rule is used because the coordinate system is left-handed.
  116. */
  117. cross(p1: Point.PointLike | Point.PointData, p2: Point.PointLike | Point.PointData): number;
  118. /**
  119. * Returns the dot product of this point with given other point.
  120. */
  121. dot(p: Point.PointLike | Point.PointData): number;
  122. /**
  123. * Returns a point that has coordinates computed as a difference between the
  124. * point and another point with coordinates `dx` and `dy`.
  125. *
  126. * If only `dx` is specified and is a number, `dy` is considered to be zero.
  127. * If only `dx` is specified and is an object, it is considered to be another
  128. * point or an object in the form `{ x: [number], y: [number] }`
  129. */
  130. diff(dx: number, dy: number): Point;
  131. diff(p: Point.PointLike | Point.PointData): Point;
  132. /**
  133. * Returns an interpolation between me and point `p` for a parametert in
  134. * the closed interval `[0, 1]`.
  135. */
  136. lerp(p: Point.PointLike | Point.PointData, t: number): Point;
  137. /**
  138. * Normalize the point vector, scale the line segment between `(0, 0)`
  139. * and the point in order for it to have the given length. If length is
  140. * not specified, it is considered to be `1`; in that case, a unit vector
  141. * is computed.
  142. */
  143. normalize(length?: number): this;
  144. /**
  145. * Moves this point along the line starting from `ref` to this point by a
  146. * certain `distance`.
  147. */
  148. move(ref: Point.PointLike | Point.PointData, distance: number): this;
  149. /**
  150. * Returns a point that is the reflection of me with the center of inversion
  151. * in `ref` point.
  152. */
  153. reflection(ref: Point.PointLike | Point.PointData): Point;
  154. /**
  155. * Snaps the point(change its x and y coordinates) to a grid of size `gridSize`
  156. * (or `gridSize` x `gridSizeY` for non-uniform grid).
  157. */
  158. snapToGrid(gridSize: number): this;
  159. snapToGrid(gx: number, gy: number): this;
  160. snapToGrid(gx: number, gy?: number): this;
  161. equals(p: Point.PointLike | Point.PointData): boolean;
  162. clone(): Point;
  163. /**
  164. * Returns the point as a simple JSON object. For example: `{ x: 0, y: 0 }`.
  165. */
  166. toJSON(): {
  167. x: number;
  168. y: number;
  169. };
  170. serialize(): string;
  171. }
  172. export declare namespace Point {
  173. function isPoint(instance: any): instance is Point;
  174. }
  175. export declare namespace Point {
  176. interface PointLike {
  177. x: number;
  178. y: number;
  179. }
  180. type PointData = [number, number];
  181. type Bearing = 'NE' | 'E' | 'SE' | 'S' | 'SW' | 'W' | 'NW' | 'N';
  182. function isPointLike(p: any): p is PointLike;
  183. function isPointData(p: any): p is PointData;
  184. }
  185. export declare namespace Point {
  186. function create(x?: number | Point | PointLike | PointData, y?: number): Point;
  187. function clone(p: Point | PointLike | PointData): Point;
  188. function toJSON(p: Point | PointLike | PointData): {
  189. x: number;
  190. y: number;
  191. };
  192. /**
  193. * Returns a new Point object from the given polar coordinates.
  194. * @see http://en.wikipedia.org/wiki/Polar_coordinate_system
  195. */
  196. function fromPolar(r: number, rad: number, origin?: Point | PointLike | PointData): Point;
  197. /**
  198. * Converts rectangular to polar coordinates.
  199. */
  200. function toPolar(point: Point | PointLike | PointData, origin?: Point | PointLike | PointData): Point;
  201. function equals(p1?: Point.PointLike, p2?: Point.PointLike): boolean;
  202. function equalPoints(p1: Point.PointLike[], p2: Point.PointLike[]): boolean;
  203. /**
  204. * Returns a point with random coordinates that fall within the range
  205. * `[x1, x2]` and `[y1, y2]`.
  206. */
  207. function random(x1: number, x2: number, y1: number, y2: number): Point;
  208. function rotate(point: Point | PointLike | PointData, angle: number, center?: Point | PointLike | PointData): Point;
  209. function rotateEx(point: Point | PointLike | PointData, cos: number, sin: number, center?: Point | PointLike | PointData): Point;
  210. }