Aurox_

houghTransform.c

Dec 7th, 2023 (edited)
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.27 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <limits.h>
  4. #include <time.h>
  5.  
  6. #define STRLEN 1024
  7. #define MIN 0
  8. #define MAX 100
  9. #define M_MIN 0.0
  10. #define M_MAX 10.0
  11. #define Q_MIN 0.0
  12. #define Q_MAX 10.0
  13. #define N_PROVE 500
  14. #define SI 1
  15. #define NO 0
  16.  
  17. #define ABS(X) ((X<0)?-X:X)
  18.  
  19. typedef int* pint;
  20. typedef float* pfloat;
  21. typedef FILE* pfile;
  22.  
  23. typedef struct _dot
  24.   {
  25.     double x;
  26.     double y;
  27.   }
  28. dot;
  29.  
  30. typedef dot* pdot;
  31.  
  32. double randminmax(double min, double max)
  33.   {
  34.     return ((((double)rand())/RAND_MAX)*(max-min))+min;
  35.   }
  36.  
  37. int conferma(void)
  38.   {
  39.     char input[STRLEN];
  40.     printf("Confermi? [S,s,Y,y = conferma / ALTRO = non conferma]: ");
  41.     scanf("%s",input);
  42.     switch (input[0])
  43.       {
  44.         case 'y':
  45.         case 'Y':
  46.         case 's':
  47.         case 'S':
  48.             return SI;
  49.         default:
  50.             return NO;
  51.       }
  52.   }
  53.  
  54. int dotCounter(double m, double q, pdot array, int numero_punti, double epsilon)
  55.   {
  56.     pdot p,pmax;
  57.     int i;
  58.     for(p=array,pmax=array+numero_punti,i=0;p<pmax;p++)
  59.       {
  60.         if(ABS(p->y-(m*(p->x)+q))<epsilon) i++;
  61.       }
  62.     return i;
  63.   }
  64.  
  65. int main(void)
  66.   {
  67.     int numero_punti;
  68.     unsigned i_m,i_q,n,n_max;
  69.     double epsilon,rumore,m,q,m_best,q_best,dm,dq;
  70.     char input[STRLEN];
  71.     pdot pcoord,p,pmax;
  72.     srand(time(NULL));
  73.  
  74.     do
  75.       {
  76.         printf("Inserisci il numero di punti (strettamente positivo): ");
  77.         scanf("%s",input); numero_punti=atoi(input);
  78.  
  79.         printf("Inserisci il valore per epsilon: ");
  80.         scanf("%s",input); epsilon=atof(input);
  81.  
  82.         printf("Inserisci il valore per r: ");
  83.         scanf("%s",input); rumore=atof(input);
  84.  
  85.         printf("Insersici il valore per m: ");
  86.         scanf("%s",input); m=atof(input);
  87.  
  88.         printf("Inserisci il valore per q: ");
  89.         scanf("%s",input); q=atof(input);
  90.  
  91.         printf("Ho ricevuto i seguenti valori:\nNumero punti: %d\nEpsilon: %f\nRumore: %f\nCoeff. angolare: %f\nIntercetta: %f\n",numero_punti,epsilon,rumore,m,q);
  92.       }
  93.     while(!conferma());
  94.  
  95.     if(numero_punti<0) goto err_num;
  96.  
  97.     if(NULL==(pcoord=(pdot)malloc(sizeof(dot)*numero_punti))) goto err_mem;
  98.  
  99.     for(p=pcoord,pmax=p+numero_punti;p<pmax;p++)
  100.       {
  101.         p->y=m*(p->x=randminmax(MIN,MAX))+q+randminmax(-rumore,rumore);
  102.       }
  103.  
  104.     dm=(M_MAX-M_MIN)/(N_PROVE-1);
  105.     dq=(Q_MAX-Q_MIN)/(N_PROVE-1);
  106.  
  107.     for(i_m=n=n_max=0,m_best=q_best=0,m=M_MIN;i_m<N_PROVE;i_m++,m+=dm)
  108.       {
  109.         for(i_q=0,q=Q_MIN;i_q<N_PROVE;i_q++,q+=dq)
  110.           {
  111.             //printf("Tentando m=%f, q=%f\n",m,q);
  112.             if((n=dotCounter(m,q,pcoord,numero_punti,epsilon))>n_max)
  113.               {
  114.                 n_max=n;
  115.                 m_best=m;
  116.                 q_best=q;
  117.                 printf("Dati aggiornati hits %d, mb=%f, qb=%f\n",n_max,m_best,q_best);
  118.               }
  119.            
  120.           }
  121.        
  122.       }
  123.  
  124.     free(pcoord);
  125.  
  126.     printf("Ho trovato un coefficiente angolare di %f, un'intercetta di %f, con un numero di hit pari a %d.\n",m_best,q_best,n_max);
  127.  
  128.     return EXIT_SUCCESS;
  129.     err_mem:
  130.         printf("ERRORE: memoria insufficiente\n");
  131.         return EXIT_FAILURE;
  132.     err_num:
  133.         printf("ERRORE: hai inserito un numero negativo\n");
  134.         return EXIT_FAILURE;
  135. }
Advertisement
Add Comment
Please, Sign In to add comment