Thread: Help with finding the area of a polygon.

  1. #1
    Registered User
    Join Date
    Mar 2012
    Posts
    38

    Help with finding the area of a polygon.

    This program takes in coordinates and then outputs the area, but there seems to be a logic error somewhere. The file it is reading from has this in it.
    4
    1.0 0.0
    13.2 1.25
    20.5 18.4
    16.37 24.54

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    int main(int argc, char **argv[]) 
    {
        int size = 0;
        char fileName[] = "pointData.txt";
        double sum=0;
        int i;
        FILE *instream=NULL;
        instream = fopen(fileName, "r");
        fscanf(instream,"%d",&size);
        double x[size];
        double y[size];
        for(i=0; i<size; i++){
            fscanf(instream,"%lf",&x[i]);
            fscanf(instream,"%lf",&y[i]);}
        fclose(instream);
        for(i=0; i<size-1; i++){
            sum+=(((x[i])*(y[i+1]))-((x[i+1])*(y[i])));}
        sum/=2.0;
        printf("Area of the polygon is: %.4f\n", sum);
      return 0;
    }
    It outputs 210.1835, it should be 197.9135

  2. #2
    Registered User
    Join Date
    Mar 2010
    Posts
    583
    You're missing the last term in the sum. For a 4 sided polygon you should be summing:

    x[0]y[1] - y[0]x[1]
    x[1]y[2] - y[1]x[2]
    x[2]y[3] - y[2]x[3]
    x[3]y[0] - y[3]x[0]

    Your for loop takes care of the first 3, but not the last one. You just need another addition after the loop.

  3. #3
    Registered User
    Join Date
    Feb 2012
    Posts
    117
    Ok this is weird.

    I changed it to how I have it in here now and it works on gcc which is what it's graded on. No errors or anything. But on Dev-C It's not compiling. Hmmm

  4. #4
    Registered User
    Join Date
    Mar 2012
    Posts
    38
    Quote Originally Posted by smokeyangel View Post
    You're missing the last term in the sum.
    I tried adding one more and it gives me the same result.

    Also I am compiling with gcc too. I am not sure what is the problem.

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by mgracecar View Post
    But on Dev-C It's not compiling. Hmmm
    I hope someday they come out with compilers that tell us why things don't compile.


    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    Registered User
    Join Date
    Mar 2012
    Posts
    38
    Ok I understand why it was not working now, thanks . Haha compilers tell you why they don't compile, but sometimes it is so hard to understand why.

  7. #7
    Registered User
    Join Date
    Mar 2010
    Posts
    583
    Great!

    Quote Originally Posted by mgracecar View Post
    I changed it to how I have it in here now and it works on gcc which is what it's graded on. No errors or anything. But on Dev-C It's not compiling. Hmmm
    Haha, yah, the error would be helpful.

    It's probably the variable length arrays:

    Code:
        double x[size];
        double y[size];
    Variable length arrays are part of C99, and most C compilers don't default to C99. gcc supports variable length arrays as an extension of both C90 and C++, so they're always allowed.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Area of a polygon
    By viblic in forum C Programming
    Replies: 1
    Last Post: 11-10-2011, 11:29 AM
  2. Area of a Polygon with Arcs
    By johnggold in forum C Programming
    Replies: 6
    Last Post: 10-20-2010, 12:54 AM
  3. finding area of ellipse
    By dvsumosize in forum C Programming
    Replies: 4
    Last Post: 10-26-2009, 07:20 PM
  4. polygon area
    By totalfreeloader in forum C++ Programming
    Replies: 4
    Last Post: 11-25-2003, 09:38 AM
  5. Polygon Area and Centroid :(
    By Moni in forum C++ Programming
    Replies: 7
    Last Post: 06-01-2003, 12:39 PM