简体   繁体   English

使用JavaScript计算不规则多边形的面积

[英]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%). 作业已上交(完成约80%)。 I still want to know how to implement this, though. 不过,我仍然想知道如何实现这一点。 I am stuck on line 84-86. 我被困在84-86行。 Here is the prompt. 这是提示。

Prompt: Calculate the area of irregularly shaped polygons using JS 提示:使用JS计算不规则形状的多边形的面积

INPUT: a nested array of 3-6 coordinates represented as [x,y] (clockwise order) 输入:3-6个坐标的嵌套数组,表示为[x,y](顺时针方向)
OUTPUT: area calculated to 2 significant figures 输出:面积计算为2位有效数字

Pseudocode: 伪代码:

  1. loop the input and check for error cases: 循环输入并检查错误情况:
    a. 一种。 array length < 3 or array length > 6 数组长度<3或数组长度> 6
    b. b。 array is not empty 数组不为空
    c. C。 numbers within -10 and 10 range -10和10范围内的数字

  2. loop to each inner array, and multiply x and y in the formula below: 循环到每个内部数组,并在以下公式中将x和y相乘:

     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 例如: area = -70 -(-56) = 57/2 = 7

  4. return area.toPrecision(2) to have one sig fig 返回area.toPrecision(2)有一个信号图

     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. 如果您仅使用Simpson规则来计算面积,则以下函数即可完成工作。 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 ). 该函数使用单个值数组,假设它们是成对的(偶数索引为x ,奇数为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). 不是多边形,而是Z形线,即使转换为单位,多边形也不能解析为“ 7英亩”,除非单位不是标准单位(例如1 = 184英尺)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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