find the area of a regular polygon circumscribed around a circle in typescript

To find the area of a regular polygon circumscribed around a circle, we can use the following formula:

A = (1/2) * n * s * r

where n is the number of sides, s is the length of each side, and r is the radius of the circumscribed circle.

In TypeScript, we can write a function that takes in the number of sides and the radius of the circumscribed circle, and returns the area of the polygon:

index.ts
function areaOfCircumscribedPolygon(n: number, r: number): number {
  const s = 2 * r * Math.sin(Math.PI / n);  // length of each side
  const A = (1/2) * n * s * r;             // area of the polygon
  return A;
}
215 chars
6 lines

We can then call this function to find the area of a regular polygon with, for example, 8 sides and a circumscribed circle of radius 5:

index.ts
const n = 8;
const r = 5;
const area = areaOfCircumscribedPolygon(n, r);  // 53.03804416204773
95 chars
4 lines

So the area of the polygon is approximately 53.04 square units.

gistlibby LogSnag