Note: so The question starts from 2.(if n points are connected to form.......) a
ID: 3549641 • Letter: N
Question
Note: so The question starts from 2.(if n points are connected to form.......) and ends at the part above 2.( represent the (x,y) coordinates.......)
Represent the (x, y) coordinates of the connected points as two arrays of at most 20 type double values. For one of your tests, use the following data set, which defines a polygon whose area is 25.5 square units. Implement the following functions: get_corners-Takes as parameters an input file, arrays x and y, and the arrays' maximum size. Fills the arrays with data from the file (ignoring any data that would overflow the arrays) and returns as the function value the number of (x, y) coordinates stored in the arrays. output_corners-Takes as parameters an output file and two type double arrays of the same size and their actual size, and outputs to the file the contents of the two arrays in two columns. polygon_area-Takes as parameters two arrays representing the (x, y) coordinates of the corners of a closed polygon and their actual size and returns as the function value the area of the closed polygon. If n points are connected to form a closed polygon as shown below, the area A of the polygon can be computed as Notice that although the illustrated polygon has only six distinct corners, n for this polygon is 7 because the algorithm expects that the last point, (x6, y6), will be a repeat of the initial point, (x0, y0).Explanation / Answer
#include<stdio.h>
#include<stdlib.h>
int get_corners( char *file_name, double x[], double y[], int max)
{
FILE *fp ;
int i = 0;
fp = fopen( file_name, "r" );
if( fp == NULL )
{
printf(" File opening failed ");
exit(1);
}
while( i < max )
{
fscanf( fp, "%lf", &x[i]);
fscanf( fp, "%lf", &y[i]);
if( feof(fp) )
break;
i++;
}
return i+1;
}
void output_corners( char * file_name, double x[], double y[], int size)
{
FILE *fp;
int i = 0;
fp = fopen( file_name, "w" );
if( fp == NULL )
{
printf(" File opening failed ");
exit(1);
}
fprintf( fp," x y");
while( i < size )
fprintf( fp," %lf %lf", x[i],y[i++]);
return ;
}
double polygon_area( double x[], double y[], int size )
{
int i = 0;
double A = 0;
while( i < size )
{
A += ( x[i+1]+x[i] )*( y[i+1]-y[i] );
i++;
}
A *= 0.5;
if( A < 0 )
return -A;
else
return A;
}
int main(int argc, char *argv[])
{
double x[20], y[20];
int count = 0;
double area = 0;
count = get_corners( "input", x, y, 20 );
output_corners("output", x, y, count);
area = polygon_area( x, y, count );
printf(" Area of required polygon is %lf ", area);
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.