【问题标题】:Issues calculating the area of a polygon in C在 C 中计算多边形面积的问题
【发布时间】:2015-06-29 17:35:24
【问题描述】:

尝试使用重定向从 .txt 文件中计算取 (x,y) 坐标最多 100 个点的多边形面积,例如./program 我在扫描输入时遇到问题,因此我的函数将计算面积。
输入是:

3 12867 1.0 2.0  1.0 5.0  4.0 5.0

其中 3 是 npoints,12867 是标识号。
这是我到目前为止生成的代码:

#include <stdio.h>
#include <stdlib.h>

#define MAX_PTS 100
#define MAX_POLYS 100
#define END_INPUT 0

// function to calculate the area of a polygon
// think it's correct
double polygon_area(int MAX_PTS, double x[], double y[])
{
  printf("In polygon.area\n");
  double area = 0.0;
  for (int i = 0; i < MAX_PTS; ++i)
  {
     int j = (i + 1)%MAX_PTS;
     area += 0.5 * (x[i]*y[j] -  x[j]*y[i]);
  }

  printf("The area of the polygon is %lf  \n", area);

  return (area);
}

// having trouble reading in values from a txt file into an array

int main(int argc, char *argv[]) {
  int npoints, poly_id;
  double // something should go here 

  if(scanf("%d %d", &npoints, &poly_id)) {
    int iteration = 0;
    struct Point initialPoint = a;
    double area = 0;  
    scanf("%lf %lf", &, &); 
    // keep getting errors with what goes next to the & 

    for (iteration = 1; iteration < npoints; ++iteration) {
        scanf("%lf %lf", &, &); 
        // keep getting errors with what goes next to the & 
        area += polygon_area(); // unsure what to do here

    }
    // now complete the polygon with last-edge joining the last-point
    // with initial-point.
    area += polygon_area(a, initialPoint);

    printf("First polygon is %d\n", poly_id);
    printf("area = %2.2lf m^2\n", area); 
  }
  return 0;
}

我碰巧是编码新手,所以过去使用数组和结构的任何东西我都不会真正理解,但仍然感谢任何帮助!

【问题讨论】:

  • 请详细介绍 npoint 和标识号。它的用途是什么? (x,y) 坐标在哪里?
  • 我怀疑scanf("%lf %lf", &amp;, &amp;); 会编译。这至少应该是 &amp;a.x, &amp;a.y 或与 Point 的定义兼容的东西。
  • 您不能将MAX_PTS 用作#define 和参数名称。它不起作用,因为编译器会将您的变量名视为 100,这是一个非法的变量名。
  • "keep getting errors with what goes next to the &amp;":嗯,这很有意义,考虑到您的代码中&amp; 旁边没有任何内容!!!
  • 只是一个提示,如果您没有使用 C,请使用其他东西。例如,Python 对于这样的简单任务要容易得多。

标签: c arrays polygon scanf area


【解决方案1】:

由于您不“要求使用 C”,因此这是我对使用 C++(和 Boost)的看法。

请注意,它还有许多其他功能,并更正输入以遵守所需的不变量。

Live On Coliru

#include <boost/geometry/algorithms/area.hpp>
#include <boost/geometry/algorithms/correct.hpp>
#include <boost/geometry/io/io.hpp>
#include <boost/geometry/geometries/point_xy.hpp>
#include <boost/geometry/geometries/polygon.hpp>
#include <boost/geometry/geometry.hpp>
#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/qi_match.hpp>

namespace qi = boost::spirit::qi;
namespace bg = boost::geometry;

using Point   = bg::model::d2::point_xy<double, bg::cs::cartesian>;
using Polygon = bg::model::polygon<Point>;

namespace boost { namespace spirit { namespace traits {
    template <>
    struct assign_to_attribute_from_value<Point, fusion::vector2<double, double> > {
        static void call(fusion::vector2<double, double> const& t, Point& attr) {
            attr = Point(fusion::at_c<0>(t), fusion::at_c<1>(t));
        }
    };
} } }

int main() {
    Polygon poly;

    int npoints, poly_id;

    if (
            (std::cin >> npoints >> poly_id) &&
            (std::cin >> std::noskipws >> qi::phrase_match(qi::repeat(npoints) [qi::attr_cast(qi::double_ >> qi::double_)], qi::blank, poly.outer()))
       )
    {
        bg::correct(poly);
        std::cout << "Polygon: " << bg::wkt(poly)  << "\n";
        std::cout << "Area: "    << bg::area(poly) << "\n";
    }
    else
        std::cout << "Parse failed\n";
}

对于给定的输入,它会打印:

Polygon: POLYGON((1 2,1 5,4 5,1 2))
Area: 4.5

【讨论】:

    【解决方案2】:

    您已经在循环读取顶点。首先读取所有顶点,然后计算面积。也就是说,你不应该在循环中调用polygon_area。这是包含修复的代码片段。

    #include <stdio.h>
    #include <stdlib.h>
    
    #define MAX_PTS 100
    #define MAX_POLYS 100
    #define END_INPUT 0
    
    // function to calculate the area of a polygon
    // think it's correct
    double polygon_area(int length, double x[], double y[])
    {
        double area = 0.0;
        int i;
        printf("In polygon.area\n");
        for (i = 0; i < length; ++i)
        {
            int j = (i + 1) % length;
            area += (x[i] * y[j] - x[j] * y[i]);
        }
        area = area / 2;
        area = (area > 0 ? area : -1 * area);
        printf("The area of the polygon is %lf  \n", area);
    
        return (area);
    }
    
    // having trouble reading in values from a txt file into an array
    
    int main(int argc, char *argv[]) {
        int npoints, poly_id;
        double x[MAX_PTS], y[MAX_PTS];
        int iteration = 0;
        double area = 0;
    
        scanf("%d %d", &npoints, &poly_id);
    
        for (iteration = 0; iteration < npoints; ++iteration) {
            scanf("%lf %lf", &(x[iteration]), &(y[iteration]));
        }
        area = polygon_area(npoints, x, y); // unsure what to do here
    
        printf("First polygon is %d\n", poly_id);
        printf("area = %2.2lf m^2\n", area);
    
        return 0;
    }
    

    【讨论】:

      【解决方案3】:

      编辑: 对不起,我在这里使用命令行参数,而不是你想要的重定向。你仍然可以像这样使用它:./program $(cat test.txt) 在 bash 语法中。

      也许这就是你要找的东西:

      #include <stdio.h>
      #include <stdlib.h>
      
      int main(int argc, char *argv[]) {
          int i;
          //argc contains the number of parameters.
          //argv[0] is the filename of the executable
          //argv[1] has your npoints
          int npoints = atoi(argv[1]);
          //argv[2] has your poly_id
          int poly_id = atoi(argv[2]);
          double *x;
          double *y;
          x = malloc(sizeof(double)*npoints); //allocate space
          y = malloc(sizeof(double)*npoints);
      
          //convert the command line parameters
          for (i=0;i<npoints;i++) {
              x[i] = atof(argv[i*2+3]);
              y[i] = atof(argv[i*2+4]);
          }
      
          //print them again, remove this and do your calculations here
          for (i=0;i<npoints;i++) {
              printf("%f %f\n", x[i], y[i]);
          }
      }
      

      【讨论】:

        猜你喜欢
        • 2013-11-13
        • 1970-01-01
        • 2011-01-03
        • 2013-10-24
        • 2013-09-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-04-07
        相关资源
        最近更新 更多