| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374 |
- /**
- * This class is responsible for storing point in 2D.
- */
- export class point {
- #x;
- #y;
-
- /**
- * This create new point, can be setup by new position.
- *
- * @param {number} x - X position
- * @param {number} y - Y position
- */
- constructor(x = 0, y = 0) {
- this.#x = x;
- this.#y = y;
- }
- /**
- * @returns {number} - X value of point
- */
- get x() {
- return this.#x;
- }
- /**
- * @returns {number} - Y value of point
- */
- get y() {
- return this.#y;
- }
- /**
- * @param {number} target - New X value
- */
- set x(target) {
- this.#x = target;
- }
-
- /**
- * @param {number} target - New Y value
- */
- set y(target) {
- this.#y = target;
- }
- /**
- * This compare two points, and return compare result.
- *
- * @example (point(5, 4).compare(point(10, 10)) => point(5, 6))
- * @param {point} target - Point to calculate
- * @returns {point} - Compare result
- */
- compare(target) {
- return new point(
- target.x - this.x,
- target.y - this.y
- );
- }
- /**
- * This calculate distance between two points.
- *
- * @param {point} target - Second point to calculate distance between
- * @returns {number} - Distance between this point and given point
- */
- distance(target) {
- const compared = this.compare(target);
- const summary = Math.pow(compared.x, 2) + Math.pow(compared.y, 2);
- const result = Math.sqrt(summary);
- return result;
- }
- }
|