Advertisement
Aurox_

cerchiLeggibile.c

Dec 7th, 2023
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.30 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4.  
  5. #define N_MAX_PUNTI 1000000
  6. #define STRLEN      1024
  7. #define FILE_PATH   "./filetxt/cerchi.txt"
  8.  
  9. #define MAX(A,B)    ((A)>(B))?A:B
  10. #define MIN(A,B)    ((A)<(B))?A:B
  11. #define ABS(A)      ((A)<0)?(-A):A
  12.  
  13. typedef struct _dot {
  14.     float x;
  15.     float y;
  16. } dot;
  17. typedef struct _circ {
  18.     dot centro;
  19.     float r;
  20. } circ;
  21.  
  22. typedef dot*    pdot;
  23. typedef circ*   pcirc;
  24. typedef FILE*   pfile;
  25.  
  26. float frandminmax(float min, float max)
  27. {
  28.     return ((((float)rand())/RAND_MAX)*(max-min))+min;
  29. }
  30.  
  31. int main(void)
  32. {
  33.     unsigned i,i_mc,n_cerchi,n_hit,check;
  34.     float x_min,x_max,y_min,y_max,x_min_mc,x_max_mc,y_min_mc,y_max_mc,r_grande,r_gq,a,b,c,area;
  35.     char s[STRLEN];
  36.     dot baricentro,test;
  37.     pcirc pcerchio,pc,pc_max;
  38.     pfile pf;
  39.  
  40.     srand(time(NULL));
  41.  
  42.     if ((pf=fopen(FILE_PATH,"r"))==NULL) goto err_file;
  43.     fscanf(pf,"%s",s);
  44.     n_cerchi=atoi(s);
  45.     if ((pcerchio=(pcirc)malloc(sizeof(circ)*n_cerchi))==NULL) goto err_mem;
  46.  
  47.     baricentro.x=baricentro.y=0;
  48.     for (i=0;i<n_cerchi;i++) {
  49.         fscanf(pf,"%s",s); baricentro.x+=(pcerchio[i].centro.x=atof(s));
  50.         fscanf(pf,"%s",s); baricentro.y+=(pcerchio[i].centro.y=atof(s));
  51.         fscanf(pf,"%s",s); pcerchio[i].r=atof(s);
  52.  
  53.         switch (i) {
  54.         case 0:
  55.             x_max=x_min=pcerchio[i].centro.x;
  56.             y_max=y_min=pcerchio[i].centro.y;
  57.             break;
  58.         default:
  59.             x_max=MAX(x_max,(pcerchio[i].centro.x+pcerchio[i].r));
  60.             x_min=MIN(x_min,(pcerchio[i].centro.x-pcerchio[i].r));
  61.             y_max=MAX(y_max,(pcerchio[i].centro.y+pcerchio[i].r));
  62.             y_min=MIN(y_min,(pcerchio[i].centro.y-pcerchio[i].r));
  63.             break;
  64.         }
  65.         printf("Cerchio n.%u. Centro: (%.3f,%.3f). Raggio: %.3f\n",i+1,pcerchio[i].centro.x,pcerchio[i].centro.y,pcerchio[i].r);
  66.     }
  67.     fclose(pf);
  68.  
  69.     baricentro.x/=n_cerchi; baricentro.y/=n_cerchi;
  70.     r_grande=MAX(MAX(ABS(x_max-baricentro.x),ABS(x_min-baricentro.x)),MAX(ABS(y_max-baricentro.y),ABS(y_min-baricentro.y)));
  71.  
  72.     r_gq=r_grande*r_grande;
  73.     pc_max=pcerchio+n_cerchi;
  74.     for (i_mc=0,n_hit=0;i_mc<N_MAX_PUNTI;i_mc++) {
  75.         test.x=baricentro.x+(a=frandminmax(-r_grande,r_grande));
  76.         test.y=baricentro.y+(b=frandminmax(-r_grande,r_grande));
  77.         if ((a*a+b*b)<r_gq) {
  78.             pc=pcerchio;
  79.             check=1;
  80.             do {
  81.                 a=test.x-pc->centro.x;
  82.                 b=test.y-pc->centro.y;
  83.                 c=pc->r;
  84.                 if ((a*a+b*b)<(c*c)) {
  85.                     n_hit++;
  86.                     check=0;
  87.                 }
  88.                 pc++;
  89.             } while ((pc<pc_max)&&(check));
  90.         }
  91.     }
  92.  
  93.     free(pcerchio);
  94.     area=4*r_grande*r_grande*((float)n_hit)/N_MAX_PUNTI; /*area quadrato * rapporto tra hit e totale punti*/
  95.  
  96.     printf("Baricentro: (%.3f,%.3f)\nx min ret: %.3f\ny min ret: %.3f\nx max ret: %.3f\ny max ret: %.3f\nRaggio circoscritto: %.3f\nNumero Hit: %u\nArea: %.3f\n",baricentro.x,baricentro.y,x_min,y_min,x_max,y_max,r_grande,n_hit,area);
  97.     return EXIT_SUCCESS;
  98.  
  99.     err_file:
  100.         printf("ERRORE: non e' stato possibile aprire il file.\n");
  101.         return EXIT_FAILURE;
  102.     err_mem:
  103.         printf("ERRORE: memoria insufficiente.\n");
  104.         return EXIT_FAILURE;
  105. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement