Thread: Circumference of Polygon

  1. #1
    Registered User
    Join Date
    Dec 2010
    Posts
    3

    Circumference of Polygon

    I need some help with a program I am writing which reads type double vertices from a text file, then computes and prints the perimeter of the resultant polygon.

    Every time i compile and run it, everything seems to be ok until i hit the for statement, which triggers a seg fault. Beyond that, there is probably a bigger problem, but i cannot see it.

    edit: I also found another thread with a very similar topic, but even after completely duplicating the resulting program, it still gave incorrect values.
    Code:
    #include <stdio.h>
    #include <math.h>
    
    typedef struct {double x, y;} vertex_t;
    double dist(vertex_t a, vertex_t b);
    
    int main(void)	{
    	
    	FILE *fp;
    	char name[64];
    	double perim = 0,x,y;
    
    	vertex_t verts[20];
    	vertex_t buffer;
    
    	int i = 0,j = 0,max = 0,status = 1;
    
    	printf("Enter the file name: ");
    	scanf("%s",name);
    
    	fp = fopen(name,"r");
    	
    
    	while (status != EOF)	{
    		status = fscanf(fp," %lf %lf ",&x, &y);
    		buffer.x = x;
    		buffer.y = y;
    		verts[i] = buffer;
    		i++;
    		max++;
    	}
    	
    	for (i = 0;i < max;j++)	{
    		perim += dist(verts[j],verts[j+1]);
    	}
    			
    	perim += dist(verts[0],verts[max]);
    
    	printf("The perimeter is %.3lf\n",perim);
    
    	return 0;
    }
    
    double dist(vertex_t a, vertex_t b)	{
    	return sqrt(pow(b.x-a.x,2)+pow(b.y-a.y,2));
    }
    Last edited by Prefixor; 12-03-2010 at 10:47 PM. Reason: woops perimeter not circumference... jesus im retarded

  2. #2
    -bleh-
    Join Date
    Aug 2010
    Location
    somewhere in this universe
    Posts
    463
    Code:
    for (i = 0;i < max;j++)	{
    		perim += dist(verts[j],verts[j+1]);
    The problem is you increment "j" instead of "i".

  3. #3
    Registered User
    Join Date
    Dec 2010
    Posts
    3
    ah, thats a foolish little mixup there. I fixed that whole for loop, but the program still returns incorrect values as well as reading too many lines.
    For instance, when i feed in a 3 line text file, with coordinates 1 2, 2 3, and 3 4, instead of getting 4*sqrt(2) (5.657), i get 10.064.
    when i insert a print statement in between the while and for loops, i is returned as 4... but it is a 3 line text file.

  4. #4
    Registered User
    Join Date
    Dec 2010
    Posts
    3
    Nvm got it working finally. Thanks for the point out nimitz.

    final working code:

    Code:
    #include <stdio.h>
    #include <math.h>
    
    typedef struct {double x, y;} vertex_t;
    double dist(vertex_t a, vertex_t b);
    
    int main(void)	{
    	
    	FILE *fp;
    	char name[64];
    	double perim = 0,x,y;
    
    	vertex_t verts[20];
    	vertex_t buffer;
    
    	int i = 0,max = -1,status = 1;
    
    	printf("Enter the file name: ");
    	scanf("%s",name);
    
    	fp = fopen(name,"r");
    	
    
    	while (status != EOF)	{
    		status = fscanf(fp," %lf %lf ",&x, &y);
    		buffer.x = x;
    		buffer.y = y;
    		verts[i] = buffer;
    		i++;
    		max++;
    	}
    	
    	for (i = 0;i < (max - 1);i++)	{
    			perim += dist(verts[i],verts[i+1]);
    	}
    			
    	perim += dist(verts[0],verts[max]);
    
    	printf("The perimeter is %.3lf\n",perim);
    
    	return 0;
    }
    
    double dist(vertex_t a, vertex_t b)	{
    	return sqrt(pow(b.x-a.x,2)+pow(b.y-a.y,2));
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 15
    Last Post: 09-16-2010, 04:05 PM
  2. Creating a Polygon with n sides
    By seeplusplus in forum C++ Programming
    Replies: 1
    Last Post: 05-17-2010, 08:21 PM
  3. Replies: 2
    Last Post: 04-13-2010, 03:14 PM
  4. Point in polygon test - spherical coords
    By bhdz in forum C Programming
    Replies: 1
    Last Post: 11-07-2007, 01:25 PM
  5. IDEA: Polygon unions and intersections
    By Magos in forum Contests Board
    Replies: 3
    Last Post: 05-21-2003, 07:16 PM