From 9cf596f4cea4c97fee6e3a29660255edcc694b0b Mon Sep 17 00:00:00 2001 From: "dario.genga" <dario.genga@etu.hesge.ch> Date: Tue, 3 May 2022 14:47:48 +0200 Subject: [PATCH] Add unfinished ex 4 and 5 --- ex4/ex4.c | 87 +++++++++++++++++++++++++++++++++++++++++ ex5/ex5.c | 115 +++++++++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 201 insertions(+), 1 deletion(-) diff --git a/ex4/ex4.c b/ex4/ex4.c index c3867d0..0d3ec90 100644 --- a/ex4/ex4.c +++ b/ex4/ex4.c @@ -8,6 +8,93 @@ #include <string.h> #include <stdbool.h> +#define QUADTREE_SIZE 16 + +typedef struct _node { + int info; + struct _node *child[4]; // or struct _node **child; +} node; + +bool is_leaf(node *tree) { + return (NULL == tree->child[0]); +} + +int max(int x, int y) { + return (x >= y ? x : y); +} + +int max_depth(int depths[4]) { + int m = depths[0]; + for (int i = 1; i < 4; ++i) { + m = max(m, depths[i]); + } + return m; +} + +int depth(node *qt) { + int depths[] = {0, 0, 0, 0}; + if (is_leaf(qt)) { + return 0; + } else { + for (int i = 0; i < 4; ++i) { + depths[i] = depth(qt->child[i]); + } + return 1 + max_depth(depths); + } +} + +node *qt_create(int depth) { + node *n = calloc(1, sizeof(node)); + if (depth > 0) { + for (int i = 0; i < 4; i++) { + n->child[i] = qt_create(depth - 1); + } + } + return n; +} + +node *position(int row, int col, node* tree) { + int d = depth(tree); + while (d > 1) { + int index = 2 * ((row % (int)pow(2, d)) / (int)pow(2, (d - 1))) + (col % (int)pow(2, d)) / (int)pow(2, (d - 1)); + tree = tree->child[index]; + d -= 1; + } + return tree; +} + +node *create_tree_from_matrix(int nb_row, int nb_col, int matrix[nb_row][nb_col], int depth) { + node *qt = qt_create(depth); + for (int row = 0; row < nb_row; row++) { + for (int col = 0; col < nb_col; col++) { + node *current = position(row, col, qt); + current->info = matrix[row][col]; + } + } + return qt; +} + int main() { + int row = QUADTREE_SIZE; + int col = QUADTREE_SIZE; + int matrix[16][16] = { + {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, + {0, 3, 3, 3, 3, 0, 0, 7, 7, 7, 7, 0, 0, 11, 11, 11}, + {0, 3, 0, 0, 0, 0, 0, 7, 0, 0, 0, 0, 0, 11, 0, 0}, + {0, 3, 3, 3, 0, 0, 0, 7, 7, 7, 0, 0, 0, 11, 11, 11}, + {0, 3, 0, 0, 0, 0, 0, 7, 0, 0, 0, 0, 0, 11, 0, 0}, + {0, 3, 0, 0, 0, 0, 0, 7, 7, 7, 7, 0, 0, 11, 11, 11}, + {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, + {0, 3, 3, 3, 3, 0, 0, 7, 7, 7, 7, 0, 0, 11, 11, 11}, + {0, 3, 0, 0, 0, 0, 0, 7, 0, 0, 0, 0, 0, 11, 0, 0}, + {0, 3, 3, 3, 0, 0, 0, 7, 7, 7, 0, 0, 0, 11, 11, 11}, + {0, 3, 0, 0, 0, 0, 0, 7, 0, 0, 0, 0, 0, 11, 0, 0}, + {0, 3, 0, 0, 0, 0, 0, 7, 7, 7, 7, 0, 0, 11, 11, 11}, + {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, + {0, 3, 3, 3, 3, 0, 0, 7, 7, 7, 7, 0, 0, 11, 11, 11} + }; + return EXIT_SUCCESS; } diff --git a/ex5/ex5.c b/ex5/ex5.c index d4d2d00..9fbb0a9 100644 --- a/ex5/ex5.c +++ b/ex5/ex5.c @@ -1,5 +1,5 @@ /* Author : Dario GENGA - * Date : 03.05.2022 + * Date : 03.05 .2022 * Description : ContrĂ´le continue 3 - Exercice 5 */ @@ -7,7 +7,120 @@ #include <stdlib.h> #include <string.h> #include <stdbool.h> +#include <math.h> + +#define QUADTREE_SIZE 16 + +typedef struct _node { + int info; + struct _node *child[4]; // or struct _node **child; +} node; + +bool is_leaf(node *tree) { + return (NULL == tree->child[0]); +} + +int max(int x, int y) { + return (x >= y ? x : y); +} + +int max_depth(int depths[4]) { + int m = depths[0]; + for (int i = 1; i < 4; ++i) { + m = max(m, depths[i]); + } + return m; +} + +int depth(node *qt) { + int depths[] = {0, 0, 0, 0}; + if (is_leaf(qt)) { + return 0; + } else { + for (int i = 0; i < 4; ++i) { + depths[i] = depth(qt->child[i]); + } + return 1 + max_depth(depths); + } +} + +node *qt_create(int depth) { + node *n = calloc(1, sizeof(node)); + if (depth > 0) { + for (int i = 0; i < 4; i++) { + n->child[i] = qt_create(depth - 1); + } + } + return n; +} + +node *position(int row, int col, node* tree) { + int d = depth(tree); + while (d > 1) { + int index = 2 * ((row % (int)pow(2, d)) / (int)pow(2, (d - 1))) + (col % (int)pow(2, d)) / (int)pow(2, (d - 1)); + tree = tree->child[index]; + d -= 1; + } + return tree; +} + +node *create_tree_from_matrix(int nb_row, int nb_col, int matrix[nb_row][nb_col], int depth) { + node *qt = qt_create(depth); + for (int row = 0; row < nb_row; row++) { + for (int col = 0; col < nb_col; col++) { + node *current = position(row, col, qt); + current->info = matrix[row][col]; + } + } + return qt; +} + +void transform(node *qt, int size, int indices[size]) { + node *current = qt; + for (int i = 0; i < size; i++) { + current = qt->child[indices[i]]; + int childs[4] = { + current->child[0]->info, + current->child[1]->info, + current->child[2]->info, + current->child[3]->info, + }; + int max = max_depth(childs); + + } +} int main() { + int row = QUADTREE_SIZE; + int col = QUADTREE_SIZE; + int matrix[16][16] = { + {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, + {0, 3, 3, 3, 3, 0, 0, 7, 7, 7, 7, 0, 0, 11, 11, 11}, + {0, 3, 0, 0, 0, 0, 0, 7, 0, 0, 0, 0, 0, 11, 0, 0}, + {0, 3, 3, 3, 0, 0, 0, 7, 7, 7, 0, 0, 0, 11, 11, 11}, + {0, 3, 0, 0, 0, 0, 0, 7, 0, 0, 0, 0, 0, 11, 0, 0}, + {0, 3, 0, 0, 0, 0, 0, 7, 7, 7, 7, 0, 0, 11, 11, 11}, + {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, + {0, 3, 3, 3, 3, 0, 0, 7, 7, 7, 7, 0, 0, 11, 11, 11}, + {0, 3, 0, 0, 0, 0, 0, 7, 0, 0, 0, 0, 0, 11, 0, 0}, + {0, 3, 3, 3, 0, 0, 0, 7, 7, 7, 0, 0, 0, 11, 11, 11}, + {0, 3, 0, 0, 0, 0, 0, 7, 0, 0, 0, 0, 0, 11, 0, 0}, + {0, 3, 0, 0, 0, 0, 0, 7, 7, 7, 7, 0, 0, 11, 11, 11}, + {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, + {0, 3, 3, 3, 3, 0, 0, 7, 7, 7, 7, 0, 0, 11, 11, 11} + }; + int parcours[2] = {3, 1}; + node* qTree = create_tree_from_matrix(row, col, matrix, 4); + transform(qTree, 3, parcours); + + for (int r = 0; r < row; r++) { + for (int c = 0; c < col; c++) { + printf("%d ", matrix[r][c]); + } + printf("\n"); + } + return EXIT_SUCCESS; } -- GitLab