Advertisement
Guest User

Untitled

a guest
Dec 9th, 2019
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.67 KB | None | 0 0
  1.  
  2. #define _CRT_SECURE_NO_WARNINGS
  3.  
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6.  
  7. int main()
  8. {
  9.  
  10.     // variables for reading the measurements from file
  11.     FILE * fp;
  12.     size_t len = 0;
  13.     char resp;
  14.  
  15.     double x, y, xPrev, yPrev, xFirst, yFirst, area, xSum, ySum;
  16.  
  17.     // open the input file "messwerte.txt" for reading
  18.     fp = fopen("polygon.txt", "r");
  19.     if (fp == NULL)
  20.     {
  21.         // if file could not be opened (wrong path, not readable, ...)
  22.         // output a short message and immediately exit
  23.         printf("Eingabedatei kann nicht geoeffnet werden.\n");
  24.         scanf("%c", &resp);
  25.         exit(EXIT_FAILURE);
  26.     }
  27.  
  28.     // print program header
  29.     printf("\n\nProgramm zur Berechnung eines Polygons\n");
  30.     printf("--------------------------------------------------------\n");
  31.  
  32.     // the following loop reads a new value in every iteration
  33.     // until the end of the file or some invalid token is found
  34.     area = 0.0;
  35.     xSum = 0.0;
  36.     ySum = 0.0;
  37.     int i;
  38.     for(i=0;;i++)
  39.     {
  40.         xPrev = x; //als Vorbereitung für die letzte Ecke
  41.         yPrev = y;
  42.  
  43.         len = fscanf(fp, "%lf %lf", &x, &y);
  44.  
  45.         if(len == EOF)
  46.             break;
  47.         else if(len == 0) {
  48.             printf("Unerwartetes Zeichen in der Eingabedatei.");
  49.             scanf("%c", &resp);
  50.             exit(EXIT_FAILURE);
  51.         }
  52.         else if(xPrev==x && yPrev==y)
  53.         {
  54.             printf("Doppelter Eckpunkt eingelesen.");
  55.             scanf("%c", &resp);
  56.             exit(EXIT_FAILURE);
  57.         }
  58.  
  59.         printf("Lese Eckpunkt: %6.2f/%6.2f\n", x, y); //Ausgabe der gegebenen Eckpunkte
  60.         xSum = xSum + x;
  61.         ySum = ySum + y;
  62.         if(i>0)
  63.         {
  64.             area = area+(x+xPrev)*(y-yPrev);
  65.         }
  66.         else
  67.         {
  68.             xFirst = x;
  69.             yFirst = y;
  70.         }
  71.     }
  72.     // finally close the input file and clean up memory
  73.     fclose(fp);
  74.  
  75.     if(i>0)
  76.         area = area+(xFirst+xPrev)*(yFirst-yPrev); //Für alles zusammensetzen inkl des letzten Punktes
  77.  
  78.     area = area/2.0;
  79.  
  80.     if(i>=3)
  81.     {
  82.         // output results
  83.         printf("\nErgebnisse:\n");
  84.         printf("Flaeche: %6.2f\nSchwerpunkt: %6.2f, %6.2f\n", area, xSum/i, ySum/i);
  85.         printf("-----------\n\n");
  86.     }
  87.     else
  88.         printf("Polygon unmoeglich (in unserem Universum)");
  89.     printf("\nDebugwert: %i",i);
  90.         //Beim folgenden Code von der Website hatte ich weder Zeit noch Ahnung wie ich das hinzufügen sollte
  91.         //Grundsätzlich verstehe ich die Mathematik dahinter, Bild ist im Anhang
  92.     /*int pnpoly(int nvert, float *vertx, float *verty, float testx, float testy)
  93.     {
  94.         int i, j, c = 0;
  95.         for (i = 0, j = nvert-1; i < nvert; j = i++) {
  96.             if ( ((verty[i]>testy) != (verty[j]>testy)) &&
  97.                  (testx < (vertx[j]-vertx[i]) * (testy-verty[i]) / (verty[j]-verty[i]) + vertx[i]) )
  98.                 c = !c;
  99.         }
  100.         return c;
  101.     }*/
  102.  
  103.     return 0;
  104. }
  105.  
  106.  
  107. //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