From 5d90eded57df8867ea882fc994f53d4fc6ea6177 Mon Sep 17 00:00:00 2001 From: Ehsan Samavati Date: Sat, 13 Apr 2024 11:01:41 +0300 Subject: [PATCH] feat(core): add Point type and distance calculation function Added the Point type, leveraging the Vector2 structure, to represent 2D points in the swiperia-core package. Also implemented a function to calculate the Euclidean distance between two points, complete with unit tests to validate functionality. --- .../swiperia-core/src/lib/point/Point.type.ts | 6 +++++ .../src/lib/point/distance.spec.ts | 22 +++++++++++++++++++ .../swiperia-core/src/lib/point/distance.ts | 16 ++++++++++++++ packages/swiperia-core/src/lib/point/index.ts | 2 ++ 4 files changed, 46 insertions(+) create mode 100644 packages/swiperia-core/src/lib/point/Point.type.ts create mode 100644 packages/swiperia-core/src/lib/point/distance.spec.ts create mode 100644 packages/swiperia-core/src/lib/point/distance.ts create mode 100644 packages/swiperia-core/src/lib/point/index.ts diff --git a/packages/swiperia-core/src/lib/point/Point.type.ts b/packages/swiperia-core/src/lib/point/Point.type.ts new file mode 100644 index 0000000..246a3db --- /dev/null +++ b/packages/swiperia-core/src/lib/point/Point.type.ts @@ -0,0 +1,6 @@ +import type { Vector2 } from "../vector2/Vector2.type"; + +/** + * Represents a 2D point or coordinate. + */ +export type Point = Vector2; diff --git a/packages/swiperia-core/src/lib/point/distance.spec.ts b/packages/swiperia-core/src/lib/point/distance.spec.ts new file mode 100644 index 0000000..59d5c7a --- /dev/null +++ b/packages/swiperia-core/src/lib/point/distance.spec.ts @@ -0,0 +1,22 @@ +import { Point } from './Point.type'; +import { distance } from './distance'; + +describe('distance', () => { + it('should calculate the distance between two points', () => { + const a: Point = [0, 0]; + const b: Point = [3, 4]; + expect(distance(a, b)).toEqual(5); + }); + + it('should handle negative coordinates', () => { + const a: Point = [-2, 3]; + const b: Point = [1, -1]; + expect(distance(a, b)).toEqual(5); + }); + + it('should return 0 for the same point', () => { + const a: Point = [5, 5]; + const b: Point = [5, 5]; + expect(distance(a, b)).toEqual(0); + }); +}); diff --git a/packages/swiperia-core/src/lib/point/distance.ts b/packages/swiperia-core/src/lib/point/distance.ts new file mode 100644 index 0000000..a79a680 --- /dev/null +++ b/packages/swiperia-core/src/lib/point/distance.ts @@ -0,0 +1,16 @@ +import type { Point } from './Point.type'; + +/** + * Calculates the Euclidean distance between two points. + * + * @param a - The first point. + * @param b - The second point. + * @returns The Euclidean distance between the two points. + */ +export const distance = (a: Point, b: Point): number => { + const [ax, ay] = a; + const [bx, by] = b; + const dx = ax - bx; + const dy = ay - by; + return Math.sqrt(dx * dx + dy * dy); +}; diff --git a/packages/swiperia-core/src/lib/point/index.ts b/packages/swiperia-core/src/lib/point/index.ts new file mode 100644 index 0000000..a31d707 --- /dev/null +++ b/packages/swiperia-core/src/lib/point/index.ts @@ -0,0 +1,2 @@ +export type { Point } from './Point.type'; +export { distance } from './distance';