From e032e57920b3612b0be3ac159653672fdea195b4 Mon Sep 17 00:00:00 2001 From: HORVILLE Ewen Date: Tue, 4 Jan 2022 15:55:34 +0100 Subject: [PATCH] =?UTF-8?q?TP07=20Listes=20Chain=C3=A9es=20(suite)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- APL1.2/TP07/chaines.c | 58 ++++++++++++++++++ APL1.2/TP07/extensions.c | 63 +++++++++++++++++++ APL1.2/TP07/flocon.c | 109 +++++++++++++++++++++++++++++++++ APL1.2/TP07/graph_sup.c | 71 +++++++++++++++++++++ APL1.2/TP07/graph_sup.h | 32 ++++++++++ APL1.2/TP07/raffinements.c | 122 +++++++++++++++++++++++++++++++++++++ 6 files changed, 455 insertions(+) create mode 100644 APL1.2/TP07/chaines.c create mode 100644 APL1.2/TP07/extensions.c create mode 100644 APL1.2/TP07/flocon.c create mode 100644 APL1.2/TP07/graph_sup.c create mode 100644 APL1.2/TP07/graph_sup.h create mode 100644 APL1.2/TP07/raffinements.c diff --git a/APL1.2/TP07/chaines.c b/APL1.2/TP07/chaines.c new file mode 100644 index 0000000..6591db3 --- /dev/null +++ b/APL1.2/TP07/chaines.c @@ -0,0 +1,58 @@ +#include +#include +#include + +struct char_ { + char letter; + struct char_* next; +}; + +typedef struct char_ string_char; +typedef string_char* string; + +void printString(string str) { + while (str != NULL) { + printf("%c", str->letter); + str = str->next; + } + printf("\n"); +} + +string toString(char* str) { + + string_char* last_c = NULL; + for (int i = strlen(str)+1; i >= 0; i--) { + string_char* c = malloc(sizeof(string_char)); + c->letter = str[i]; + c->next = last_c; + last_c = c; + } + + return last_c; +} + +char* toCharT(string str) { + int count = 0; + string str2 = str; + while (str2 != NULL) { + count++; + str2 = str2->next; + } + char* new_str = calloc(sizeof(char), count); + + for (int i = 0; i < count; i++) { + new_str[i] = str->letter; + str = str->next; + } + return new_str; +} + +int main(int argc, char * argv[]) { + string c = toString("Hello World !"); + printString(c); + char* c2 = toCharT(c); + printf("%s\n", c2); + + return EXIT_SUCCESS; +} + diff --git a/APL1.2/TP07/extensions.c b/APL1.2/TP07/extensions.c new file mode 100644 index 0000000..86fd63d --- /dev/null +++ b/APL1.2/TP07/extensions.c @@ -0,0 +1,63 @@ +#include +#include +#include + +struct char_ { + char letter; + struct char_* next; +}; + +typedef struct char_ string_char; +typedef string_char* string; + +string toString(char* str) { + + string_char* last_c = NULL; + for (int i = strlen(str)+1; i >= 0; i--) { + string_char* c = malloc(sizeof(string_char)); + c->letter = str[i]; + c->next = last_c; + last_c = c; + } + + return last_c; +} + +char* toCharT(string str) { + int count = 0; + string str2 = str; + while (str2 != NULL) { + count++; + str2 = str2->next; + } + char* new_str = calloc(sizeof(char), count); + + for (int i = 0; i < count; i++) { + new_str[i] = str->letter; + str = str->next; + } + return new_str; +} + +void sputs(string str) { + while (str != NULL) { + printf("%c", str->letter); + str = str->next; + } + printf("\n"); +} + +void sfgets(string* str, int maxLength, FILE * stream) { + char line[2048]; + fgets(line, maxLength, stream); + + *str = toString(line); +} + +int main(int argc, char * argv[]) { + string c; + sfgets(&c, 1000, stdin); + sputs(c); + return EXIT_SUCCESS; +} + diff --git a/APL1.2/TP07/flocon.c b/APL1.2/TP07/flocon.c new file mode 100644 index 0000000..d6cc4b3 --- /dev/null +++ b/APL1.2/TP07/flocon.c @@ -0,0 +1,109 @@ +#include +#include +#include +#include +#include "graph_sup.h" + +struct vertex { + double x; + double y; + struct vertex* next; +}; + +typedef struct vertex vert; +typedef vert* poly; + +double rad(double deg) { + return deg/180 * M_PI; +} + +poly makeTri(double x, double y, double r) { + vert* p1 = malloc(sizeof(vert)); + p1->x = x + cos(rad(30)) * r; + p1->y = y + sin(rad(30)) * r; + + vert* p2 = malloc(sizeof(vert)); + p2->x = x + cos(rad(150)) * r; + p2->y = y + sin(rad(150)) * r; + + vert* p3 = malloc(sizeof(vert)); + p3->x = x + cos(rad(270)) * r; + p3->y = y + sin(rad(270)) * r; + + p1->next = p2; + p2->next = p3; + p3->next = p1; + + return p1; +} + +void incPoly(poly* tri_add) { + poly tri = *tri_add; + vert* start = tri; + + int has_started = 0; + while (tri != start || has_started == 0) { + has_started = 1; + int x = tri->x; + int y = tri->y; + vert* next = tri->next; + int xx = next->x; + int yy = next->y; + + vert* p1 = malloc(sizeof(vert)); + vert* p2 = malloc(sizeof(vert)); + vert* p3 = malloc(sizeof(vert)); + + double tx = (xx - x) / 3; + double ty = (yy - y) / 3; + + p1->x = x + tx; + p1->y = y + ty; + + p3->x = x + tx * 2; + p3->y = y + ty * 2; + + p2->x = p1->x + (cos(rad(-60)) * tx - sin(rad(-60)) * ty); + p2->y = p1->y + (sin(rad(-60)) * tx + cos(rad(-60)) * ty); + + tri->next = p1; + p1->next = p2; + p2->next = p3; + p3->next = next; + + tri = tri->next->next->next->next; + } +} + +void drawPoly(poly tri) { + vert* start = tri; + + int has_started = 0; + while (tri != start || has_started == 0) { + has_started = 1; + int x = tri->x; + int y = tri->y; + vert* next = tri->next; + int xx = next->x; + int yy = next->y; + + tri = tri->next; + DessinerSegment(x, y, xx, yy); + } +} + +int main(int argc, char * argv[]) { + + poly tri = makeTri(1920/2, 1080/2, 550); + + + InitialiserGraphique(); + CreerFenetre(100, 100, 1920, 1080); + SetColor(255, 0, 0); + for (int i = 0; i < 10; i++) incPoly(&tri); + drawPoly(tri); + Touche(); + FermerGraphique(); + return EXIT_SUCCESS; +} + diff --git a/APL1.2/TP07/graph_sup.c b/APL1.2/TP07/graph_sup.c new file mode 100644 index 0000000..c008e65 --- /dev/null +++ b/APL1.2/TP07/graph_sup.c @@ -0,0 +1,71 @@ +#include "utils.h" +#include +#include +#include +#include +#include "graph_sup.h" + +#define FPS 2.0 + +/*La liste des bouttons et leur nombre*/ +button* Buttons; +int BT_Count = 0; + +/*Des variables afin de faciliter le calcul du temps entre chaque image pour int DrawNextFrame().*/ +double delta = (1/FPS)*1000000; +unsigned long suivant = (1/FPS)*1000000; + +/*Un set de fonction permettant de facilement mettre en place des boutons, pouvoir les manipuler et les supprimer + Ces fonctions utilisent la structure struct Button.*/ +void ClearButtons() { + BT_Count = 0; + Buttons = (button*)realloc(Buttons, sizeof(button) * 1); +} + +void AddButton(int x, int y, int w, int h, int id) { + button BT = {x, y, w, h, id}; + BT_Count++; + Buttons = (button*) realloc(Buttons, (BT_Count+1) * sizeof(button)); + Buttons[BT_Count-1] = BT; +} + +int GetButton(int x, int y) { + for (int ID = 0; ID < BT_Count; ID++) { + button BT = Buttons[ID]; + if (x >= BT.x && y >= BT.y && x <= BT.x + BT.w && y <= BT.y + BT.h) { + return BT.id; + } + } + return -1; +} + +/*Des fonctions appelant celles de la bibliothèque graphique afin de réduire la longueur des noms..*/ +couleur GetColorN(char* name) { + return CouleurParNom(name); +} + +couleur GetColor(unsigned char r, unsigned char g, unsigned char b) { + return CouleurParComposante(r, g, b); +} + +void SetColor(unsigned char r, unsigned char g, unsigned char b) { + ChoisirCouleurDessin(CouleurParComposante(r, g, b)); +} + +void SetColorC(couleur Color) { + ChoisirCouleurDessin(Color); +} + +void SetColorN(char* name) { + ChoisirCouleurDessin(CouleurParNom(name)); +} + +/*Permet de faire tourner la partie graphique à X images par secondes définit par la constante FPS.*/ +int DrawNextFrame() { + if (Microsecondes() >= suivant) { + suivant = Microsecondes() + delta; + return 1; + } + + return 0; +} \ No newline at end of file diff --git a/APL1.2/TP07/graph_sup.h b/APL1.2/TP07/graph_sup.h new file mode 100644 index 0000000..fac112b --- /dev/null +++ b/APL1.2/TP07/graph_sup.h @@ -0,0 +1,32 @@ +#ifndef _GRAPH_SUP_H +#define _GRAPH_SUP_H + +#define WIDTH 1200 +#define HEIGHT 700 + +struct Button { + int x; + int y; + int w; + int h; + int id; +}; + +typedef struct Button button; + +extern button* Buttons; +extern int BT_Count; + +void ClearButtons(); +int GetButton(int x, int y); +void AddButton(int x, int y, int w, int h, int id); + +int DrawNextFrame(void); + +couleur GetColorN(char* name); +couleur GetColor(unsigned char r, unsigned char g, unsigned char b); +void SetColor(unsigned char r, unsigned char g, unsigned char b); +void SetColorC(couleur Color); +void SetColorN(char* name); + +#endif \ No newline at end of file diff --git a/APL1.2/TP07/raffinements.c b/APL1.2/TP07/raffinements.c new file mode 100644 index 0000000..0af5899 --- /dev/null +++ b/APL1.2/TP07/raffinements.c @@ -0,0 +1,122 @@ +#include +#include +#include + +struct char_ { + char letter; + struct char_* next; + struct char_* prev; +}; + +typedef struct char_ string_char; +typedef string_char* string; + + +string toString(char* str) { + + string_char* last_c = NULL; + for (int i = strlen(str); i >= 0; i--) { + string_char* c = malloc(sizeof(string_char)); + c->letter = str[i]; + c->next = last_c; + if (last_c != NULL) last_c->prev = c; + last_c = c; + } + + return last_c; +} + +char* toCharT(string str) { + int count = 0; + string str2 = str; + while (str2 != NULL) { + count++; + str2 = str2->next; + } + char* new_str = calloc(sizeof(char), count); + + for (int i = 0; i < count; i++) { + new_str[i] = str->letter; + str = str->next; + } + return new_str; +} + +void sputs(string str) { + while (str != NULL) { + printf("%c", str->letter); + str = str->next; + } + printf("\n"); +} + +void sfgets(string* str, int maxLength, FILE * stream) { + char line[2048]; + fgets(line, maxLength, stream); + + *str = toString(line); +} + +int sstrlen(string str) { + int count = 0; + while (str != NULL) { + count++; + str = str->next; + } + + return count - 1; +} + +void sstrcpy(string* str1, string str2) { + string_char* last_c = NULL; + int length = sstrlen(str2); + while (str2->next != NULL) str2 = str2->next; + + for (int i = length; i >= 0; i--) { + string_char* c = malloc(sizeof(string_char)); + c->next = last_c; + c->letter = str2->letter; + str2 = str2->prev; + if (last_c != NULL) c->prev = last_c; + last_c = c; + } + + *str1 = last_c; +} + +int sstrcmp(string str1, string str2) { + while (str1 != NULL || str2 != NULL) { + if (str1 == NULL && str2 == NULL) break; + else if (str1 == NULL && str2 != NULL) return -1; + else if (str1 != NULL && str2 == NULL) return -1; + else if (str1->letter != str2->letter) return -1; + + str1 = str1->next; + str2 = str2->next; + } + + return 0; +} + +string sstrcat(string* stra, string* strb) { + string str1 = *stra; + string str2 = *strb; + + while (str1->next->next != NULL) str1 = str1->next; + str1->next = str2; + str2->prev = str1; + while (str1->prev != NULL) str1 = str1->prev; +} + +int main(int argc, char * argv[]) { + string c = toString("Hello World !"); + string d = NULL; + sstrcpy(&d, c); + sputs(d); + sputs(c); + sstrcat(&d, &c); + sputs(d); + + return EXIT_SUCCESS; +} +