All files DPoint.ts

100% Statements 367/367
99.42% Branches 174/175
100% Functions 91/91
100% Lines 361/361

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 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099  11x 11x 11x     11x   11x   11x   11x   11x                                   11x 11x 11x 11x 11x 11x 11x 11x 11x       11x   781246x                                                     781246x     111445x                     1499x 1499x 4x   1495x 1495x 10x   1485x 1485x 2x   1485x 1x 1x     1x   1484x     4452x   1484x 1484x   1484x 1484x   1484x 1484x     4452x         11x 11x 11x 11x 11x       2x               4x 4x   15x 15x   9x     1x 1x     1x 1x     3x   3x 3x   1x     3x       2x                           6x     6x 6x     6x                     3x 3x 14x   14x   14x 4x     14x 4x 4x   14x   3x                     7x     7x 7x 7x 7x             1997x 5988x         5988x             6x             4x                           429170x         429170x 3x   429167x 429167x 429167x 429167x 72779x   356388x 66372x   290016x       30x             30x 30x 30x 16x   14x       32131x       15x       64x 64x       4x 4x       2322x         2322x 2322x 2322x       8x         8x   8x 8x 8x       8x 8x       1x                 1x 1x 1x 1x                             81x 81x                             15x 15x                               16x 16x         26x 26x       132317x 132317x 132317x       11x       6x       8x       3x               12x 12x 12x 12x 12x               9x 9x 9x 9x               9x 9x 9x 9x               9x 9x 9x 9x                                                           62129x 62129x   62129x 62129x 60251x 60251x 60251x 82x     1878x 1878x 1878x 1x     62129x 62129x 62129x 83x   62129x       13x     13x 13x   13x 13x 13x       7x     7x 7x   7x 7x 7x       307x     307x       642x     642x       2x     2x       2x     2x       179x 179x 179x       8x 8x 8x       29x 29x 29x             44x 44x 44x       379x 379x 379x                                                           2509x 2509x   2509x 2509x 2x 2x 2x 1x     2507x 2507x 2507x 1x     2509x 2509x 2509x 2x   2509x                                                           574x 574x   574x 574x 4x 4x 4x 1x     570x 570x 570x 1x     574x 574x 574x 2x   574x                                                                   9849x 9849x   9849x 9849x 4x 4x 4x 1x     9845x 9845x 9845x 1x     9849x 9849x 9849x 2x   9849x                                                                   2469x 2469x   2469x 2469x 4x 4x 4x 1x     2465x 2465x 2465x 1x     2469x 2469x 2469x 2x   2469x       487269x               7298x 3223x   4075x 4075x 4075x 4075x                             19x 19x 11x   19x 19x             31645x 3x   31645x             15820x 3x   15820x               15687x 3x   15687x       5527x       1x       51624x       1x       1x     1x       8x     8x       3x       3x       5x 5x       10x 10x       10x 10x       5x       5x       32x       2x       5x       1x       12x       2x       70x       11x       5x                   458x 458x 458x       81x 81x 81x       1024x                           1x         1x 1x 1x 1x 1x   360x 360x   360x         3x     12x 12x 12x   15x       9x 2x   7x 7x 7x 7x 7x 7x 7x 5x   2x     9x      
/* eslint-disable max-lines */
import {DLine} from './DLine';
import {DPolygon} from './DPolygon';
import {checkFunction, createArray, DGeo, div, isDefAndNotNull, toDegreesMinutesSeconds} from './utils';
import {Point, Feature} from 'geojson';
 
const diff = 0;
 
const radiansPolygon = new DPolygon();
 
const pseudoMercatorPolygon = new DPolygon();
 
const worldGeodeticPolygon = new DPolygon();
// For WGS-84 6378137, for IAU-2009 6378136.6
export const EARTH_RADIUS_IN_METERS = 6378137;
 
export type DCoord = [number, number] | [number, number, number];
 
export interface LatLng {
  lat: number;
  lng?: number;
  lon?: number;
  alt?: number;
}
 
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export interface XYZ extends Record<string, any> {
  x: number;
  y: number;
  z?: number;
}
 
const EARTH_IN_METERS = 20037508.34;
const DEGREES_IN_EARTH = 180;
const METERS_IN_ONE_DEGREE = EARTH_IN_METERS / DEGREES_IN_EARTH;
const DEGREES_IN_ONE_METER = DEGREES_IN_EARTH / EARTH_IN_METERS;
export const HALF_PI_IN_DEGREE = 90;
export const PI_IN_DEGREE = 180;
export const DOUBLE_PI_IN_DEGREE = 360;
export const PI_TO_DEGREE = Math.PI / PI_IN_DEGREE;
export const DEGREE_TO_PI = PI_IN_DEGREE / Math.PI;
 
type SetterFunction<T> = (t: DPoint) => T;
 
export class DPoint {
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
  properties: Record<string, any> = {};
 
  /**
   * Create point with zero coords `(0, 0)`
   */
  constructor();
 
  /**
   * Create point
   * @param xy - `x` and `y` value
   */
  constructor(xy: number);
 
  /**
   * Create point
   * @param x - lng, meters to East, radians to East or width
   * @param y - lat, meters to North, radians to North or height
   */
  constructor(x: number, y: number);
 
  /**
   * Create point
   * @param x - lng, meters to East, radians to East or width
   * @param y - lat, meters to North, radians to North or height
   * @param z - height
   */
  constructor(x: number, y: number, z?: number);
  constructor(public x: number = 0, public y: number = x, public z?: number) {}
 
  static zero(): DPoint {
    return new DPoint();
  }
 
  /**
   * @param c
   * @param [format='xyz'] Default value `DGeo.parseFormat`
   */
  static parse(
    c: LatLng | number[] | DCoord | Point | Feature<Point> | XYZ,
    format: string = DGeo.parseFormat
  ): DPoint {
    const {lat, lon, lng = lon, alt} = c as LatLng;
    if (lat && lng) {
      return new DPoint(lng, lat, alt ?? 0);
    }
    const {x, y, z, ...properties} = c as XYZ;
    if (isDefAndNotNull(x) && isDefAndNotNull(y)) {
      return new DPoint(x, y, z).setProperties(properties);
    }
    let t = c as DCoord;
    if ((c as Point).type === 'Point') {
      t = (c as Point).coordinates as DCoord;
    }
    if ((c as Feature<Point>).type === 'Feature') {
      const f = DPoint.parse((c as Feature<Point>).geometry, format);
      f.properties = {
        ...(c as Feature<Point>).properties
      };
      return f;
    }
    return format.replace(/[^x-z]/gmiu, '')
      .split('')
      .reduce((a: DPoint, k: string, index: number) => {
        switch (k) {
          case 'x':
            a.x = t[index] ?? 0;
            break;
          case 'y':
            a.y = t[index] ?? 0;
            break;
          case 'z':
            a.z = t[index];
            break;
          default:
        }
        return a;
      }, new DPoint());
  }
 
  static parseFromWKT(wkt: string): DPoint {
    const regexp = /POINT \((?<data>(?:(?!\)).)*?)\)$/miu;
    const data = wkt.trim().toUpperCase();
    const res = regexp.exec(data)!;
    const [x, y, z] = res.groups!.data.split(' ').map(Number);
    return new DPoint(x, y, z);
  }
 
  static random(): DPoint {
    return new DPoint(Math.random(), Math.random());
  }
 
  /**
   * Parse tile coords from BING Map quad string
   * @param quadKey
   */
  static getTileFromQuadKey(quadKey: string): DPoint {
    const p = new DPoint(0, 0, quadKey.length);
    for (let i = p.z!; i > 0; i--) {
      // eslint-disable-next-line no-bitwise
      const mask = 1 << (i - 1);
      switch (quadKey[p.z! - i]) {
        case '0':
          break;
        case '1':
          // eslint-disable-next-line no-bitwise
          p.x |= mask;
          break;
        case '2':
          // eslint-disable-next-line no-bitwise
          p.y |= mask;
          break;
        case '3':
          // eslint-disable-next-line no-bitwise
          p.x |= mask;
          // eslint-disable-next-line no-bitwise
          p.y |= mask;
          break;
        default:
          throw new Error('Invalid QuadKey digit sequence.');
      }
    }
    return p;
  }
 
  toDegreesMinutesSeconds(): {x: string, y: string} {
    return {
      x: toDegreesMinutesSeconds(this.x),
      y: toDegreesMinutesSeconds(this.y)
    };
  }
 
  /**
   * @remark Point should be Lng/Lat.
   *
   * @remark `z` value default for `zoom` argument.
   *
   * @param [zoom=this.z]
   */
  getTileFromCoords(zoom: number = this.z!): DPoint {
    checkFunction('getTileFromCoords')
      .checkArgument('this')
      .shouldBeDegree(this);
    const x = Math.floor((this.x + PI_IN_DEGREE) / DOUBLE_PI_IN_DEGREE * (2 ** zoom));
    const y = Math.floor((
      1 - Math.log(Math.tan(this.y * PI_TO_DEGREE) + 1 / Math.cos(this.y * PI_TO_DEGREE)) / Math.PI
    ) / 2 * (2 ** zoom));
    return new DPoint(x, y, zoom);
  }
 
  /**
   * Get BING Maps quad key
   *
   * @remark `z` value default for `zoom` argument.
   *
   * @param [zoom=this.z]
   */
  getQuadKeyFromTile(zoom: number = this.z!): string {
    const quadKey = [];
    for (let i = zoom; i > 0; i--) {
      let digit = 0;
      // eslint-disable-next-line no-bitwise
      const mask = 1 << (i - 1);
      // eslint-disable-next-line no-bitwise
      if ((this.x & mask) !== 0) {
        digit++;
      }
      // eslint-disable-next-line no-bitwise
      if ((this.y & mask) !== 0) {
        digit++;
        digit++;
      }
      quadKey.push(digit);
    }
    return quadKey.join('');
  }
 
  /**
   * Result would be Lng/Lat.
   *
   * @remark `z` value default for `zoom` argument.
   *
   * @param [zoom=this.z]
   */
  getCoordsFromTile(zoom: number = this.z!): DPoint {
    checkFunction('getCoordsFromTile')
      .checkArgument('this')
      .shouldBeUInt(this);
    const n = Math.PI - 2 * Math.PI * this.y / (2 ** zoom);
    const x = this.x / (2 ** zoom) * DOUBLE_PI_IN_DEGREE - PI_IN_DEGREE;
    const y = PI_IN_DEGREE / Math.PI * Math.atan((Math.exp(n) - Math.exp(-n)) / 2);
    return new DPoint(x, y, zoom);
  }
 
  /**
   * @param [format='xyz'] Default value `DGeo.parseFormat`
   */
  toCoords(format: string = DGeo.parseFormat): DCoord {
    return format.replace(/[^x-z]/gmiu, '').split('')
      .map((k) => ({
        x: this.x,
        y: this.y,
        z: this.z
      })[k])
      .filter((r) => r !== undefined) as DCoord;
  }
 
  /**
   * @param [format='xyz'] Default value `DGeo.parseFormat`
   */
  toGeoJSON(format: string = DGeo.parseFormat): Point {
    return {
      type: 'Point',
      coordinates: this.toCoords(format)
    };
  }
 
  toGeoJSONFeature(format: string = DGeo.parseFormat): Feature<Point> {
    return {
      type: 'Feature',
      properties: {
        ...this.properties
      },
      geometry: this.toGeoJSON(format)
    };
  }
 
  /**
   * Find line between two points.
   * @param p
   */
  findLine(p: DPoint): DLine {
    checkFunction('findLine')
      .checkArgument('this')
      .shouldBeMeters(this)
      .checkArgument('p')
      .shouldBeMeters(p);
    if (this.equal(p)) {
      return this.findLine(p.clone().move(0, 1));
    }
    const a = this.y - p.y - diff;
    const b = p.x - this.x - diff;
    const c = this.x * p.y - p.x * this.y - diff;
    if (a === 0) {
      return new DLine(0, 1, c / b, this, p);
    }
    if (b === 0) {
      return new DLine(1, 0, c / a, this, p);
    }
    return new DLine(a, b, c, this, p);
  }
 
  findInnerAngle(p1: DPoint, p3: DPoint): number {
    checkFunction('findInnerAngle')
      .checkArgument('this')
      .shouldBeMeters(this)
      .checkArgument('p1')
      .shouldBeMeters(p1)
      .checkArgument('p3')
      .shouldBeMeters(p3);
    const a1 = this.findLine(p1).getFi();
    const a2 = this.findLine(p3).getFi();
    if (a2 >= a1) {
      return a2 - a1;
    }
    return a2 + Math.PI * 2 - a1;
  }
 
  toString(): string {
    return `${this.x} ${this.y}`;
  }
 
  getValue(): [number, number] {
    return [this.x, this.y];
  }
 
  height(z: number): DPoint {
    this.z = z;
    return this;
  }
 
  toWKT(): string {
    const {x, y} = this;
    return `POINT (${x} ${y})`;
  }
 
  distance(p: DPoint): number {
    checkFunction('distance')
      .checkArgument('this')
      .shouldBeMeters(this)
      .checkArgument('p')
      .shouldBeMeters(p);
    const dx = p.x - this.x;
    const dy = p.y - this.y;
    return Math.sqrt(dx * dx + dy * dy);
  }
 
  distanceLatLon(p: DPoint): number {
    checkFunction('distance')
      .checkArgument('this')
      .shouldBeDegree(this)
      .checkArgument('p')
      .shouldBeDegree(p);
    const d = p.clone().move(this.clone().minus())
      .degreeToRadians();
    const t1 = this.clone().degreeToRadians();
    const t2 = p.clone().degreeToRadians();
    const a = Math.sin(d.lat / 2) ** 2 +
      Math.cos(t1.lat) *
      Math.cos(t2.lat) *
      ((Math.sin(d.lon / 2)) ** 2);
    const c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
    return EARTH_RADIUS_IN_METERS * c;
  }
 
  distance3d(p: DPoint): number {
    checkFunction('distance3d')
      .checkArgument('this')
      .shouldBeMeters(this)
      .checkArgument('p')
      .shouldBeMeters(p)
      .checkArgument('this.z')
      .shouldExist(this.z)
      .checkArgument('p.z')
      .shouldExist(p.z);
    const dx = p.x - this.x;
    const dy = p.y - this.y;
    const dz = p.z! - this.z!;
    return Math.sqrt(dx * dx + dy * dy + dz * dz);
  }
 
  /**
   * Set `x` value
   * @param x
   */
  setX(x: number): DPoint;
 
  /**
   * Transform `x` value by function
   * @param f
   */
  setX(f: SetterFunction<number>): DPoint;
  setX(x: number | SetterFunction<number>): DPoint {
    this.x = typeof x === 'number' ? x : x(this);
    return this;
  }
 
  /**
   * Set `z` value
   * @param z
   */
  setZ(z: number): DPoint;
 
  /**
   * Transform `z` value by function
   * @param f
   */
  setZ(f: SetterFunction<number>): DPoint;
  setZ(z: number | SetterFunction<number>): DPoint {
    this.z = typeof z === 'number' ? z : z(this);
    return this;
  }
 
 
  /**
   * Set `y` value
   * @param y
   */
  setY(y: number): DPoint;
 
  /**
   * Transform `y` value by function
   * @param f
   */
  setY(f: SetterFunction<number>): DPoint;
  setY(y: number | SetterFunction<number>): DPoint {
    this.y = typeof y === 'number' ? y : y(this);
    return this;
  }
 
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
  setProperties(v: SetterFunction<Record<string, any>> | Record<string, any>): DPoint {
    this.properties = typeof v === 'object' ? v : v(this);
    return this;
  }
 
  clone(): DPoint {
    const p = new DPoint(this.x, this.y, this.z);
    p.properties = {...this.properties};
    return p;
  }
 
  gt(p: DPoint): boolean {
    return this.x > p.x && this.y > p.y;
  }
 
  lt(p: DPoint): boolean {
    return this.x < p.x && this.y < p.y;
  }
 
  gtOrEqual(p: DPoint): boolean {
    return this.gt(p) || this.equal(p);
  }
 
  ltOrEqual(p: DPoint): boolean {
    return this.lt(p) || this.equal(p);
  }
 
  /**
   * Clockwise rotation
   * @param a radians
   */
  rotate(a: number): DPoint {
    const x = this.x * Math.cos(a) - this.y * Math.sin(a);
    const y = this.x * Math.sin(a) + this.y * Math.cos(a);
    this.x = x;
    this.y = y;
    return this;
  }
 
  /**
   * 3D rotation
   * @param a radians
   */
  rotate3dX(a: number): DPoint {
    const {y, z} = this;
    this.y = y * Math.cos(a) + z! * Math.sin(a);
    this.z = -y * Math.sin(a) + z! * Math.cos(a);
    return this;
  }
 
  /**
   * 3D rotation
   * @param a radians
   */
  rotate3dY(a: number): DPoint {
    const {x, z} = this;
    this.x = x * Math.cos(a) + z! * Math.sin(a);
    this.z = -x * Math.sin(a) + z! * Math.cos(a);
    return this;
  }
 
  /**
   * 3D rotation
   * @param a radians
   */
  rotate3dZ(a: number): DPoint {
    const {x, y} = this;
    this.x = x * Math.cos(a) - y * Math.sin(a);
    this.y = -x * Math.sin(a) + y * Math.cos(a);
    return this;
  }
 
  /**
   * Add `v` to `x`, `y` and `z` (if `z` exist)
   * @param v
   */
  move(v: number): DPoint;
 
  /**
   * Add `p.x` to `x` field, `p.y` to `y` field and `p.z` to `z` field (if `z exist).
   * @param p
   */
  move(p: DPoint): DPoint;
 
  /**
   * Add `x` to `x` field and `y` to `y` field.
   * @param x
   * @param y
   */
  move(x: number, y: number): DPoint;
 
  /**
   * Add `x` to `x` field, `y` to `y` field and `z` to `z` field (if `z` exist).
   * @param x
   * @param y
   * @param z
   */
  move(x: number, y: number, z: number): DPoint;
  move(x: number | DPoint, y: number = (x as number), z?: number): DPoint {
    let xV = 0;
    let yV = 0;
    // eslint-disable-next-line no-undef-init
    let zV: number | undefined = undefined;
    if (x instanceof DPoint) {
      xV = this.x + x.x;
      yV = this.y + x.y;
      if (isDefAndNotNull(this.z) && isDefAndNotNull(x.z)) {
        zV = this.z! + x.z!;
      }
    } else {
      xV = this.x + (x as number);
      yV = this.y + y;
      if (isDefAndNotNull(this.z) && isDefAndNotNull(z)) {
        zV = this.z! + z!;
      }
    }
    this.x = xV;
    this.y = yV;
    if (isDefAndNotNull(zV)) {
      this.z = zV;
    }
    return this;
  }
 
  degreeToMeters(): DPoint {
    checkFunction('degreeToMeters')
      .checkArgument('this')
      .shouldBeDegree(this);
    const x = ((this.x + PI_IN_DEGREE) % DOUBLE_PI_IN_DEGREE - PI_IN_DEGREE) * METERS_IN_ONE_DEGREE;
    const y = (Math.log(Math.tan(((this.y + HALF_PI_IN_DEGREE) % PI_IN_DEGREE) *
      (Math.PI / DOUBLE_PI_IN_DEGREE))) / PI_TO_DEGREE) * METERS_IN_ONE_DEGREE;
    this.x = x;
    this.y = y;
    return this;
  }
 
  metersToDegree(): DPoint {
    checkFunction('metersToDegree')
      .checkArgument('this')
      .shouldBeMeters(this);
    const lon = this.x * DEGREES_IN_ONE_METER;
    const lat = Math.atan(Math.E ** ((this.y / METERS_IN_ONE_DEGREE) * PI_TO_DEGREE)) *
      (DOUBLE_PI_IN_DEGREE / Math.PI) - HALF_PI_IN_DEGREE;
    this.x = lon;
    this.y = lat;
    return this;
  }
 
  degreeToRadians(): DPoint {
    checkFunction('degreeToRadians')
      .checkArgument('this')
      .shouldBeDegree(this);
    return this.scale(PI_TO_DEGREE);
  }
 
  radiansToDegrees(): DPoint {
    checkFunction('radiansToDegrees')
      .checkArgument('this')
      .shouldBeRadians(this);
    return this.scale(DEGREE_TO_PI);
  }
 
  radiansToMeters(): DPoint {
    checkFunction('radiansToMeters')
      .checkArgument('this')
      .shouldBeRadians(this);
    return this.radiansToDegrees().degreeToMeters();
  }
 
  metersToRadians(): DPoint {
    checkFunction('metersToRadians')
      .checkArgument('this')
      .shouldBeMeters(this);
    return this.metersToDegree().degreeToRadians();
  }
 
  round(): DPoint {
    this.x = Math.round(this.x);
    this.y = Math.round(this.y);
    return this;
  }
 
  ceil(): DPoint {
    this.x = Math.ceil(this.x);
    this.y = Math.ceil(this.y);
    return this;
  }
 
  floor(): DPoint {
    this.x = Math.floor(this.x);
    this.y = Math.floor(this.y);
    return this;
  }
 
  /**
   * @param [n=2]
   */
  toFixed(n: number = 2): DPoint {
    this.x = parseFloat(this.x.toFixed(n));
    this.y = parseFloat(this.y.toFixed(n));
    return this;
  }
 
  abs(): DPoint {
    this.x = Math.abs(this.x);
    this.y = Math.abs(this.y);
    return this;
  }
 
  /**
   * Multiply `v` to `x`, `y` and `z` (if `z` exist)
   * @param v
   */
  scale(v: number): DPoint;
 
  /**
   * Multiply `p.x` to `x` field, `p.y` to `y` field and `p.z` to `z` field (if `z` exist).
   * @param p
   */
  scale(p: DPoint): DPoint;
 
  /**
   * Multiply `x` to `x` field and `y` to `y` field.
   * @param x
   * @param y
   */
  scale(x: number, y: number): DPoint;
 
  /**
   * Multiply `x` to `x` field, `y` to `y` field and `z` to `z` field (if `z` exist).
   * @param x
   * @param y
   * @param z
   */
  scale(x: number, y: number, z: number): DPoint;
  scale(x: number | DPoint, y: number = (x as number), z?: number): DPoint {
    let xV = 0;
    let yV = 0;
    // eslint-disable-next-line no-undef-init
    let zV: number | undefined = undefined;
    if (x instanceof DPoint) {
      xV = this.x * x.x;
      yV = this.y * x.y;
      if (isDefAndNotNull(this.z) && isDefAndNotNull(x.z)) {
        zV = this.z! * x.z!;
      }
    } else {
      xV = this.x * (x as number);
      yV = this.y * y;
      if (isDefAndNotNull(this.z) && isDefAndNotNull(z)) {
        zV = this.z! * z!;
      }
    }
    this.x = xV;
    this.y = yV;
    if (isDefAndNotNull(zV)) {
      this.z = zV;
    }
    return this;
  }
 
  /**
   * Divide `x`, `y` and `z` (if `z` exist) to `v`
   * @param v
   */
  divide(v: number): DPoint;
 
  /**
   * Divide `x` field to `p.x`, `y` field to `p.y` and `z` field to `p.z` (if `z` exist).
   * @param p
   */
  divide(p: DPoint): DPoint;
 
  /**
   * Divide `x` field to `x` and `y` field to `y`.
   * @param x
   * @param y
   */
  divide(x: number, y: number): DPoint;
 
  /**
   * Divide `x` field to `x`, `y` field to `y` and `z` field to `z`.
   * @param x
   * @param y
   * @param z
   */
  divide(x: number, y: number, z: number): DPoint;
  divide(x: number | DPoint, y: number = (x as number), z?: number): DPoint {
    let xV = 0;
    let yV = 0;
    // eslint-disable-next-line no-undef-init
    let zV: number | undefined = undefined;
    if (x instanceof DPoint) {
      xV = this.x / x.x;
      yV = this.y / x.y;
      if (isDefAndNotNull(this.z) && isDefAndNotNull(x.z)) {
        zV = this.z! / x.z!;
      }
    } else {
      xV = this.x / (x as number);
      yV = this.y / y;
      if (isDefAndNotNull(this.z) && isDefAndNotNull(z)) {
        zV = this.z! / z!;
      }
    }
    this.x = xV;
    this.y = yV;
    if (isDefAndNotNull(zV)) {
      this.z = zV;
    }
    return this;
  }
 
  /**
   * [Modulo operation](https://en.wikipedia.org/wiki/Modulo_operation)
   * Divide `x`, `y` and `z` (if `z` exist) to `v`
   * @param v
   */
  mod(v: number): DPoint;
 
  /**
   * [Modulo operation](https://en.wikipedia.org/wiki/Modulo_operation)
   * Divide `x` field to `p.x`, `y` field to `p.y` and `z` field to `p.z` (if `z` exist).
   * @param p
   */
  mod(p: DPoint): DPoint;
 
  /**
   * [Modulo operation](https://en.wikipedia.org/wiki/Modulo_operation)
   * Divide `x` field to `x` and `y` field to `y`.
   * @param x
   * @param y
   */
  mod(x: number, y: number): DPoint;
 
  /**
   * [Modulo operation](https://en.wikipedia.org/wiki/Modulo_operation)
   * Divide `x` field to `x`, `y` field to `y` and `z` field to `z`.
   * @param x
   * @param y
   * @param z
   */
  mod(x: number, y: number, z: number): DPoint;
  mod(x: number | DPoint, y: number = (x as number), z?: number): DPoint {
    let xV = 0;
    let yV = 0;
    // eslint-disable-next-line no-undef-init
    let zV: number | undefined = undefined;
    if (x instanceof DPoint) {
      xV = this.x % x.x;
      yV = this.y % x.y;
      if (isDefAndNotNull(this.z) && isDefAndNotNull(x.z)) {
        zV = this.z! % x.z!;
      }
    } else {
      xV = this.x % (x as number);
      yV = this.y % y;
      if (isDefAndNotNull(this.z) && isDefAndNotNull(z)) {
        zV = this.z! % z!;
      }
    }
    this.x = xV;
    this.y = yV;
    if (isDefAndNotNull(zV)) {
      this.z = zV;
    }
    return this;
  }
 
  /**
   * [Euclidean division](https://en.wikipedia.org/wiki/Euclidean_division)
   * Divide `x`, `y` and `z` (if `z` exist) to `v`
   * @param v
   */
  div(v: number): DPoint;
 
  /**
   * [Euclidean division](https://en.wikipedia.org/wiki/Euclidean_division)
   * Divide `x` field to `p.x`, `y` field to `p.y` and `z` field to `p.z` (if `z` exist).
   * @param p
   */
  div(p: DPoint): DPoint;
 
  /**
   * [Euclidean division](https://en.wikipedia.org/wiki/Euclidean_division)
   * Divide `x` field to `x` and `y` field to `y`.
   * @param x
   * @param y
   */
  div(x: number, y: number): DPoint;
 
  /**
   * [Euclidean division](https://en.wikipedia.org/wiki/Euclidean_division)
   * Divide `x` field to `x`, `y` field to `y` and `z` field to `z`.
   * @param x
   * @param y
   * @param z
   */
  div(x: number, y: number, z: number): DPoint;
  div(x: number | DPoint, y: number = (x as number), z?: number): DPoint {
    let xV = 0;
    let yV = 0;
    // eslint-disable-next-line no-undef-init
    let zV: number | undefined = undefined;
    if (x instanceof DPoint) {
      xV = div(this.x, x.x);
      yV = div(this.y, x.y);
      if (isDefAndNotNull(this.z) && isDefAndNotNull(x.z)) {
        zV = div(this.z!, x.z!);
      }
    } else {
      xV = div(this.x, (x as number));
      yV = div(this.y, y);
      if (isDefAndNotNull(this.z) && isDefAndNotNull(z)) {
        zV = div(this.z!, z!);
      }
    }
    this.x = xV;
    this.y = yV;
    if (isDefAndNotNull(zV)) {
      this.z = zV;
    }
    return this;
  }
 
  equal(p: DPoint): boolean {
    return this.x === p.x && this.y === p.y && this.z === p.z;
  }
 
  /**
   * @param p
   * @param [d=0.001]
   */
  like(p: DPoint, d = 0.001): boolean {
    if (this.equal(p)) {
      return true;
    }
    const likeX = Math.abs(this.x - p.x) < d;
    const likeY = Math.abs(this.y - p.y) < d;
    const likeZ = Math.abs((this.z ?? p.z ?? 0) - (p.z ?? this.z ?? 0)) < d;
    return likeX && likeY && likeZ;
  }
 
  /**
   * Flip vertically
   * @param size canvas size
   */
  flipVertically(size: DPoint): DPoint;
 
  /**
   * Flip vertically
   * @param height canvas height
   */
  flipVertically(height: number): DPoint;
  flipVertically(size: DPoint | number): DPoint {
    let v = size as number;
    if (size instanceof DPoint) {
      v = size.y;
    }
    this.y = v - this.y;
    return this;
  }
 
  /**
   * Check if point looks like radians
   */
  get likeRadians(): boolean {
    if (radiansPolygon.length === 0) {
      radiansPolygon.push(new DPoint(-Math.PI, -Math.PI / 2), new DPoint(Math.PI, Math.PI / 2));
    }
    return radiansPolygon.simpleInclude(this);
  }
 
  /**
   * Check if point looks like `EPSG:4326` (degrees)
   */
  get likeWorldGeodeticSystem(): boolean {
    if (worldGeodeticPolygon.length === 0) {
      worldGeodeticPolygon.push(new DPoint(-180, -90), new DPoint(180, 90));
    }
    return !this.likeRadians && worldGeodeticPolygon.simpleInclude(this);
  }
 
 
  /**
   * Check if point looks like `EPSG:3857` (meters)
   */
  get likePseudoMercator(): boolean {
    if (pseudoMercatorPolygon.length === 0) {
      pseudoMercatorPolygon.push(new DPoint(-20026376.39, -20048966.10), new DPoint(20026376.39, 20048966.10));
    }
    return !this.likeRadians && !this.likeWorldGeodeticSystem && pseudoMercatorPolygon.simpleInclude(this);
  }
 
  get w(): number {
    return this.x;
  }
 
  set w(x: number) {
    this.x = x;
  }
 
  get h(): number {
    return this.y;
  }
 
  set h(y: number) {
    this.y = y;
  }
 
  get area(): number {
    checkFunction('area')
      .checkArgument('this')
      .shouldBeMeters(this);
    return this.w * this.h;
  }
 
  get hip(): number {
    checkFunction('hip')
      .checkArgument('this')
      .shouldBeMeters(this);
    return Math.sqrt(this.w * this.w + this.h * this.h);
  }
 
  get min(): number {
    return Math.min(this.x, this.y);
  }
 
  get max(): number {
    return Math.max(this.x, this.y);
  }
 
  get hipPoint(): DPoint {
    const {hip} = this;
    return new DPoint(hip, hip);
  }
 
  get xPoint(): DPoint {
    const {x} = this;
    return new DPoint(x, x);
  }
 
  get yPoint(): DPoint {
    const {y} = this;
    return new DPoint(y, y);
  }
 
  get wPoint(): DPoint {
    return this.xPoint;
  }
 
  get hPoint(): DPoint {
    return this.yPoint;
  }
 
  get lat(): number {
    return this.y;
  }
 
  set lat(v: number) {
    this.y = v;
  }
 
  get lng(): number {
    return this.x;
  }
 
  set lng(v: number) {
    this.x = v;
  }
 
  get lon(): number {
    return this.x;
  }
 
  set lon(v: number) {
    this.x = v;
  }
 
  get alt(): number | undefined {
    return this.z;
  }
 
  set alt(v: number | undefined) {
    this.z = v;
  }
 
  simple<T extends Record<string, number>>(xKey: string = 'x', yKey: string = 'y', zKey: string = 'z'): T {
    return {
      [xKey]: this.x,
      [yKey]: this.y,
      ...(isDefAndNotNull(this.z) ? {
        [zKey]: this.z
      } : {})
    } as T;
  }
 
  setIfLessThan(p: DPoint): DPoint {
    this.x = Math.max(this.x, p.x);
    this.y = Math.max(this.y, p.y);
    return this;
  }
 
  setIfMoreThan(p: DPoint): DPoint {
    this.x = Math.min(this.x, p.x);
    this.y = Math.min(this.y, p.y);
    return this;
  }
 
  minus(): DPoint {
    return this.scale(-1);
  }
 
  /**
   * Find [orthodromic path](https://en.wikipedia.org/wiki/Great-circle_navigation) between to points.
   *
   * @remark Points should be Lng/Lat.
   *
   * ![example](/media/examples/orthodromicPath.png)
   *
   * @param point
   * @param [pointsCount=360]
   */
  orthodromicPath(point: DPoint, pointsCount = 360): DPolygon {
    checkFunction('orthodromicPath')
      .checkArgument('this')
      .shouldBeDegree(this)
      .checkArgument('point')
      .shouldBeDegree(point);
    const t = this.clone().degreeToRadians();
    const p = point.clone().degreeToRadians();
    const d = Math.sin(p.x - t.x);
    const step = (p.x - t.x) / (pointsCount - 1);
    return new DPolygon(createArray(pointsCount)
      .map((v: number, i: number) => {
        const x = t.x + step * i;
        const y = Math.atan((Math.tan(t.y) * Math.sin(p.x - x)) / d +
          (Math.tan(p.y) * Math.sin(x - t.x)) / d);
        return new DPoint(x, y).radiansToDegrees();
      }));
  }
 
  sortByDistance(p: DPolygon): DPolygon {
    return p
      .clone()
      .map((d: DPoint, index: number) => {
        d.properties.distance = d.distance(this);
        d.properties.index = index;
        return d;
      })
      .sort((a: DPoint, b: DPoint) => a.properties.distance - b.properties.distance);
  }
 
  calculateAltitudeByDistanceBetweenPoints(p1: DPoint, p2: DPoint): DPoint {
    if (p1.alt === p2.alt) {
      this.alt = p1.alt;
    } else {
      const minAlt = Math.min(p1.alt ?? 0, p2.alt ?? 0);
      const maxAlt = Math.max(p1.alt ?? 0, p2.alt ?? 0);
      const dAlt = maxAlt - minAlt;
      const distance1 = this.distance(p1);
      const distance2 = this.distance(p2);
      const totalDistance = distance1 + distance2;
      if ((p1.alt ?? 0) === minAlt) {
        this.alt = minAlt + distance1 / totalDistance * dAlt;
      } else {
        this.alt = minAlt + distance2 / totalDistance * dAlt;
      }
    }
    return this;
  }
}