首页 > 其他分享 >Cesium计算多边形面积(十一)

Cesium计算多边形面积(十一)

时间:2023-01-26 17:11:49浏览次数:41  
标签:十一 多边形 Cartesian3 geometry let new Cesium console

 

      //计算三角形面积
      function triangleArea(p0, p1, p2) {
        let v0 = Cesium.Cartesian3.subtract(p0, p1, new Cesium.Cartesian3())      
        let v1 = Cesium.Cartesian3.subtract(p2, p1, new Cesium.Cartesian3())
        let cross = Cesium.Cartesian3.cross(v0, v1,v0);
        return Cesium.Cartesian3.magnitude(cross) * 0.5
      }

      //计算多边形面积
      function area(positions) {
        let result = 0
        if (!Array.isArray(positions)) {
          console.log('不是数组');          
          return result
        }

        if (!(positions[0] instanceof Cesium.Cartesian3)) {
          console.log('不是Cartesian3');
          //转换为Cartesian3数组
          positions = Transform.transformWGS84ArrayToCartesianArray(positions);
        }

        //创建一个geometry
        let geometry = Cesium.CoplanarPolygonGeometry.createGeometry(
          Cesium.CoplanarPolygonGeometry.fromPositions({
            positions: positions,
            vertexFormat: Cesium.VertexFormat.POSITION_ONLY
          })
        )

        if (!geometry) {
          console.log('不是geometry');
          return result
        }

        //顶点坐标数组p;
        let flatPositions = geometry.attributes.position.values
        console.log(flatPositions);

        //确定geometry的基本体的索引,也就是组成其的三角形的索引,每三个为一组,组成一个三角形
        let indices = geometry.indices
        console.log(indices);
        

        //计算三角形面积,最后加在一起
        for (let i = 0; i < indices.length; i += 3) {
          let p0 = Cesium.Cartesian3.unpack(
            flatPositions,
            indices[i] * 3,
            new Cesium.Cartesian3()
          )
          console.log(p0);
          
          let p1 = Cesium.Cartesian3.unpack(
            flatPositions,
            indices[i + 1] * 3,
            new Cesium.Cartesian3()
          )
          console.log(p1);
          let p2 = Cesium.Cartesian3.unpack(
            flatPositions,
            indices[i + 2] * 3,
            new Cesium.Cartesian3()
          )
          console.log(p2);
          result += triangleArea(p0, p1, p2)
        }
        return result
      }

      //一个正方形的四个端点
      var p0 = new Cesium.Cartesian3(0,0,0);
      var p1 = new Cesium.Cartesian3(1,0,0);
      var p2 = new Cesium.Cartesian3(1,1,0);
      var p3 = new Cesium.Cartesian3(0,1,0);

      var input = [p0,p1,p2,p3];
      var output = area(input);
      console.log(output);
  

顶点坐标数组flatPositions:

 

 

 

确定geometry的基本体的索引,也就是组成其的三角形的索引,每三个为一组,组成一个三角形indices:

 

 也就是顶点2、3、0组成一个三角形,0,1,2组成一个三角形

 

至于代码中为了要乘3,是为了保证每次取的数组是连续的,比如2、3、0乘3是6、9、0,从6开始取三个6,7,8,从9开始取三个9,10,11,从0开始取三个0,1,2,这样取到的三个点才是正确的三角形

 

用到的一些方法:

Cesium.CoplanarPolygonGeometry.createGeometry 创建geometry Cesium.Cartesian3.unpack 从数组中生成Cartesian3 Cesium.Cartesian3.subtract 相减,用于生成向量 Cesium.Cartesian3.cross 求叉积 Cesium.Cartesian3.magnitude 求向量的长度 Array.isArray 判断是否是数组

 

 

 

标签:十一,多边形,Cartesian3,geometry,let,new,Cesium,console
From: https://www.cnblogs.com/LJXXXX/p/17067917.html

相关文章