尝试使用重定向来计算从(。,x)坐标到.txt文件的可能100点的多边形区域,例如./program< 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;
}
我碰巧是编码的新手,所以过去使用数组和结构的东西我都不会真正理解,但任何帮助仍然受到赞赏!
答案 0 :(得分:3)
由于您不需要使用C&#34;,我在此处使用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
答案 1 :(得分:0)
编辑:对不起,我在这里使用命令行参数,而不是你想要的重定向。你仍然可以像使用bash语法中的./program $(cat test.txt)
一样使用它。
也许这就是你要找的东西:
#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]);
}
}
答案 2 :(得分:0)
您已经循环读取顶点。首先读取所有顶点,然后计算面积。也就是说,你不应该在循环中调用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;
}