简体   繁体   English

在d3中计算多边形的面积

[英]Calculate area of polygon in d3

I cannot calculate the area of a polygon in D3 using the function path.area() 我无法使用函数path.area()计算D3中多边形的面积

I have tried feeding it a list of coordinates as follows: 我试过给它一个坐标列表如下:

var d = [
      [-1, 415.44],
      [146.93, 304.47],
      [195.45, 152.13],
      [-1, 134.64]
    ]

path.area(d)

I have also tried to feed it (what I think is) a TopoJSON object, as follows: 我也尝试过它(我认为是)一个TopoJSON对象,如下所示:

path.area({        
        "type": "Feature",
        "geometry": {
            "type": "Polygon", 
            "coordinates": d},            
        });

The first attempt gives you value 0 . 第一次尝试给你的值为0 The second gives you 'NaN' . 第二个给你'NaN' Do you guys know what I am doing wrong? 你们知道我做错了吗? As an FYI, I need to calculate the area of a polygon that I reference as follows: 作为一个FYI,我需要计算我引用的多边形的面积,如下所示:

d3.select("#IDofpolygon");

You have to use d3.polygonArea , which: 你必须使用d3.polygonArea ,它:

Returns the signed area of the specified polygon. 返回指定多边形的带符号区域。 If the vertices of the polygon are in counterclockwise order (assuming a coordinate system where the origin ⟨0,0⟩ is in the top-left corner), the returned area is positive; 如果多边形的顶点是逆时针顺序(假设原点⟨0,0⟩在左上角的坐标系),则返回的区域为正; otherwise it is negative, or zero. 否则为负,或为零。

Here is the demo: 这是演示:

 var d = [ [-1, 415.44], [146.93, 304.47], [195.45, 152.13], [-1, 134.64] ]; var area = d3.polygonArea(d); console.log(area) 
 <script src="https://d3js.org/d3.v4.min.js"></script> 

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

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