在 C 中计算多边形面积的问题

Issues calculating the area of a polygon in C

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

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;
}

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

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

也许这就是您要找的:

#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]);
    }
}

您已经在循环读取顶点。先读取所有的顶点,然后计算面积。也就是说,你不应该在循环中调用 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;
}

既然你不会 "require to use 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