-3

Situation

I have a circle segment and some information about the circle it belongs to.

Given Information:

  • coordinates of the center of the circle (latitude / longitude)
  • radius of the circle in meters
  • central angle in degrees

I need to calculate the area of the circle segment in JavaScript. But it should also depend on the radius of the Earth (like great-circle distance).

Problem

I have no idea how to do that and couldn't found any algorithms.

Thanks for help

Max Mikhalchuk
  • 147
  • 1
  • 1
  • 10

1 Answers1

1

There is an excellent tutorial on this problem on geeksforgeeks.org.

I have translated their code for JavaScript, since they didn't have it in that language and you tagged it in your question:

//Code ported from: https://www.geeksforgeeks.org/program-find-area-circular-segment/
function AreaOfSegment(radius, angle) {
    // Calculating area of sector 
    var areaOfSector = Math.PI * (radius * radius) *
        (angle / 360);
    // Calculating area of triangle 
    var areaOfTriangle = 0.5 * (radius * radius)
        * Math.sin((angle * Math.PI) / 180);
    return areaOfSector - areaOfTriangle;
}
//TEST
var radiusLabel = document.body.appendChild(document.createElement("label"));
radiusLabel.textContent = "Radius: ";
var radiusInput = radiusLabel.appendChild(document.createElement("input"));
radiusInput.type = "number";
radiusInput.step = "any";
radiusInput.min = "0";
radiusInput.value = "6371008.0";
var angleLabel = document.body.appendChild(document.createElement("label"));
angleLabel.textContent = "Angle: ";
var angleInput = angleLabel.appendChild(document.createElement("input"));
angleInput.type = "number";
angleInput.step = "any";
angleInput.min = "0";
angleInput.max = "360";
angleInput.value = "90";
var output = document.body.appendChild(document.createElement("p"));
function compute() {
    var radius = parseFloat(radiusInput.value);
    var angle = parseFloat(angleInput.value);
    output.textContent = 'Area is: ' + AreaOfSegment(radius, angle).toString();
}
compute();
["change", "keyup", "mouseup"].forEach(function (evt) {
    angleInput.addEventListener(evt, compute);
    radiusInput.addEventListener(evt, compute);
});
Emil S. Jørgensen
  • 6,216
  • 1
  • 15
  • 28
  • Thanks, but it seems that this algorithm counts area of the flat surface instead of sphere (doesn't take Earth radius into account) – Max Mikhalchuk Jan 18 '19 at 10:50