All files DLine.ts

97.31% Statements 145/149
93.9% Branches 77/82
100% Functions 31/31
97.27% Lines 143/147

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 38311x   11x 11x 11x     11x                   484790x 484790x 484790x 484790x 484790x       1x       573x             573x       372x             372x 372x 372x                     207867x 207867x 177759x 5602x   172157x   30108x       40x   40x 40x   40x   40x 1x     39x 2x     37x 37x 37x 37x   37x   37x 37x   37x                 195022x 195022x 195022x 195022x 195022x                 6307x 6307x       373x                           195023x       195023x       195023x       195023x       1x       3x               38056x 6141x   31915x 6727x   25188x               135958x 2x   135956x 2x   135954x                 208419x 15847x   192572x 13779x   178793x 11643x   167150x 11549x   155601x 9778x 9778x   145823x 12235x 12235x   133588x 6797x 6797x   126791x 7417x 7417x   119374x 119374x 485x   118889x             3x       899829x       870840x             4245x             69x         69x 69x 69x     69x             1x 1x                                 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 2x     3x   3x   4x                 18351x 18351x 18351x 142x 18209x     18351x               18345x 18345x                   7x       7x 7x 7x 7x 7x 7x   7x     7x 42x 42x 7x   35x 35x 23x     23x 23x   35x 30x     30x 30x       7x      
import {DPoint} from './DPoint';
import {DCircle} from './DCircle';
import {checkFunction} from './utils';
import {DNumbers} from './DNumbers';
import {DPolygon} from './DPolygon';
 
// eslint-disable-next-line padded-blocks
export class DLine {
 
  /**
   * @param a
   * @param b
   * @param c
   * @param [begin=DPoint.zero()]
   * @param [end=DPoint.zero()]
   */
  constructor(
    public a: number,
    public b: number,
    public c: number,
    public begin: DPoint = DPoint.zero(),
    public end: DPoint = DPoint.zero()
  ) {}
 
  clone(): DLine {
    return new DLine(this.a, this.b, this.c, this.begin.clone(), this.end.clone());
  }
 
  findPerpendicular(p: DPoint): DLine {
    checkFunction('findPerpendicular')
      .checkArgument('this.begin')
      .shouldBeMeters(this.begin)
      .checkArgument('this.end')
      .shouldBeMeters(this.end)
      .checkArgument('p')
      .shouldBeMeters(p);
    return new DLine(-this.b, this.a, this.b * p.x - this.a * p.y);
  }
 
  perpendicularDistance(p: DPoint): number {
    checkFunction('perpendicularDistance')
      .checkArgument('this.begin')
      .shouldBeMeters(this.begin)
      .checkArgument('this.end')
      .shouldBeMeters(this.end)
      .checkArgument('p')
      .shouldBeMeters(p);
    const perpendicularLine = this.findPerpendicular(p);
    const targetPoint = perpendicularLine.findPoint(this);
    return targetPoint!.distance(p);
  }
 
  /**
   * Find intersection of two lines segments.
   * For intersection of two lines use [[findPoint]]
   * @param l
   * @param [d=0]
   * @param [includeOnly=false]
   */
  intersection(l: DLine, d: number = 0, includeOnly: boolean = false): DPoint | null {
    const p = this.findPoint(l);
    if (p) {
      if (includeOnly) {
        return this.insideRange(p, d) && l.insideRange(p, d) ? p : null;
      }
      return this.inRange(p, d) && l.inRange(p, d) ? p : null;
    }
    return null;
  }
 
  intersectionWithCircle(circle: DCircle): DPoint | [DPoint, DPoint] | null {
    const {center, r} = circle;
 
    const per = this.findPerpendicular(center);
    const t = this.findPoint(per)!;
 
    const d = t.distance(center);
 
    if (d > r) {
      return null;
    }
 
    if (d === r) {
      return t;
    }
 
    const {a, b} = this;
    const len = Math.sqrt(a * a + b * b);
    const dx = -b / len;
    const dy = a / len;
 
    const h = Math.sqrt(r * r - d * d);
 
    const p1 = t.clone().move(dx * h, dy * h);
    const p2 = t.clone().move(-dx * h, -dy * h);
 
    return [p1, p2];
  }
 
  /**
   * Check if point below to line segment
   * @param p
   * @param d
   */
  inRange(p: DPoint, d: number = 0): boolean {
    const {minX, minY, maxX, maxY} = this;
    const {x, y} = p;
    const isInX = x >= minX - d && x <= maxX + d;
    const isInY = y >= minY - d && y <= maxY + d;
    return isInX && isInY;
  }
 
  /**
   * Check if point below to line segment, but not equal star or end point.
   * @param p
   * @param [d=0]
   */
  insideRange(p: DPoint, d: number = 0): boolean {
    const {begin, end} = this;
    return this.inRange(p, d) && !begin.like(p, 0.00001) && !end.like(p, 0.00001);
  }
 
  get center(): DPoint {
    return this.begin
      .clone()
      .setIfLessThan(this.end)
      .move(this.end
        .clone()
        .move(this.begin
          .clone()
          .minus())
        .abs()
        .minus()
        .divide(2));
  }
 
  get minX(): number {
    return Math.min(this.begin.x, this.end.x);
  }
 
  get minY(): number {
    return Math.min(this.begin.y, this.end.y);
  }
 
  get maxX(): number {
    return Math.max(this.begin.x, this.end.x);
  }
 
  get maxY(): number {
    return Math.max(this.begin.y, this.end.y);
  }
 
  toString(): string {
    return `(${this.a}, ${this.b}, ${this.c})`;
  }
 
  getValue(): number[] {
    return [this.a, this.b, this.c];
  }
 
  /**
   * Find `x` from `y` value of point
   * @param p
   */
  x(p: DPoint): DPoint {
    if (this.isParallelY) {
      return new DPoint(-this.c / this.a, p.y);
    }
    if (this.isParallelX) {
      return new DPoint(p.x, -this.c / this.b);
    }
    return new DPoint(-this.b / this.a * p.y - this.c / this.a, p.y);
  }
 
  /**
   * Find `y` from `x` value of point
   * @param p
   */
  y(p: DPoint): DPoint {
    if (this.isParallelY) {
      return new DPoint(-this.c / this.a, p.y);
    }
    if (this.isParallelX) {
      return new DPoint(p.x, -this.c / this.b);
    }
    return new DPoint(p.x, -this.a / this.b * p.x - this.c / this.b);
  }
 
  /**
   * Find intersection of two lines.
   * For intersection of two lines segments use [[intersection]]
   * @param l
   */
  findPoint(l: DLine): DPoint | null {
    if (this.isParallelY && l.isParallelY) {
      return null;
    }
    if (this.isParallelX && l.isParallelX) {
      return null;
    }
    if (this.isParallelX && l.isParallelY) {
      return new DPoint(-l.c / l.a, -this.c / this.b);
    }
    if (this.isParallelY && l.isParallelX) {
      return new DPoint(-this.c / this.a, -l.c / l.b);
    }
    if (this.isParallelY) {
      const x = -this.c / this.a;
      return l.y(new DPoint(x));
    }
    if (this.isParallelX) {
      const y = -this.c / this.b;
      return l.x(new DPoint(0, y));
    }
    if (l.isParallelY) {
      const x = -l.c / l.a;
      return this.y(new DPoint(x));
    }
    if (l.isParallelX) {
      const y = -l.c / l.b;
      return this.x(new DPoint(0, y));
    }
    const res = this.y(new DPoint((l.c / l.b - this.c / this.b) / (this.a / this.b - l.a / l.b)));
    if (!isFinite(res.x) && !isFinite(res.y)) {
      return null;
    }
    return res;
  }
 
  /**
   * Check if line parallel to `x` or `y` axis
   */
  get isParallel(): boolean {
    return this.isParallelX || this.isParallelY;
  }
 
  get isParallelY(): boolean {
    return Math.abs(this.b) < 0.001;
  }
 
  get isParallelX(): boolean {
    return Math.abs(this.a) < 0.001;
  }
 
  /**
   * Get lines segment start and end points as array
   */
  get points(): [DPoint, DPoint] {
    return [this.begin, this.end];
  }
 
  /**
   * Get line segment direction (from start point to end point)
   */
  getFi(): number {
    checkFunction('getFi')
      .checkArgument('this.begin')
      .shouldBeMeters(this.begin)
      .checkArgument('this.end')
      .shouldBeMeters(this.end);
    const {x, y} = this.end.clone().move(this.begin.clone().minus());
    let v = Math.atan2(y, x) - Math.PI;
    Iif (v > 0) {
      v = Math.PI - v;
    }
    return (Math.PI - v) % (Math.PI * 2);
  }
 
  toWKT(): string {
    const {
      begin: {x: x1, y: y1},
      end: {x: x2, y: y2}
    } = this;
    return `LINESTRING (${x1} ${y1}, ${x2} ${y2})`;
  }
 
  /**
   * Move lines point to left or right
   * @param point
   * @param distance
   */
  movePoint(point: DPoint, distance: number): DPoint;
 
  /**
   * Move lines point to left or right
   * @param points
   * @param distances
   */
  movePoint(points: DPoint[], distances: number[]): DPoint[];
  movePoint(i: DPoint | DPoint[], k: number | number[]): DPoint[] | DPoint {
    const isArray = Array.isArray(i);
    const p: DPoint[] = isArray ? i : [i];
    const d: number[] = (isArray ? k : [k]) as number[];
    const fi = this.findFi(new DLine(1, 0, 0));
    const td = this.x(new DPoint(1, 1)).distance(this.x(new DPoint(2, 2))) / 2;
    const sinCos = new DPoint(Math.sin(fi), Math.cos(fi));
    const dt = sinCos.clone().scale(td);
    const p1T = p[0].clone().move(dt.clone().minus());
    const p2T = p[0].clone().move(dt);
    let res: DPoint[] = [];
    if (DNumbers.like(this.y(p1T).y, p1T.y) || DNumbers.like(this.y(p2T).y, p2T.y)) {
      res = p.map((t: DPoint, index: number) => t.clone()
        .move(sinCos.clone().scale(d[index])));
    } else {
      res = p.map((t: DPoint, index: number) => t.clone()
        .move(sinCos.clone().scale(d[index])
          .setX(({x}) => -x)));
    }
    return isArray ? res : res[0];
  }
 
  /**
   * Find angle between current line and line in argument
   * @param l
   * @param delta
   */
  findFi({a, b}: DLine, delta = 1.0001): number {
    const {a: q, b: w} = this;
    let val = (q * a + w * b) / (Math.sqrt(q * q + w * w) * Math.sqrt(a * a + b * b));
    if (val > 1 && val < delta) {
      val = 1;
    } else Iif (val < -1 && val > -delta) {
      val = -1;
    }
    return Math.acos(val);
  }
 
  /**
   * [Cross product](https://en.wikipedia.org/wiki/Cross_product)
   * @param l {DLine}
   */
  vectorProduct({a, b, c}: DLine): DLine {
    const {a: q, b: w, c: e} = this;
    return new DLine(w * c - e * b, e * a - q * c, q * b - w * a);
  }
 
  /**
   * [Bresenham's line algorithm](https://en.wikipedia.org/wiki/Bresenham%27s_line_algorithm)
   */
  bresenhamsLine(): DPolygon {
    let {
      x: x0,
      y: y0
    } = this.begin;
    const {
      x: x1,
      y: y1
    } = this.end;
    const dx = Math.abs(x1 - x0);
    const sx = x0 < x1 ? 1 : -1;
    const dy = -Math.abs(y1 - y0);
    const sy = y0 < y1 ? 1 : -1;
    let error = dx + dy;
 
    const res = new DPolygon();
 
    // eslint-disable-next-line no-constant-condition
    while (true) {
      res.push(new DPoint(x0, y0));
      if (x0 === x1 && y0 === y1) {
        break;
      }
      const e2 = 2 * error;
      if (e2 >= dy) {
        Iif (x0 === x1) {
          break;
        }
        error += dy;
        x0 += sx;
      }
      if (e2 <= dx) {
        Iif (y0 === y1) {
          break;
        }
        error += dx;
        y0 += sy;
      }
    }
 
    return res;
  }
}