简体   繁体   中英

Calculating the area of an irregular polygon using JavaScript

I got this as an assignment, but I'm stuck on implementing the area portion. The assignment is already turned in (finished about 80%). I still want to know how to implement this, though. I am stuck on line 84-86. Here is the prompt.

Prompt: Calculate the area of irregularly shaped polygons using JS

INPUT: a nested array of 3-6 coordinates represented as [x,y] (clockwise order)
OUTPUT: area calculated to 2 significant figures

Pseudocode:

  1. loop the input and check for error cases:
    a. array length < 3 or array length > 6
    b. array is not empty
    c. numbers within -10 and 10 range

  2. loop to each inner array, and multiply x and y in the formula below:

     sum_x_to_y = (X0 *Y1) + (X1* Y2)...X(n-1)* Yn sum_y_to_x = (Y0 * X1) + (Y1-X2)...Y(n-1)* Xn 

    ex:

     (0, -10) (7,-10) (0,-8) (0,-10) | x | y | | 0 |-10 | | 7 |-10 | | 0 |-8 | | 0 |-10 | sum_x_to_y = 0*-10 + 7*-8 + 0*-10 = -56 sum_y_to_x = -10*7 + -10*0 + -8*0 = -70 
  3. area = (sum_y_to_x - sum_x_to_y) / (2.00)

    ex: area = -70 -(-56) = 57/2 = 7

  4. return area.toPrecision(2) to have one sig fig

     function PaddockArea(array_coords) { var sum_x_to_y = 0; var sum_y_to_x = 0; var arr_size = array_coords.length; if (arr_size === 0) { //check for empty array console.log("Invalid input. Coordinates cannot be empty."); } if (arr_size < 3 || arr_size > 7) { //check input outside of 3-6 range console.log("Input out of range."); } for (var i = 0; i < arr_size; i++) { for (var j = 0; j < array_coords[i].length; j++) { //test for inner coordinates -10 to 10 range if (array_coords[i][j] < -10 || array_coords[i][j] > 10) { console.log("Coordinates outside of -10 to 10 range."); } // I NEED TO IMPLEMENT TO calc for AREA here sum_x_to_y += array_coords[i][j] * array_coords[j][i]; sum_y_to_x += array_coords[j][i] * array_coords[i][j]; var area = (sum_y_to_x - sum_x_to_y) / 2; console.log(area.toPrecision(2) + "acres"); } } } 

If you're just using Simpson's rule to calculate area, the following function will do the job. Just make sure the polygon is closed. If not, just repeat the first coordinate pair at the end.

This function uses a single array of values, assuming they are in pairs (even indexes are x , odd are y ). It can be converted to using an array of arrays containing coordinate pairs.

The function doesn't do any out of bounds or other tests on the input values.

function areaFromCoords(coordArray) {

    var x = coordArray,
        a = 0;

    // Must have even number of elements
    if (x.length % 2) return;

    // Process pairs, increment by 2 and stop at length - 2
    for (var i=0, iLen=x.length-2; i<iLen; i+=2) {
       a += x[i]*x[i+3] - x[i+2]*x[i+1];
    }
    return Math.abs(a/2);
}

console.log('Area: ' + areaFromCoords([1,1,3,1,3,3,1,3,1,1])); // 4

console.log('Area: ' + areaFromCoords([0,-10, 7,-10, 0,-8, 0,-10,])); // 7

Because you haven't posted actual code, I haven't input any of your examples. The sequence:

[[1,0],[1,1],[0,0],[0,1]]

is not a polygon, it's a Z shaped line, and even if converted to a unit polygon can't resolve to "7 acres" unless the units are not standard (eg 1 = 184 feet approximately).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM