From 109e4a685be2d29cd3ec532d241cb5f3ee0873bc Mon Sep 17 00:00:00 2001 From: Prashal Ruchiranga <98536996+prashalruchiranga@users.noreply.github.com> Date: Sat, 8 Oct 2022 12:26:06 +0530 Subject: [PATCH] algorithm: find length of an arc and area of the sector formed by an arc of a circle (#1119) * Add an algorithm to find length and area of an arc of a circle * Updated to follow Javascript Standard Style * Update CircularArc.js * Update CircularArc.js * Add tests * Followed Javascript standard style --- Maths/CircularArc.js | 31 +++++++++++++++++++++++++++++++ Maths/test/CircularArc.test.js | 19 +++++++++++++++++++ 2 files changed, 50 insertions(+) create mode 100644 Maths/CircularArc.js create mode 100644 Maths/test/CircularArc.test.js diff --git a/Maths/CircularArc.js b/Maths/CircularArc.js new file mode 100644 index 000000000..27d690bcf --- /dev/null +++ b/Maths/CircularArc.js @@ -0,0 +1,31 @@ +import { degreeToRadian } from './DegreeToRadian.js' + +/** + * @function circularArcLength + * @description calculate the length of a circular arc + * @param {Integer} radius + * @param {Integer} degrees + * @returns {Integer} radius * angle_in_radians + * @see https://en.wikipedia.org/wiki/Circular_arc + * @example circularArcLength(3, 45) = 2.356194490192345 + */ +function circularArcLength (radius, degrees) { + return radius * degreeToRadian(degrees) +} +/** + * @function circularArcArea + * @description calculate the area of the sector formed by an arc + * @param {Integer} radius + * @param {Integer} degrees + * @returns {Integer} 0.5 * r * r * angle_in_radians + * @see https://en.wikipedia.org/wiki/Circular_arc + * @example circularArcArea(3,45) = 3.5342917352885173 + */ +function circularArcArea (radius, degrees) { + return Math.pow(radius, 2) * degreeToRadian(degrees) / 2 +} + +export { + circularArcLength, + circularArcArea +} diff --git a/Maths/test/CircularArc.test.js b/Maths/test/CircularArc.test.js new file mode 100644 index 000000000..1819d30ee --- /dev/null +++ b/Maths/test/CircularArc.test.js @@ -0,0 +1,19 @@ +import { circularArcLength, circularArcArea } from '../CircularArc' + +describe('circularArcLength', () => { + it('with natural number', () => { + const arcLengthOfOneThirty = circularArcLength(1, 30) + const arcLengthOfThreeSixty = circularArcLength(3, 60) + expect(arcLengthOfOneThirty).toBe(0.5235987755982988) + expect(arcLengthOfThreeSixty).toBe(3.141592653589793) + }) +}) + +describe('circularArcArea', () => { + it('with natural number', () => { + const arcAreaOfOneThirty = circularArcArea(1, 30) + const arcAreaOfThreeSixty = circularArcArea(3, 60) + expect(arcAreaOfOneThirty).toBe(0.2617993877991494) + expect(arcAreaOfThreeSixty).toBe(4.71238898038469) + }) +})