Advertisement
zhenialeks

Untitled

Jun 19th, 2020
2,061
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.37 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3.  
  4. #define s_point struct point
  5. #define s_equation struct equation
  6. #define POINT 1
  7. #define INF 2
  8. #define EMPTY_SET 3
  9.  
  10.  
  11. struct point {
  12.     float x;
  13.     float y;
  14. };
  15.  
  16. struct equation {
  17.     float a;
  18.     float b;
  19.     float c;
  20. };
  21.  
  22. float determinant(float x1, float y1, float x2, float y2){
  23. //    float kk = x1*y2 - y1*x2;
  24. //    printf("[inside determinant] x1=%f, y1=%f, x2=%f, y2=%f\n", x1, y1, x2, y2);
  25. //    printf("[inside determinant] kk = %f\n", (double) x1*y2 - y1*x2);
  26.     return x1*y2 - y1*x2;
  27. }
  28.  
  29. s_point* intersection(s_equation* eq1, s_equation* eq2, int *type) {
  30.     float a1, a2, b1, b2, c1, c2;
  31.     float x;
  32.     float y;
  33.  
  34.     a1 = eq1->a;
  35.     a2 = eq2->a;
  36.     b1 = eq1->b;
  37.     b2 = eq2->b;
  38.     c1 = eq1->c;
  39.     c2 = eq2->c;
  40.  
  41. //    printf("we are inside intersection a1=%f b1=%f a2=%f b2=%f c1=%f c2=%f\n", a1, a2, b1, b2, c1, c2);
  42.     s_point *new_p;
  43.  
  44.     float m = determinant(a1, b1, a2, b2);
  45.     if ( m == 0){
  46.         if (a1 == 0 && a2 == 0){
  47.             if (determinant(c1, b1, c2, b2) == 0){
  48.                 // совпадение
  49.                 *type = INF;
  50.             } else {
  51.                 // параллельность
  52.                 *type = EMPTY_SET;
  53.             }
  54.         } else
  55.         if (b1 == 0 && b2 == 0){
  56.             if (determinant(c1, a1, c2, a2) == 0){
  57.                 // совпадение
  58.                 *type = INF;
  59.             } else {
  60.                 // параллельность
  61.                 *type = EMPTY_SET;
  62.             }
  63.         } else {//TODO:check for it
  64.             if (determinant(c1, b1, c2, b2) == 0){
  65.                 // совпадение
  66.                 *type = INF;
  67.             } else {
  68.                 // параллельность
  69.                 *type = EMPTY_SET;
  70.             }
  71.         }
  72.         return NULL;
  73.     } else { //пересечение
  74.         float ssss = determinant(a1, b1, a2, b2);
  75.         x = determinant(c1, b1, c2, b2)/ssss;
  76.         y = determinant(a1, c1, a2, c2)/ssss;
  77.         new_p = (s_point *) malloc(sizeof(s_point));
  78.         new_p->x = x;
  79.         new_p->y = y;
  80.  
  81.         *type = POINT;
  82.         return new_p;
  83.     }
  84. }
  85.  
  86. int main() {
  87.     int n;
  88.     int res_type;
  89.  
  90.     FILE*    fin;
  91.     float f1, f2;
  92.     s_point* tmp;
  93.     char     c;
  94.     int inf_flag = 0;
  95.  
  96.     int size = 10;
  97.     int cur_pos = 0;
  98.     int should_add;
  99.     s_point *res_points = (s_point *) calloc(size, sizeof(s_point));
  100.  
  101.  
  102.  
  103.     fin = fopen("input.txt", "r");
  104.     fscanf(fin, "%d", &n);
  105.  
  106.     s_equation* input_data = (s_equation*)malloc(sizeof(s_equation)*n);
  107.  
  108. //    fscanf(fin, "%f", &f1);
  109.  
  110.  
  111.  
  112.  
  113.     for (int i = 0; i < n; i++)
  114.         fscanf(fin, "%f%f%f", &(input_data[i].a), &(input_data[i].b), &(input_data[i].c));
  115.  
  116. //    fscanf(fin, "%f", &f2);
  117.  
  118.  
  119. /*
  120.  
  121.     for (int i = 0; i < n; i++){
  122.         printf("OK, i've read a=%f b=%f c=%f\n", input_data[i].a, input_data[i].b, input_data[i].c);
  123.     }
  124. */
  125.  
  126.     for (int i = 0; i < n-1; i++){
  127.         for (int j = i + 1; j < n; j++) {
  128. //            printf("j = %d, input_data[j].c = %f\n", j, input_data[j].c);
  129.             tmp = intersection(&(input_data[i]), &(input_data[j]), &res_type);
  130. //            printf("%f %f\n", tmp->x, tmp->y);
  131.             switch (res_type){
  132.                 case INF: inf_flag = 1; break;
  133.  
  134.                 case POINT: //добавим в массив точку
  135.                     should_add = 1;
  136.                     for (int k = 0; k < cur_pos; k++){
  137.                         if (res_points[k].x == tmp->x &&
  138.                             res_points[k].y == tmp->y)
  139.                         {
  140.                             should_add = 0;
  141.                             break;
  142.                         }
  143.                     }
  144.                     if (should_add){
  145.                         //если массив забит, то увеличим количество места
  146.                         if (cur_pos == size){
  147.                             size += 10;
  148.                             res_points = realloc(res_points, sizeof(s_point)*size);
  149.                         }
  150.  
  151.                         res_points[cur_pos++] = *tmp;
  152.                         if (tmp != NULL) free(tmp);
  153.                     }
  154.                     break;
  155.             }
  156.         }
  157.     }
  158.     if (inf_flag) printf("Infinity");
  159.     else printf("%d", cur_pos);
  160.     fclose(fin);
  161.     return 0;
  162. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement