• 求任意多边形的面积


    输入一个点列,顺序连接成一个封闭多边形,计算多边形的面积。

    思路:求多边形的面积可以使用叉乘求面积。设多边形有n个顶点,用数组将其存起来,其余各点均与第一个点连起来,这样从逆时针方向来看,每两个点构成一个三角形,在使用叉乘来求的面积。有一点需要明确一下,每一次使用叉乘求面积,不能加绝对值,因为每一步叉乘求面积求得的是有向面积,将所有的有向面积加和所求的就是该多边形的面积,这种方法不仅适用于凸多边形,同时也适用于凹多边形。

    (由于输入点的顺序是 “ 逆时针 ”,所以如果是顺时针输入的话,就会出现负数的情况,所以在优化的时候,给结果加入了一个绝对值即可)

     1 #include <cstdio>
     2 #include <cstring>
     3 #include <cmath>
     4 #include <algorithm>
     5 using namespace std;
     6 
     7 const int maxn = 105;
     8 
     9 struct node{
    10     int x, y;
    11 }point[maxn];
    12 
    13 int main(){
    14     int t;
    15     scanf("%d", &t);
    16     while (t--){
    17         int n;
    18         scanf("%d", &n);
    19         for (int i = 0; i < n; i++){
    20             scanf("%d %d", &point[i].x, &point[i].y);
    21         }
    22         int i = 1;
    23         double ans = 0;
    24         double cx = 0;
    25         while (i < n - 1){
    26             double x1 = point[i].x - point[0].x;
    27             double y1 = point[i].y - point[0].y;
    28             double x2 = point[i + 1].x - point[0].x;
    29             double y2 = point[i + 1].y - point[0].y;
    30             i++;
    31             cx = x1*y2 - x2*y1;
    32             ans += cx / 2;
    33         }
    34         ans = fabs(ans);
    35         printf("%.1lf
    ", ans);
    36     }
    37     system("pause");
    38     return 0;
    39 }
  • 相关阅读:
    data object audit
    trigger dependencies
    redo allocation latch redo copy latch
    查看TEMP 表空间usage
    oracle 查看隐藏参数
    weblogic 10 无密码启动
    lead 函数和 lag函数
    oracle latch
    查看OS 各项参数
    深度学习小贴士
  • 原文地址:https://www.cnblogs.com/ouyang_wsgwz/p/7598697.html
Copyright © 2020-2023  润新知