Advertisement
Guest User

Untitled

a guest
Jan 23rd, 2020
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 8.41 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <math.h>
  5.  
  6.  
  7.  
  8. typedef struct lista_okregow{
  9.     char figura;
  10.     int wsp_x, wsp_y, r;
  11.     struct lista_okregow *next;
  12. }lista_o;
  13.  
  14. typedef struct lista_prostokatow{
  15.     char figura;
  16.     int wsp_x, wsp_y, wsp2_x, wsp2_y;
  17.     struct lista_prostokatow *next;
  18. }lista_p;
  19.  
  20. void sortowanie (char* nazwa_pliku, lista_o **wierzcholek_o, lista_p **wierzcholek_p)
  21. {
  22.     lista_o *nowy1;
  23.     lista_p *nowy2;
  24.     FILE * plik;
  25.     char figura, wiersz[64], pom;
  26.     int wsp_x, wsp_y, r, wsp2_x, wsp2_y;
  27.  
  28.     if((plik = fopen(nazwa_pliku,"r"))==NULL)
  29.     {
  30.         printf("plik nie zostal otwarty");
  31.         return NULL;
  32.     }
  33.  
  34.     while((fgets(wiersz, sizeof(wiersz), plik)))
  35.     {
  36.         pom = wiersz[0];
  37.         if(pom == 'o' || pom == 'O')
  38.         {
  39.             //printf("%c\n",pom );
  40.             nowy1 = malloc(sizeof(lista_o));
  41.             sscanf(wiersz, "%c %d %d %d", &figura, &wsp_x, &wsp_y, &r);
  42.             nowy1 -> figura = figura;
  43.             nowy1 -> wsp_x = wsp_x;
  44.             nowy1 -> wsp_y = wsp_y;
  45.             nowy1 -> r = r;
  46.             nowy1 -> next = *wierzcholek_o;
  47.             *wierzcholek_o = nowy1;
  48.         }
  49.         else if(pom == 'p' || pom == 'P')
  50.         {
  51.             //printf("%c\n",pom );
  52.             nowy2 = malloc(sizeof(lista_p));
  53.             sscanf(wiersz, "%c %d %d %d %d", &figura, &wsp_x, &wsp_y, &wsp2_x, &wsp2_y);
  54.             nowy2 -> figura = figura;
  55.             nowy2 -> wsp_x = wsp_x;
  56.             nowy2 -> wsp_y = wsp_y;
  57.             nowy2 -> wsp2_x = wsp2_x;
  58.             nowy2 -> wsp2_y = wsp2_y;
  59.             nowy2 -> next = *wierzcholek_p;
  60.             *wierzcholek_p = nowy2;
  61.         }
  62.     }
  63.     fclose(plik);
  64. }
  65.  
  66. void wyswietlanie_o(lista_o* nazwa_listy)
  67. {
  68.     lista_o *pomoc;
  69.     pomoc = nazwa_listy;
  70.  
  71.     while(pomoc->next != NULL) {
  72.         printf("%c %d %d %d\n",pomoc->figura, pomoc->wsp_x, pomoc->wsp_y, pomoc->r);
  73.         pomoc = pomoc->next;
  74.     }
  75. }
  76. void wyswietlanie_p(lista_p* nazwa_listy)
  77. {
  78.     lista_p *pomoc;
  79.     pomoc = nazwa_listy;
  80.  
  81.     while(pomoc->next != NULL) {
  82.         printf("%c %d %d %d %d\n",pomoc->figura, pomoc->wsp_x, pomoc->wsp_y, pomoc->wsp2_x, pomoc->wsp2_y);
  83.         pomoc = pomoc->next;
  84.     }
  85. }
  86.  
  87. int pop_o(lista_o** adres_listy) { //Usuwa pierwszy element listy.
  88.  
  89.         lista_o* new_head = (*adres_listy)->next;
  90.         free(*adres_listy);
  91.         if(new_head != NULL) *adres_listy = new_head;
  92.         else *adres_listy = NULL;
  93.  
  94.     return 0;
  95. }
  96.  
  97. int pop_p(lista_p** adres_listy) {
  98.  
  99.         lista_p* new_head = (*adres_listy)->next;
  100.         free(*adres_listy);
  101.         if(new_head != NULL) *adres_listy = new_head;
  102.         else *adres_listy = NULL;
  103.  
  104.     return 0;
  105. }
  106.  
  107. void usun_element_z_listy_o(lista_o** adres_listy,lista_o** element)
  108. {
  109.         lista_o* current = (*((lista_o**)adres_listy));
  110.         lista_o* to_remove = (lista_o*) element;
  111.  
  112.         if(to_remove == current) {
  113.             pop_o((lista_o**)adres_listy);
  114.             return;
  115.         }
  116.  
  117.         while(current->next != NULL) {
  118.             if(current->next == to_remove) {
  119.                 current->next = to_remove->next;
  120.                 free(to_remove);
  121.                 break;
  122.             }
  123.             current = current->next;
  124. }
  125. }
  126.  
  127. void usun_element_z_listy_p(lista_p** adres_listy,lista_p** element)
  128. {
  129.         lista_p* current = (*((lista_p**)adres_listy));
  130.         lista_p* to_remove = (lista_p*) element;
  131.  
  132.         if(to_remove == current) {
  133.             pop_p((lista_p**)adres_listy);
  134.             return;
  135.         }
  136.  
  137.         while(current->next != NULL) {
  138.             if(current->next == to_remove) {
  139.                 current->next = to_remove->next;
  140.                 free(to_remove);
  141.                 break;
  142.             }
  143.             current = current->next;
  144.         }
  145. }
  146.  
  147. void remove_overlapping_rectangles(lista_p** rectangle_list) { //Usuwa odpowiedni z dwoch nakladajacych sie prostokatow.
  148.  
  149.     //if(*rectangle_list == NULL) return;
  150.  
  151.     lista_p* current_a = *rectangle_list, *prev_a = NULL;
  152.     lista_p* current_b = NULL, *prev_b = NULL;
  153.  
  154.     while(current_a->next != NULL && current_a != NULL) {
  155.         double ltx_a = current_a->wsp_x;
  156.         double lty_a = current_a->wsp_y;
  157.         double rbx_a = current_a->wsp2_x;
  158.         double rby_a = current_a->wsp2_y;
  159.  
  160.         double circumference_a = (abs(rbx_a - ltx_a) * 2) + (abs(rby_a - lty_a) * 2);
  161.  
  162.         current_b = current_a->next;
  163.         while(current_b != NULL) {
  164.             double ltx_b = current_b->wsp_x;
  165.             double lty_b = current_b->wsp_y;
  166.             double rbx_b = current_b->wsp2_x;
  167.             double rby_b = current_b->wsp2_y;
  168.  
  169.             double circumference_b = (abs(rbx_b - ltx_b) * 2) + (abs(rby_b - lty_b) * 2);
  170.  
  171.             if(!((rbx_b < ltx_a)
  172.             || (rbx_a < ltx_b)
  173.             || (lty_b < rby_a)
  174.             || (lty_a < rby_b))) {
  175.                 if(circumference_a > circumference_b) {
  176.                     usun_element_z_listy_p(rectangle_list, current_b);
  177.                 }
  178.                 else if(circumference_a < circumference_b) {
  179.                     usun_element_z_listy_p(rectangle_list, current_a);
  180.                     break;
  181.                 }
  182.             }
  183.             current_b = current_b->next;
  184.         }
  185.         current_a = current_a->next;
  186.     }
  187. }
  188.  
  189. void usun_nakladajace_kola(lista_o** lista_okr) {
  190.  
  191.     lista_o* ok1 = *lista_okr;
  192.     lista_o* ok2 = NULL;
  193.  
  194.     while(ok1->next != NULL && ok1 != NULL) {
  195.         double x_a = ok1->wsp_x;
  196.         double y_a = ok1->wsp_y;
  197.         double r_a = ok1->r;
  198.         ok2 = ok1->next;
  199.         while(ok2 != NULL) {
  200.             double x_b = ok2->wsp_x;
  201.             double y_b = ok2->wsp_y;
  202.             double r_b = ok2->r;
  203.  
  204.             double odleglosc_srodkow = hypot((x_b - x_a), (y_b - y_a)); //odleglosc srodkow porownywanych okregow
  205.             double roznica = abs(r_b - r_a); //modul z roznicy promieni tych okregow
  206.             double suma = r_b + r_a; //suma ich promieni
  207.  
  208.             if((odleglosc_srodkow == roznica)
  209.             || (odleglosc_srodkow == suma)
  210.             || (odleglosc_srodkow < roznica)
  211.             || (roznica < odleglosc_srodkow && odleglosc_srodkow < suma)) {
  212.                 if(r_a > r_b) {
  213.                     usun_element_z_listy_o(lista_okr, ok2);
  214.                 }
  215.                 else if(r_a < r_b) {
  216.                     usun_element_z_listy_o(lista_okr, ok1);
  217.                     break;
  218.                 }
  219.             }
  220.             ok2 = ok2->next;
  221.         }
  222.         ok1 = ok1->next;
  223.     }
  224. }
  225.  
  226. void send_lists_to_file(char* nazwa_pliku, lista_o* circle_list, lista_p* rectangle_list) {
  227.     FILE* file = fopen(nazwa_pliku, "w");
  228.  
  229.     if(file == NULL) {
  230.         printf("Couldn't load up file.");
  231.         return;
  232.     }
  233.  
  234.     //Czy listy zawieraja elementy do zapisania?
  235.     if(circle_list == NULL) {
  236.         printf("List is NULL.");
  237.         return;
  238.     }
  239.     if(rectangle_list == NULL) {
  240.         printf("List is NULL.");
  241.         return;
  242.     }
  243.  
  244.     //lista_o* current_circle = circle_list;
  245.  
  246.     while(circle_list->next != NULL) {
  247.         fprintf(file, "o %d %d %d\n", circle_list->wsp_x, circle_list->wsp_y, circle_list->r);
  248.         circle_list = circle_list->next;
  249.     }
  250.  
  251.     lista_p* current_rectangle = rectangle_list;
  252.  
  253.     while(current_rectangle->next != NULL) {
  254.         fprintf(file, "r %d %d %d %d\n", current_rectangle->wsp_x, current_rectangle->wsp_y, current_rectangle->wsp2_x, current_rectangle->wsp2_y);
  255.         current_rectangle = current_rectangle->next;
  256.     }
  257.  
  258.     printf("List sent to %s successfuly.\n", nazwa_pliku);
  259.  
  260. }
  261. int main()
  262. {
  263.  
  264.  
  265.     lista_o *wierzcholek_o = malloc(sizeof(lista_o)) , *pomoc_o;
  266.     lista_p *wierzcholek_p = malloc(sizeof(lista_p)) , *pomoc_p;
  267.     wierzcholek_o->next = NULL;
  268.     wierzcholek_p->next = NULL;
  269.  
  270.     sortowanie("figury.txt", &wierzcholek_o, &wierzcholek_p);
  271.  
  272.     /*printf("%p\n", *wierzcholek_o);
  273.     printf("%p\n", *wierzcholek_p);
  274.     pomoc_o = wierzcholek_o;
  275.     pomoc_p = wierzcholek_p;*/
  276.  
  277.     wyswietlanie_o(wierzcholek_o);
  278.     wyswietlanie_p(wierzcholek_p);
  279.     printf("\n");
  280.  
  281.     //pop_o(&wierzcholek_o);
  282.     //pop_p(&wierzcholek_p);
  283.     remove_overlapping_rectangles(&wierzcholek_p);
  284.     usun_nakladajace_kola(&wierzcholek_o);
  285.  
  286.     wyswietlanie_o(wierzcholek_o);
  287.     wyswietlanie_p(wierzcholek_p);
  288.  
  289.  
  290.  
  291.     send_lists_to_file("figury2.txt",wierzcholek_o,wierzcholek_p);
  292.  
  293.  
  294.  
  295.     return 0;
  296. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement