line.d.ts 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. import { Point } from './point';
  2. import { Geometry } from './geometry';
  3. import { Rectangle } from './rectangle';
  4. import { Ellipse } from './ellipse';
  5. import { Path } from './path';
  6. import { Polyline } from './polyline';
  7. export declare class Line extends Geometry {
  8. start: Point;
  9. end: Point;
  10. get center(): Point;
  11. constructor();
  12. constructor(x1: number, y1: number, x2: number, y2: number);
  13. constructor(p1: Point.PointLike | Point.PointData, p2: Point.PointLike | Point.PointData);
  14. getCenter(): Point;
  15. /**
  16. * Rounds the line to the given `precision`.
  17. */
  18. round(precision?: number): this;
  19. translate(tx: number, ty: number): this;
  20. translate(p: Point.PointLike | Point.PointData): this;
  21. /**
  22. * Rotate the line by `angle` around `origin`.
  23. */
  24. rotate(angle: number, origin?: Point.PointLike | Point.PointData): this;
  25. /**
  26. * Scale the line by `sx` and `sy` about the given `origin`. If origin is not
  27. * specified, the line is scaled around `0,0`.
  28. */
  29. scale(sx: number, sy: number, origin?: Point.PointLike | Point.PointData): this;
  30. /**
  31. * Returns the length of the line.
  32. */
  33. length(): number;
  34. /**
  35. * Useful for distance comparisons in which real length is not necessary
  36. * (saves one `Math.sqrt()` operation).
  37. */
  38. squaredLength(): number;
  39. /**
  40. * Scale the line so that it has the requested length. The start point of
  41. * the line is preserved.
  42. */
  43. setLength(length: number): this;
  44. parallel(distance: number): Line;
  45. /**
  46. * Returns the vector of the line with length equal to length of the line.
  47. */
  48. vector(): Point;
  49. /**
  50. * Returns the angle of incline of the line.
  51. *
  52. * The function returns `NaN` if the start and end endpoints of the line
  53. * both lie at the same coordinates(it is impossible to determine the angle
  54. * of incline of a line that appears to be a point). The
  55. * `line.isDifferentiable()` function may be used in advance to determine
  56. * whether the angle of incline can be computed for a given line.
  57. */
  58. angle(): number;
  59. /**
  60. * Returns a rectangle that is the bounding box of the line.
  61. */
  62. bbox(): Rectangle;
  63. /**
  64. * Returns the bearing (cardinal direction) of the line.
  65. *
  66. * The return value is one of the following strings:
  67. * 'NE', 'E', 'SE', 'S', 'SW', 'W', 'NW' and 'N'.
  68. *
  69. * The function returns 'N' if the two endpoints of the line are coincident.
  70. */
  71. bearing(): Point.Bearing;
  72. /**
  73. * Returns the point on the line that lies closest to point `p`.
  74. */
  75. closestPoint(p: Point.PointLike | Point.PointData): Point;
  76. /**
  77. * Returns the length of the line up to the point that lies closest to point `p`.
  78. */
  79. closestPointLength(p: Point.PointLike | Point.PointData): number;
  80. /**
  81. * Returns a line that is tangent to the line at the point that lies closest
  82. * to point `p`.
  83. */
  84. closestPointTangent(p: Point.PointLike | Point.PointData): Line | null;
  85. /**
  86. * Returns the normalized length (distance from the start of the line / total
  87. * line length) of the line up to the point that lies closest to point.
  88. */
  89. closestPointNormalizedLength(p: Point.PointLike | Point.PointData): number;
  90. /**
  91. * Returns a point on the line that lies `rate` (normalized length) away from
  92. * the beginning of the line.
  93. */
  94. pointAt(ratio: number): Point;
  95. /**
  96. * Returns a point on the line that lies length away from the beginning of
  97. * the line.
  98. */
  99. pointAtLength(length: number): Point;
  100. /**
  101. * Divides the line into two lines at the point that lies `rate` (normalized
  102. * length) away from the beginning of the line.
  103. */
  104. divideAt(ratio: number): Line[];
  105. /**
  106. * Divides the line into two lines at the point that lies length away from
  107. * the beginning of the line.
  108. */
  109. divideAtLength(length: number): Line[];
  110. /**
  111. * Returns `true` if the point `p` lies on the line. Return `false` otherwise.
  112. */
  113. containsPoint(p: Point.PointLike | Point.PointData): boolean;
  114. /**
  115. * Returns an array of the intersection points of the line with another
  116. * geometry shape.
  117. */
  118. intersect(shape: Line | Rectangle | Polyline | Ellipse): Point[] | null;
  119. intersect(shape: Path, options?: Path.Options): Point[] | null;
  120. /**
  121. * Returns the intersection point of the line with another line. Returns
  122. * `null` if no intersection exists.
  123. */
  124. intersectsWithLine(line: Line): Point | null;
  125. /**
  126. * Returns `true` if a tangent line can be found for the line.
  127. *
  128. * Tangents cannot be found if both of the line endpoints are coincident
  129. * (the line appears to be a point).
  130. */
  131. isDifferentiable(): boolean;
  132. /**
  133. * Returns the perpendicular distance between the line and point. The
  134. * distance is positive if the point lies to the right of the line, negative
  135. * if the point lies to the left of the line, and `0` if the point lies on
  136. * the line.
  137. */
  138. pointOffset(p: Point.PointLike | Point.PointData): number;
  139. /**
  140. * Returns the squared distance between the line and the point.
  141. */
  142. pointSquaredDistance(x: number, y: number): number;
  143. pointSquaredDistance(p: Point.PointLike | Point.PointData): number;
  144. /**
  145. * Returns the distance between the line and the point.
  146. */
  147. pointDistance(x: number, y: number): number;
  148. pointDistance(p: Point.PointLike | Point.PointData): number;
  149. /**
  150. * Returns a line tangent to the line at point that lies `rate` (normalized
  151. * length) away from the beginning of the line.
  152. */
  153. tangentAt(ratio: number): Line | null;
  154. /**
  155. * Returns a line tangent to the line at point that lies `length` away from
  156. * the beginning of the line.
  157. */
  158. tangentAtLength(length: number): Line | null;
  159. /**
  160. * Returns which direction the line would have to rotate in order to direct
  161. * itself at a point.
  162. *
  163. * Returns 1 if the given point on the right side of the segment, 0 if its
  164. * on the segment, and -1 if the point is on the left side of the segment.
  165. *
  166. * @see https://softwareengineering.stackexchange.com/questions/165776/what-do-ptlinedist-and-relativeccw-do
  167. */
  168. relativeCcw(x: number, y: number): -1 | 0 | 1;
  169. relativeCcw(p: Point.PointLike | Point.PointData): -1 | 0 | 1;
  170. /**
  171. * Return `true` if the line equals the other line.
  172. */
  173. equals(l: Line): boolean;
  174. /**
  175. * Returns another line which is a clone of the line.
  176. */
  177. clone(): Line;
  178. toJSON(): {
  179. start: {
  180. x: number;
  181. y: number;
  182. };
  183. end: {
  184. x: number;
  185. y: number;
  186. };
  187. };
  188. serialize(): string;
  189. }
  190. export declare namespace Line {
  191. function isLine(instance: any): instance is Line;
  192. }