-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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.
- Loading branch information
Showing
4 changed files
with
46 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import type { Vector2 } from "../vector2/Vector2.type"; | ||
|
||
/** | ||
* Represents a 2D point or coordinate. | ||
*/ | ||
export type Point = Vector2; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export type { Point } from './Point.type'; | ||
export { distance } from './distance'; |