Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <math.h>
- typedef struct lista_okregow{
- char figura;
- int wsp_x, wsp_y, r;
- struct lista_okregow *next;
- }lista_o;
- typedef struct lista_prostokatow{
- char figura;
- int wsp_x, wsp_y, wsp2_x, wsp2_y;
- struct lista_prostokatow *next;
- }lista_p;
- void sortowanie (char* nazwa_pliku, lista_o **wierzcholek_o, lista_p **wierzcholek_p)
- {
- lista_o *nowy1;
- lista_p *nowy2;
- FILE * plik;
- char figura, wiersz[64], pom;
- int wsp_x, wsp_y, r, wsp2_x, wsp2_y;
- if((plik = fopen(nazwa_pliku,"r"))==NULL)
- {
- printf("plik nie zostal otwarty");
- return NULL;
- }
- while((fgets(wiersz, sizeof(wiersz), plik)))
- {
- pom = wiersz[0];
- if(pom == 'o' || pom == 'O')
- {
- //printf("%c\n",pom );
- nowy1 = malloc(sizeof(lista_o));
- sscanf(wiersz, "%c %d %d %d", &figura, &wsp_x, &wsp_y, &r);
- nowy1 -> figura = figura;
- nowy1 -> wsp_x = wsp_x;
- nowy1 -> wsp_y = wsp_y;
- nowy1 -> r = r;
- nowy1 -> next = *wierzcholek_o;
- *wierzcholek_o = nowy1;
- }
- else if(pom == 'p' || pom == 'P')
- {
- //printf("%c\n",pom );
- nowy2 = malloc(sizeof(lista_p));
- sscanf(wiersz, "%c %d %d %d %d", &figura, &wsp_x, &wsp_y, &wsp2_x, &wsp2_y);
- nowy2 -> figura = figura;
- nowy2 -> wsp_x = wsp_x;
- nowy2 -> wsp_y = wsp_y;
- nowy2 -> wsp2_x = wsp2_x;
- nowy2 -> wsp2_y = wsp2_y;
- nowy2 -> next = *wierzcholek_p;
- *wierzcholek_p = nowy2;
- }
- }
- fclose(plik);
- }
- void wyswietlanie_o(lista_o* nazwa_listy)
- {
- lista_o *pomoc;
- pomoc = nazwa_listy;
- while(pomoc->next != NULL) {
- printf("%c %d %d %d\n",pomoc->figura, pomoc->wsp_x, pomoc->wsp_y, pomoc->r);
- pomoc = pomoc->next;
- }
- }
- void wyswietlanie_p(lista_p* nazwa_listy)
- {
- lista_p *pomoc;
- pomoc = nazwa_listy;
- while(pomoc->next != NULL) {
- printf("%c %d %d %d %d\n",pomoc->figura, pomoc->wsp_x, pomoc->wsp_y, pomoc->wsp2_x, pomoc->wsp2_y);
- pomoc = pomoc->next;
- }
- }
- int pop_o(lista_o** adres_listy) { //Usuwa pierwszy element listy.
- lista_o* new_head = (*adres_listy)->next;
- free(*adres_listy);
- if(new_head != NULL) *adres_listy = new_head;
- else *adres_listy = NULL;
- return 0;
- }
- int pop_p(lista_p** adres_listy) {
- lista_p* new_head = (*adres_listy)->next;
- free(*adres_listy);
- if(new_head != NULL) *adres_listy = new_head;
- else *adres_listy = NULL;
- return 0;
- }
- void usun_element_z_listy_o(lista_o** adres_listy,lista_o** element)
- {
- lista_o* current = (*((lista_o**)adres_listy));
- lista_o* to_remove = (lista_o*) element;
- if(to_remove == current) {
- pop_o((lista_o**)adres_listy);
- return;
- }
- while(current->next != NULL) {
- if(current->next == to_remove) {
- current->next = to_remove->next;
- free(to_remove);
- break;
- }
- current = current->next;
- }
- }
- void usun_element_z_listy_p(lista_p** adres_listy,lista_p** element)
- {
- lista_p* current = (*((lista_p**)adres_listy));
- lista_p* to_remove = (lista_p*) element;
- if(to_remove == current) {
- pop_p((lista_p**)adres_listy);
- return;
- }
- while(current->next != NULL) {
- if(current->next == to_remove) {
- current->next = to_remove->next;
- free(to_remove);
- break;
- }
- current = current->next;
- }
- }
- void remove_overlapping_rectangles(lista_p** rectangle_list) { //Usuwa odpowiedni z dwoch nakladajacych sie prostokatow.
- //if(*rectangle_list == NULL) return;
- lista_p* current_a = *rectangle_list, *prev_a = NULL;
- lista_p* current_b = NULL, *prev_b = NULL;
- while(current_a->next != NULL && current_a != NULL) {
- double ltx_a = current_a->wsp_x;
- double lty_a = current_a->wsp_y;
- double rbx_a = current_a->wsp2_x;
- double rby_a = current_a->wsp2_y;
- double circumference_a = (abs(rbx_a - ltx_a) * 2) + (abs(rby_a - lty_a) * 2);
- current_b = current_a->next;
- while(current_b != NULL) {
- double ltx_b = current_b->wsp_x;
- double lty_b = current_b->wsp_y;
- double rbx_b = current_b->wsp2_x;
- double rby_b = current_b->wsp2_y;
- double circumference_b = (abs(rbx_b - ltx_b) * 2) + (abs(rby_b - lty_b) * 2);
- if(!((rbx_b < ltx_a)
- || (rbx_a < ltx_b)
- || (lty_b < rby_a)
- || (lty_a < rby_b))) {
- if(circumference_a > circumference_b) {
- usun_element_z_listy_p(rectangle_list, current_b);
- }
- else if(circumference_a < circumference_b) {
- usun_element_z_listy_p(rectangle_list, current_a);
- break;
- }
- }
- current_b = current_b->next;
- }
- current_a = current_a->next;
- }
- }
- void usun_nakladajace_kola(lista_o** lista_okr) {
- lista_o* ok1 = *lista_okr;
- lista_o* ok2 = NULL;
- while(ok1->next != NULL && ok1 != NULL) {
- double x_a = ok1->wsp_x;
- double y_a = ok1->wsp_y;
- double r_a = ok1->r;
- ok2 = ok1->next;
- while(ok2 != NULL) {
- double x_b = ok2->wsp_x;
- double y_b = ok2->wsp_y;
- double r_b = ok2->r;
- double odleglosc_srodkow = hypot((x_b - x_a), (y_b - y_a)); //odleglosc srodkow porownywanych okregow
- double roznica = abs(r_b - r_a); //modul z roznicy promieni tych okregow
- double suma = r_b + r_a; //suma ich promieni
- if((odleglosc_srodkow == roznica)
- || (odleglosc_srodkow == suma)
- || (odleglosc_srodkow < roznica)
- || (roznica < odleglosc_srodkow && odleglosc_srodkow < suma)) {
- if(r_a > r_b) {
- usun_element_z_listy_o(lista_okr, ok2);
- }
- else if(r_a < r_b) {
- usun_element_z_listy_o(lista_okr, ok1);
- break;
- }
- }
- ok2 = ok2->next;
- }
- ok1 = ok1->next;
- }
- }
- void send_lists_to_file(char* nazwa_pliku, lista_o* circle_list, lista_p* rectangle_list) {
- FILE* file = fopen(nazwa_pliku, "w");
- if(file == NULL) {
- printf("Couldn't load up file.");
- return;
- }
- //Czy listy zawieraja elementy do zapisania?
- if(circle_list == NULL) {
- printf("List is NULL.");
- return;
- }
- if(rectangle_list == NULL) {
- printf("List is NULL.");
- return;
- }
- //lista_o* current_circle = circle_list;
- while(circle_list->next != NULL) {
- fprintf(file, "o %d %d %d\n", circle_list->wsp_x, circle_list->wsp_y, circle_list->r);
- circle_list = circle_list->next;
- }
- lista_p* current_rectangle = rectangle_list;
- while(current_rectangle->next != NULL) {
- fprintf(file, "r %d %d %d %d\n", current_rectangle->wsp_x, current_rectangle->wsp_y, current_rectangle->wsp2_x, current_rectangle->wsp2_y);
- current_rectangle = current_rectangle->next;
- }
- printf("List sent to %s successfuly.\n", nazwa_pliku);
- }
- int main()
- {
- lista_o *wierzcholek_o = malloc(sizeof(lista_o)) , *pomoc_o;
- lista_p *wierzcholek_p = malloc(sizeof(lista_p)) , *pomoc_p;
- wierzcholek_o->next = NULL;
- wierzcholek_p->next = NULL;
- sortowanie("figury.txt", &wierzcholek_o, &wierzcholek_p);
- /*printf("%p\n", *wierzcholek_o);
- printf("%p\n", *wierzcholek_p);
- pomoc_o = wierzcholek_o;
- pomoc_p = wierzcholek_p;*/
- wyswietlanie_o(wierzcholek_o);
- wyswietlanie_p(wierzcholek_p);
- printf("\n");
- //pop_o(&wierzcholek_o);
- //pop_p(&wierzcholek_p);
- remove_overlapping_rectangles(&wierzcholek_p);
- usun_nakladajace_kola(&wierzcholek_o);
- wyswietlanie_o(wierzcholek_o);
- wyswietlanie_p(wierzcholek_p);
- send_lists_to_file("figury2.txt",wierzcholek_o,wierzcholek_p);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement