find the area of a segment of a circle in javascript

To find the area of a segment of a circle, we can use the following formula:

Segment area formula

where r is the radius of the circle and θ is the central angle of the segment in radians.

Here's the JavaScript code to calculate the area of a segment of a circle:

index.tsx
function segmentArea(r, theta) {
  // Convert angle from degrees to radians
  var radians = theta * Math.PI / 180;

  // Calculate segment area
  var area = 0.5 * r * r * (radians - Math.sin(radians));

  return area;
}
220 chars
10 lines

You can call this function with the radius r and central angle theta (in degrees) of the segment as arguments:

index.tsx
var radius = 5;
var centralAngle = 60; // degrees

var area = segmentArea(radius, centralAngle);

console.log(area); // Output: 5.245
134 chars
7 lines

This will output the area of the segment rounded to three decimal places.

gistlibby LogSnag