Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define _CRT_SECURE_NO_WARNINGS
- #include <stdio.h>
- #include <stdlib.h>
- int main()
- {
- // variables for reading the measurements from file
- FILE * fp;
- size_t len = 0;
- char resp;
- double x, y, xPrev, yPrev, xFirst, yFirst, area, xSum, ySum;
- // open the input file "messwerte.txt" for reading
- fp = fopen("polygon.txt", "r");
- if (fp == NULL)
- {
- // if file could not be opened (wrong path, not readable, ...)
- // output a short message and immediately exit
- printf("Eingabedatei kann nicht geoeffnet werden.\n");
- scanf("%c", &resp);
- exit(EXIT_FAILURE);
- }
- // print program header
- printf("\n\nProgramm zur Berechnung eines Polygons\n");
- printf("--------------------------------------------------------\n");
- // the following loop reads a new value in every iteration
- // until the end of the file or some invalid token is found
- area = 0.0;
- xSum = 0.0;
- ySum = 0.0;
- int i;
- for(i=0;;i++)
- {
- xPrev = x; //als Vorbereitung für die letzte Ecke
- yPrev = y;
- len = fscanf(fp, "%lf %lf", &x, &y);
- if(len == EOF)
- break;
- else if(len == 0) {
- printf("Unerwartetes Zeichen in der Eingabedatei.");
- scanf("%c", &resp);
- exit(EXIT_FAILURE);
- }
- else if(xPrev==x && yPrev==y)
- {
- printf("Doppelter Eckpunkt eingelesen.");
- scanf("%c", &resp);
- exit(EXIT_FAILURE);
- }
- printf("Lese Eckpunkt: %6.2f/%6.2f\n", x, y); //Ausgabe der gegebenen Eckpunkte
- xSum = xSum + x;
- ySum = ySum + y;
- if(i>0)
- {
- area = area+(x+xPrev)*(y-yPrev);
- }
- else
- {
- xFirst = x;
- yFirst = y;
- }
- }
- // finally close the input file and clean up memory
- fclose(fp);
- if(i>0)
- area = area+(xFirst+xPrev)*(yFirst-yPrev); //Für alles zusammensetzen inkl des letzten Punktes
- area = area/2.0;
- if(i>=3)
- {
- // output results
- printf("\nErgebnisse:\n");
- printf("Flaeche: %6.2f\nSchwerpunkt: %6.2f, %6.2f\n", area, xSum/i, ySum/i);
- printf("-----------\n\n");
- }
- else
- printf("Polygon unmoeglich (in unserem Universum)");
- printf("\nDebugwert: %i",i);
- //Beim folgenden Code von der Website hatte ich weder Zeit noch Ahnung wie ich das hinzufügen sollte
- //Grundsätzlich verstehe ich die Mathematik dahinter, Bild ist im Anhang
- /*int pnpoly(int nvert, float *vertx, float *verty, float testx, float testy)
- {
- int i, j, c = 0;
- for (i = 0, j = nvert-1; i < nvert; j = i++) {
- if ( ((verty[i]>testy) != (verty[j]>testy)) &&
- (testx < (vertx[j]-vertx[i]) * (testy-verty[i]) / (verty[j]-verty[i]) + vertx[i]) )
- c = !c;
- }
- return c;
- }*/
- return 0;
- }
- //Even Odd: gegebener Punkt geht in eine Richtung, als Strahl. zb +x. Dann wird geguckt wie oft überquert man eine der Kanten?
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement