UNPKG

@itwin/core-react

Version:

A react component library of iTwin.js UI general purpose components

68 lines 2.29 kB
/*--------------------------------------------------------------------------------------------- * Copyright (c) Bentley Systems, Incorporated. All rights reserved. * See LICENSE.md in the project root for license terms and full copyright notice. *--------------------------------------------------------------------------------------------*/ /** @packageDocumentation * @module Utilities */ /** Describes and provides methods to work with 2d points. * @internal */ export class Point { x; y; /** Creates point from PointProps */ static create(pointProps) { return new Point(pointProps.x, pointProps.y); } /** Creates a new point. */ constructor(x = 0, y = 0) { this.x = x; this.y = y; } /** Calculates Euclidean distance to other point. */ getDistanceTo(other) { const offset = this.getOffsetTo(other); return Math.sqrt(Math.pow(offset.x, 2) + Math.pow(offset.y, 2)); } /** Gets offset to other point. */ getOffsetTo(other) { return new Point(other.x - this.x, other.y - this.y); } /** @returns New [[Point]] that is offset along the X and Y axes. */ offset(offset) { return new Point(this.x + offset.x, this.y + offset.y); } /** @returns New [[Point]] that is offset along the X axis. */ offsetX(offset) { return new Point(this.x + offset, this.y); } /** @returns New [[Point]] that is offset along the Y axis. */ offsetY(offset) { return new Point(this.x, this.y + offset); } /** @returns True if position of this and other points are equal. */ equals(other) { return other.x === this.x && other.y === this.y; } /** @returns New [[Point]] with modified x value. */ setX(x) { return new Point(x, this.y); } /** @returns New [[Point]] with modified y value. */ setY(y) { return new Point(this.x, y); } /** @returns New [[Point]] with coordinates multiplied by specified factor. */ multiply(factor) { return new Point(this.x * factor, this.y * factor); } /** @returns PointProps object for this point. */ toProps() { return { x: this.x, y: this.y, }; } } //# sourceMappingURL=Point.js.map