mirror of
https://github.com/google/blockly.git
synced 2026-05-09 21:50:12 +02:00
b0475b0c68
* fix: Remove spurious blank lines Remove extraneous blank lines introduced by deletion of 'use strict'; pragmas. Also fix the location of the goog.declareModuleId call in core/utils/array.ts. * fix: Add missing double-blank-line before body of modules Our convention is to have two blank lines between the imports (or module ID, if there are no imports) and the beginning of the body of the module. Enforce this. * fix: one addition format error for PR #6243
125 lines
3.1 KiB
TypeScript
125 lines
3.1 KiB
TypeScript
/**
|
|
* @license
|
|
* Copyright 2019 Google LLC
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
*/
|
|
|
|
/**
|
|
* @fileoverview Utility methods for coordinate manipulation.
|
|
* These methods are not specific to Blockly, and could be factored out into
|
|
* a JavaScript framework such as Closure.
|
|
*/
|
|
|
|
/**
|
|
* Utility methods for coordinate manipulation.
|
|
* These methods are not specific to Blockly, and could be factored out into
|
|
* a JavaScript framework such as Closure.
|
|
* @class
|
|
*/
|
|
import * as goog from '../../closure/goog/goog.js';
|
|
goog.declareModuleId('Blockly.utils.Coordinate');
|
|
|
|
|
|
/**
|
|
* Class for representing coordinates and positions.
|
|
* @alias Blockly.utils.Coordinate
|
|
*/
|
|
export class Coordinate {
|
|
/**
|
|
* @param x Left.
|
|
* @param y Top.
|
|
*/
|
|
constructor(public x: number, public y: number) {}
|
|
|
|
/**
|
|
* Creates a new copy of this coordinate.
|
|
* @return A copy of this coordinate.
|
|
*/
|
|
clone(): Coordinate {
|
|
return new Coordinate(this.x, this.y);
|
|
}
|
|
|
|
/**
|
|
* Scales this coordinate by the given scale factor.
|
|
* @param s The scale factor to use for both x and y dimensions.
|
|
* @return This coordinate after scaling.
|
|
*/
|
|
scale(s: number): Coordinate {
|
|
this.x *= s;
|
|
this.y *= s;
|
|
return this;
|
|
}
|
|
|
|
/**
|
|
* Translates this coordinate by the given offsets.
|
|
* respectively.
|
|
* @param tx The value to translate x by.
|
|
* @param ty The value to translate y by.
|
|
* @return This coordinate after translating.
|
|
*/
|
|
translate(tx: number, ty: number): Coordinate {
|
|
this.x += tx;
|
|
this.y += ty;
|
|
return this;
|
|
}
|
|
|
|
/**
|
|
* Compares coordinates for equality.
|
|
* @param a A Coordinate.
|
|
* @param b A Coordinate.
|
|
* @return True iff the coordinates are equal, or if both are null.
|
|
*/
|
|
static equals(a: Coordinate|null, b: Coordinate|null): boolean {
|
|
if (a === b) {
|
|
return true;
|
|
}
|
|
if (!a || !b) {
|
|
return false;
|
|
}
|
|
return a.x === b.x && a.y === b.y;
|
|
}
|
|
|
|
/**
|
|
* Returns the distance between two coordinates.
|
|
* @param a A Coordinate.
|
|
* @param b A Coordinate.
|
|
* @return The distance between `a` and `b`.
|
|
*/
|
|
static distance(a: Coordinate, b: Coordinate): number {
|
|
const dx = a.x - b.x;
|
|
const dy = a.y - b.y;
|
|
return Math.sqrt(dx * dx + dy * dy);
|
|
}
|
|
|
|
/**
|
|
* Returns the magnitude of a coordinate.
|
|
* @param a A Coordinate.
|
|
* @return The distance between the origin and `a`.
|
|
*/
|
|
static magnitude(a: Coordinate): number {
|
|
return Math.sqrt(a.x * a.x + a.y * a.y);
|
|
}
|
|
|
|
/**
|
|
* Returns the difference between two coordinates as a new
|
|
* Coordinate.
|
|
* @param a An x/y coordinate.
|
|
* @param b An x/y coordinate.
|
|
* @return A Coordinate representing the difference between `a` and `b`.
|
|
*/
|
|
static difference(a: Coordinate|SVGPoint, b: Coordinate|SVGPoint):
|
|
Coordinate {
|
|
return new Coordinate(a.x - b.x, a.y - b.y);
|
|
}
|
|
|
|
/**
|
|
* Returns the sum of two coordinates as a new Coordinate.
|
|
* @param a An x/y coordinate.
|
|
* @param b An x/y coordinate.
|
|
* @return A Coordinate representing the sum of the two coordinates.
|
|
*/
|
|
static sum(a: Coordinate|SVGPoint, b: Coordinate|SVGPoint): Coordinate {
|
|
return new Coordinate(a.x + b.x, a.y + b.y);
|
|
}
|
|
}
|