You are to modify the program below so that after inputting a polygon and comput
ID: 3810434 • Letter: Y
Question
You are to modify the program below so that after inputting a polygon and computing and displaying the area, the program will read an arbitrary number of points and determine if each one is inside or outside the polygon. Again a non-numeric indicates the end of this list of points. See sample output 2. Your program must have a function with header: int ptInPoly(Point q, Point p[], int n) that determines if point q is in the polygon given in p. The points in p are in clockwise order. ptInPoly is called from your main function for each point to be checked for inclusion in a polygon. Your functions returns 1 if the point is inside the polygon and 0 if it is not. A point on the boundary is considered to be inside the polygon. #include <stdio.h> #include <stdlib.h> typedef struct { double x, y; } Point; double parea(Point p[], int n) { // Finds area of a polygon given as n points // in clockwise order around the polygon. // Does not matter which point is first. int i, j = n - 1; // j starts at last point double area = 0.0; for (i = 0; i < n; i++) { area += (p[i].x - p[j].x) * (p[i].y + p[j].y); // divide by 2 done once j = i; } return area / 2.0; } int main(void) { Point poly[100]; int n; char str[10]; setvbuf(stdout, NULL, _IONBF, 0); while (1) { printf("Enter polygon points in clockwise direction: "); n=0; while (1) { if(2>scanf("%lf%lf",&poly[n].x,&poly[n].y)){ scanf("%s",str); // this clears the nonnumeric from the input break; } n++; } if (n == 0) break; printf("Area= %f ", parea(poly, n)); } printf("Program completed normally. "); return EXIT_SUCCESS; }
Explanation / Answer
#include #include typedef struct { double x, y; } Point; double parea(Point p[], int n) { // Finds area of a polygon given as n points // in clockwise order around the polygon. // Does not matter which point is first. int i, j = n - 1; // j starts at last point double area = 0.0; for (i = 0; i scanf("%lf%lf",&poly[n].x,&poly[n].y)){ scanf("%s",str); // this clears the nonnumeric from the input break; } n++; } if (n == 0) break; printf("Area= %f ", parea(poly, n)); } printf("Program completed normally. "); return EXIT_SUCCESS; }Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.