Add to Favorites    Make Home Page 301 Online  
 Language Categories  
 Our Services  

Home » C Home » Mathematics Home » Program to calculate Area of a Polygon

A D V E R T I S E M E N T

Search Projects & Source Codes:

Title Program to calculate Area of a Polygon
Author Elliot Marks
Author Email emarks [at] shaw.ca
Description Calculates the area and perimeter of a convex polygon
Category C » Mathematics
Hits 368449
Code Select and Copy the Code
/* Given the coordinates of the vertices of a convex polygon, calculate its area and perimeter. Subdivide it into triangles and calculate the area of each triangle with Heron's formula. Requires data file pvert.txt containing coordinates of each vertex. Example of data for a polygon with 5 vertices: 3 7 6 4 3 -2 -6 1 -6 7 */ #include <stdio.h> #include <stdlib.h> #include <math.h> #define MAX_VERT 50 enum {x, y}; typedef struct triangle { double v1[2]; double v2[2]; double v3[2]; } triangle; double area(triangle a); double perimeter(double *vertices, int size); double side(double *p1, double *p2); int main(void) { int n, idx; int triangles; int index; int xycount; double xy; double triangle_area; double polygon_area; double perim; double polygon_vertices[MAX_VERT] = {0.0}; triangle a; FILE *data; xycount = 0; polygon_area = 0; if((data = fopen("pvert.txt", "r")) == NULL) { fprintf(stderr, "can't open data file "); exit(EXIT_FAILURE); } /* Read x-y coordinates of the vertices of the polygon from a file. */ while(fscanf(data, "%lf", &xy) == 1) polygon_vertices[xycount++] = xy; fclose(data); idx = 0; /* triangles in polygon = vertices - 2 */ triangles = (xycount / 2) - 2; putchar(' '); for(index = 2, idx = 0; idx < triangles; index += 2, ++idx) { /* Load vertices of a triangle into struct. 1st vertex of the polygon will be the 1st vertex of each triangle. index holds the starting index of each consecutive set of triangle vertices after the 1st. */ a.v1[x] = polygon_vertices[0]; a.v1[y] = polygon_vertices[1]; a.v2[x] = polygon_vertices[index+0]; a.v2[y] = polygon_vertices[index+1]; a.v3[x] = polygon_vertices[index+2]; a.v3[y] = polygon_vertices[index+3]; /* calculate the area of the triangle */ triangle_area = area(a); printf("area of triangle = %.2f ", triangle_area); /* add triangle area to polygon area */ polygon_area += triangle_area; } printf(" area of polygon = %.2f ", polygon_area); /* calculate the perimeter of the polygon */ perim = perimeter(polygon_vertices, xycount); printf("perimeter of polygon = %.2f ", perim); return 0; } /* calculate triangle area with Heron's formula */ double area(triangle a) { double s1, s2, s3, S, area; s1 = side(a.v1, a.v2); s2 = side(a.v2, a.v3); s3 = side(a.v3, a.v1); S = (s1 + s2 + s3) / 2; area = sqrt(S*(S - s1)*(S - s2)*(S - s3)); return area; } /* calculate polygon perimeter */ double perimeter(double *vertices, int size) { int idx, jdx; double p1[2], p2[2], pfirst[2], plast[2]; double perimeter; perimeter = 0.0; /* 1st vertex of the polygon */ pfirst[x] = vertices[0]; pfirst[y] = vertices[1]; /* last vertex of polygon */ plast[x] = vertices[size-2]; plast[y] = vertices[size-1]; /* calculate perimeter minus last side */ for(idx = 0; idx <= size-3; idx += 2) { for(jdx = 0; jdx < 4; ++jdx) { p1[x] = vertices[idx]; p1[y] = vertices[idx+1]; p2[x] = vertices[idx+2]; p2[y] = vertices[idx+3]; } perimeter += side(p1, p2); } /* add last side */ perimeter += side(plast, pfirst); return perimeter; } /* calculate length of side */ double side(double *p1, double *p2) { double s1, s2, s3; s1 = (p1[x] - p2[x]); s2 = (p1[y] - p2[y]); s3 = (s1 * s1) + (s2 * s2); return sqrt(s3); }

Related Source Codes

Script Name Author
The Game Opposite as seen on Nokia 2300 Mobile Manikanta
RECURSIVE BALANCED QUICK SORT ashish
Radix Sort ashish
Change your mouse pointer Ashim
The blinking star Shashank
Data Validation Crylittlebaby
To search a file by giving file type like mp3 or mpeg or doc Prashanth SR
Menus Demonstration B.Chidhambaram
Employee Database Project Using C. Reenku Raman Nayak
Creating a Lexical Analyzer in c fahad bader al-buhairi ¦Õ¤ ?¤Ð Ãß??ÝÐÝ
Calendar Program Omkar & Devendra
Stop double Process for start in C Cedrik Jurak
Stop double Process for start in C Cedrik Jurak
Time Scheduler Atiq Anwar
A timepass game between atmost two players Rahul Roy

A D V E R T I S E M E N T




Google Groups Subscribe to SourceCodesWorld - Techies Talk
Email:

Free eBook - Interview Questions: Get over 1,000 Interview Questions in an eBook for free when you join JobsAssist. Just click on the button below to join JobsAssist and you will immediately receive the Free eBook with thousands of Interview Questions in an ebook when you join.

New! Click here to Add your Code!


ASP Home | C Home | C++ Home | COBOL Home | Java Home | Pascal Home
Source Codes Home Page

 Advertisements  

Google Search

Google

Source Codes World.com is a part of Vyom Network.

Vyom Network : Web Hosting | Dedicated Server | Free SMS, GRE, GMAT, MBA | Online Exams | Freshers Jobs | Software Downloads | Interview Questions | Jobs, Discussions | Placement Papers | Free eBooks | Free eBooks | Free Business Info | Interview Questions | Free Tutorials | Arabic, French, German | IAS Preparation | Jokes, Songs, Fun | Free Classifieds | Free Recipes | Free Downloads | Bangalore Info | Tech Solutions | Project Outsourcing, Web Hosting | GATE Preparation | MBA Preparation | SAP Info | Software Testing | Google Logo Maker | Freshers Jobs

Sitemap | Privacy Policy | Terms and Conditions | Important Websites
Copyright ©2003-2024 SourceCodesWorld.com, All Rights Reserved.
Page URL: http://www.sourcecodesworld.com/source/show.asp?ScriptID=636


Download Yahoo Messenger | Placement Papers | Free SMS | C Interview Questions | C++ Interview Questions | Quick2Host Review