Search code examples
carrayspolygonscanfarea

Issues calculating the area of a polygon in C


Attempting to calculate the area of polygon that takes (x,y) coordinates up to a possible 100 points from .txt file using redirection e.g. ./program < file.txt
I'm having trouble scanning in the input so that my function will calculate the area.
The input is:

3 12867 1.0 2.0  1.0 5.0  4.0 5.0

Where 3 is the npoints and 12867 is the identification number.
This is my code I have produced so far:

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

I happen to be a newbie to coding so anything past using arrays and structs I won't really understand, but any help is still appreciated!


Solution

  • You are already looping for reading the vertices. First read all the vertices, and then calculate the area. That is to say, you should not call polygon_area in a loop. Here is a code piece with the fixes.

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