-
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.
Update package version and add math utility functions
- Loading branch information
Showing
21 changed files
with
403 additions
and
2 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
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
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
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,15 @@ | ||
/** | ||
* Calculates the area of a circle. | ||
* @param radius - The radius of the circle. | ||
* @returns The area of the circle. | ||
*/ | ||
export function areaCircle(radius: number): number { | ||
if (radius === 0) { | ||
return 0; | ||
} | ||
|
||
if (radius < 0) { | ||
return NaN; | ||
} | ||
return Math.PI * radius ** 2; | ||
} |
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,18 @@ | ||
/** | ||
* Calculates the area of a rectangle. | ||
* | ||
* @param length - The length of the rectangle. | ||
* @param width - The width of the rectangle. | ||
* @returns The area of the rectangle. | ||
*/ | ||
export function areaRectangle(length: number, width: number): number { | ||
if (length === 0 || width === 0) { | ||
return 0; | ||
} | ||
|
||
if (length < 0 || width < 0) { | ||
return NaN; | ||
} | ||
|
||
return length * width; | ||
} |
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,10 @@ | ||
/** | ||
* Calculates the area of a triangle. | ||
* | ||
* @param base - The length of the base of the triangle. | ||
* @param height - The height of the triangle. | ||
* @returns The area of the triangle. | ||
*/ | ||
export function areaTriangle(base: number, height: number): number { | ||
return 0.5 * base * height; | ||
} |
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,8 @@ | ||
/** | ||
* Calculates the circumference of a circle given the radius. | ||
* @param radius - The radius of the circle. | ||
* @returns The circumference of the circle. | ||
*/ | ||
export function circumference(radius: number): number { | ||
return 2 * Math.PI * radius; | ||
} |
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
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,10 @@ | ||
/** | ||
* Linearly interpolates between two numbers. | ||
* @param from - The starting value. | ||
* @param to - The ending value. | ||
* @param t - The interpolation factor (between 0 and 1). | ||
* @returns The interpolated value. | ||
*/ | ||
export function lerp(from: number, to: number, t: number): number { | ||
return from + (to - from) * t; | ||
} |
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,10 @@ | ||
/** | ||
* Calculates the ratio between two numbers. | ||
* | ||
* @param a - The numerator. | ||
* @param b - The denominator. | ||
* @returns The ratio of a to b. | ||
*/ | ||
export function ratio(a: number, b: number): number { | ||
return a / b; | ||
} |
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,8 @@ | ||
/** | ||
* Calculates the volume of a sphere given its radius. | ||
* @param radius The radius of the sphere. | ||
* @returns The volume of the sphere. | ||
*/ | ||
export function volumeSphere(radius: number): number { | ||
return (4 / 3) * Math.PI * radius ** 3; | ||
} |
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
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,10 @@ | ||
/** | ||
* Pads a number with leading zeros to a specified length. | ||
* | ||
* @param num - The number to pad. | ||
* @param length - The desired length of the padded number. | ||
* @returns The padded number as a string. | ||
*/ | ||
export function padNumber(num: number, length: number): string { | ||
return num.toString().padStart(length, "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,24 @@ | ||
import { expect, describe, it } from "bun:test"; | ||
import { areaCircle } from "../../src/math/areaCircle"; | ||
|
||
describe("areaCircle", () => { | ||
it("should calculate the area of a circle with a given radius", () => { | ||
const radius = 5; | ||
const expectedArea = Math.PI * radius ** 2; | ||
const calculatedArea = areaCircle(radius); | ||
expect(calculatedArea).toBe(expectedArea); | ||
}); | ||
|
||
it("should return 0 when the radius is 0", () => { | ||
const radius = 0; | ||
const expectedArea = 0; | ||
const calculatedArea = areaCircle(radius); | ||
expect(calculatedArea).toBe(expectedArea); | ||
}); | ||
|
||
it("should return NaN when the radius is negative", () => { | ||
const radius = -5; | ||
const calculatedArea = areaCircle(radius); | ||
expect(calculatedArea).toBeNaN(); | ||
}); | ||
}); |
Oops, something went wrong.