diff --git a/perso/galaxy.c b/perso/galaxy.c index b7029607ad42e96acaef86c1a0a372896a0d4872..ac6ee68251692741a3a88402564ab35738f01811 100644 --- a/perso/galaxy.c +++ b/perso/galaxy.c @@ -30,63 +30,97 @@ * * * * * * * * * * * * * * * * * * */ int main(int argc, char **argv) { - /* - gestion_de_la_ligne_de_commande(); - allocation_mémoire_et_initialisation_de_la_galaxie(); - itérations_temporelles { - liberation_des_etoiles_qui_sortent_du_domaine(); - affichage();mise_a_zero_des_accelerations(); - creation_quad_tree(); - mise_a_jour_des_accelerations_via_le_quad_tree(); - mise_a_jour_des_positions(); - destruction_du_quad_tree(); - - destruction_galaxie(); - */ - - printf("\n\n"); + if (argc == 3) { - tests_vector(); - tests_box(); - tests_star(); - // tests_quadtree(); + // convertit argv[#] en int + int nb_stars = atoi(argv[1]); + double theta = atoi(argv[2]); - // tests_galaxy(); - - // on crée une étoile en (0;0) qui a comme masse 10^6 * masse solaire - // sun = new_star_vel(new_vec(0, 0), Vector speed, Vector acc, (math.powf(10, 6) * MASSE_SOLAIRE), DELTA_T) + printf("\n\n"); + tests(); + + Box box_initial = new_box(0.0, 0.0, 10.0, 10.0); + // Galaxy* galaxy = create_and_init_galaxy(nb_stars, box_initial, DELTA_T); + + + + + + /* + gestion_de_la_ligne_de_commande(); + allocation_mémoire_et_initialisation_de_la_galaxie(); + itérations_temporelles { + liberation_des_etoiles_qui_sortent_du_domaine(); + affichage();mise_a_zero_des_accelerations(); + creation_quad_tree(); + mise_a_jour_des_accelerations_via_le_quad_tree(); + mise_a_jour_des_positions(); + destruction_du_quad_tree(); + + destruction_galaxie(); + */ + + + } else { + printf("Nombre d'arguments non valide"); + exit(1); + } } + + + /* * * * * * * * * * * * * * * * * * * * --- FONCTIONS --- * * * * * * * * * * * * * * * * * * * */ -/* -Galaxy* create_and_init_galaxy(int nb_bodies, Box box, double delta_t) { +Galaxy* create_and_init_galaxy(int nb_bodies, Box box, double delta_t) { + Star* list_stars = malloc(sizeof(Star) * nb_bodies); + // on crée une étoile en (0;0) qui a comme masse 10^6 * masse solaire + // Star* sun = new_star_vel(new_vec(0, 0), speed, acc, 1e6 * MASSE_SOLAIRE, DELTA_T); + // list_stars[0] = sun; + + for (int i = 1; i <= nb_bodies; i++) { + // Star* temp = new_star_vel(); + } } void reset_acc_galaxy(Galaxy* galaxy) { - + printf("bla"); } void update_pos_galaxy(Galaxy* galaxy, double delta_t) { - + printf("bla"); } void free_galaxy(Galaxy* galaxy) { - + } void resize_galaxy(Galaxy* galaxy) { - + double enlarge = 10.0; + + galaxy -> box.x0 -= enlarge; + galaxy -> box.y0 += enlarge; + + galaxy -> box.x1 += enlarge; + galaxy -> box.y1 -= enlarge; } -*/ \ No newline at end of file + + +void tests() { + tests_vector(); + tests_box(); + tests_star(); + // tests_quadtree(); + // tests_galaxy(); +} \ No newline at end of file diff --git a/perso/galaxy.h b/perso/galaxy.h index 0599320e2b92cdc17f6004ee3356d1db87efc772..e12526cd1a6ed8c6009bb4179c37111b45312617 100644 --- a/perso/galaxy.h +++ b/perso/galaxy.h @@ -9,8 +9,8 @@ #include "star.h" -#define FORCE_GRAVITATION (6.67 * math.pow(10, -11)) -#define NB_PLANETS 10 + +#define DELTA_T 1e10 // discrétisation temporelle /* * * * * * * * * * * * * * * * * * * @@ -43,8 +43,6 @@ typedef struct __node { * * * * * * * * * * * * * * * * * * */ -/* --- GALAXIE --- */ - // alloue et initialise une galaxie Galaxy* create_and_init_galaxy(int nb_bodies, Box box, double delta_t); @@ -62,49 +60,14 @@ void resize_galaxy(Galaxy* galaxy); +// fait les tests de toutes les librairies +void tests(); -// calcule la force résultante d'une liste de vecteurs -Vector resultante(Vector** list_vectors); - -// calcule la position de la étoile après avoir subi la force résultante des autres vecteurs (deuxième loi de Newton) -void new_position(Star* star, Vector resultante); - -// calcule l'accélération -double acceleration(Star* star, Vector resultante); - -// discrétise le temps - - - - - - - -// UTILITAIRES - -// une seule étoile ne devra pas avoir de masse et de position aléatoire : elle sera en (0,0) et aura math.pow(10, 6) * MASSE_SOLAIRE comme masse - -// donne une masse aléatoire à une étoile -int random_mass(Star* star); -// MASSE_MIN + (R(10) * MASSE_SOLAIRE) - -// donne une position aléatoire à une étoile -Vector random_position(Star* star); -// R * (math.log(1 - random_1()) / 1.8) * chaque composante du vecteur v(0.5 - random_1() ; 0.5 - random_1()) -// où R = math.pow(10, 18) [m] et R(0) est R(0) un nombre aléatoire entre 0 et 1 - -// donne une vitesse aléatoire à une étoile -Vector random_speed(Star* star); -// math.sqrt(FORCE_GRAVITATION * (star -> mass + )) * chaque composante du vecteur v(-sin(phi) ; cos(phi)) -// où phi = atan2(star -> position.y / star -> position.x) // tan⁻¹ - -// calcule une super etoile d'après le poids et la position d'un groupe d'étoiles -Star super_star(Star** list_stars); void insert_star(Node *n, Star *s); @@ -135,10 +98,7 @@ void insert_star(Node *n, Star *s); } */ -int random_10(); -// (rand() % 10) + 1 -double random_1(); #endif \ No newline at end of file diff --git a/perso/star.c b/perso/star.c index 5fb350b7095414457807d292f28b6163a7d01eac..8263e07a52446b45ab5ef1c86e377263eb12c0b3 100644 --- a/perso/star.c +++ b/perso/star.c @@ -2,9 +2,6 @@ #include "star.h" -#define MASSE_MIN 1e20 -#define MASSE_SOLAIRE 1.98892e30 -#define DELTA_T 1e10 // discrétisation temporelle /* * * * * * * * * * * * * * * * * * * @@ -17,21 +14,36 @@ Star* new_star_vel(Vector new_pos, Vector new_speed, Vector new_acc, double new_ Star* new_star = malloc(sizeof(Star)); new_star -> pos = new_pos; + new_star -> acc = new_acc; + new_star -> mass = new_mass; + + + // copie mais en pointeur de speed + Vector* p_speed = new_vec(new_speed.x , new_speed.y); + // est-ce que c'est égal à &new_speed ? // on multiplie le vecteur speed par delta_t (qui retourne un pointeur sur vecteur, que l'on déréférence) - Vector *pos_tmp = mul_vec(&new_speed, new_dt); + Vector* pos_tmp = mul_vec(p_speed, new_dt); new_star -> previous_pos = *pos_tmp; free(pos_tmp); + free(p_speed); + + + + // copie mais en pointeur de previous_pos + Vector* p_pre_pos = new_vec(new_star -> previous_pos.x, new_star -> previous_pos.y); + // copie mais en pointeur de pos + Vector* p_pos = new_vec(new_star -> pos.x, new_star -> pos.y); // et on soustrait ce résultat au vecteur position (qui retourne un pointeur sur vecteur, que l'on déréférence) - pos_tmp = sub_vec(&(new_star -> previous_pos), &new_pos); + pos_tmp = sub_vec(p_pre_pos, p_pos); new_star -> previous_pos = *pos_tmp; free(pos_tmp); + free(p_pre_pos); + free(p_pos); pos_tmp = NULL; - new_star -> acc = new_acc; - new_star -> mass = new_mass; return new_star; } @@ -66,31 +78,82 @@ void print_star(const Star* const star) { masse = %f \n", star -> pos.x, star -> pos.y, star -> previous_pos.x, star -> previous_pos.y, star -> acc.x, star -> acc.y, star -> mass); } -/* + + +int random_mass() { + return MASSE_MIN + (random_10() * MASSE_SOLAIRE); +} + + +Vector random_position() { + // R * (math.log(1 - random_1()) / 1.8) * chaque composante du vecteur v(0.5 - random_1() ; 0.5 - random_1()) + // où R = math.pow(10, 18) [m] et R(0) est R(0) un nombre aléatoire entre 0 et 1 + + Vector position; + double temp = 1e18 * (log(1 - random_1()) / 1.8); + position.x = temp * (0.5 - random_1()); + + temp = 1e18 * (log(1 - random_1()) / 1.8); + position.y = temp * (0.5 - random_1()); + + return position; +} + + +Vector random_speed(Star* star) { + // math.sqrt(FORCE_GRAVITATION * (star -> mass + )) * chaque composante du vecteur v(-sin(phi) ; cos(phi)) + // où phi = atan2(star -> position.y / star -> position.x) // tan⁻¹ + + Vector* p_norm = new_vec(star -> pos.x, star -> pos.y); + double temp = sqrt(FORCE_GRAVITATION * (star -> mass + (1e6 * MASSE_SOLAIRE)) / norm(p_norm)); + double phi = atan2(star -> pos.y, star -> pos.x); + + Vector speed; + speed.x = temp * (-sin(phi)); + speed.y = temp * (cos(phi)); + + return speed; +} + + // il faut deja faire une liste de stars qui se trouvent dans une box Star* super_star(Star* list_stars, int size_list) { - Star* super_star = malloc(sizeof(Star)) + Star* super_star = malloc(sizeof(Star)); for (int i = 0; i < size_list; i++) { // position - super_star -> pos -> x += list_stars[i] -> pos -> x; - super_star -> pos -> y += list_stars[i] -> pos -> y; + super_star -> pos.x += list_stars[i].pos.x; + super_star -> pos.y += list_stars[i].pos.y; // accélération - super_star -> acc -> x += list_stars[i] -> acc -> x; - super_star -> acc -> y += list_stars[i] -> acc -> y; + super_star -> acc.x += list_stars[i].acc.x; + super_star -> acc.y += list_stars[i].acc.y; // masse - super_star -> mass -> x += list_stars[i] -> pos -> masse; + super_star -> mass += list_stars[i].mass; } // ne donne pas la même chose si on fait /= 2 pour chaque boucle for - super_star -> mass /= size_list; + super_star -> pos.x /= size_list; + super_star -> pos.y /= size_list; + super_star -> acc.x /= size_list; + super_star -> acc.y /= size_list; + super_star -> mass /= size_list; return super_star; } -*/ + + + + +int random_10() { + return ((rand() % 10) + 1); +} + +double random_1() { + return ((rand() % 10) / 10); +} @@ -113,6 +176,8 @@ void tests_star() { print_star(s1); + + free(s1); free(position); free(speed); diff --git a/perso/star.h b/perso/star.h index 9950834491bf16d01be5370dd68cd611a74a68dd..1216c3b638b9c521eb00fa970980d392020a1cd2 100644 --- a/perso/star.h +++ b/perso/star.h @@ -4,6 +4,12 @@ #include "vector.h" +#define MASSE_MIN 1e20 +#define MASSE_SOLAIRE 1.98892e30 +#define DELTA_T 1e10 // discrétisation temporelle +#define FORCE_GRAVITATION (6.67 * 1e-11) + + /* * * * * * * * * * * * * * * * * * * * --- STRUCTURES --- * @@ -41,6 +47,38 @@ void update_pos_star(Star* star, double delta_t); // affiche les champs d'une étoile void print_star(const Star* const star); + +// calcule la force résultante d'une liste de vecteurs +Vector resultante(Vector** list_vectors); + +// calcule la position de la étoile après avoir subi la force résultante des autres vecteurs (deuxième loi de Newton) +void new_position(Star* star, Vector resultante); + +// calcule l'accélération +double acceleration(Star* star, Vector resultante); + + + +// retourne une masse aléatoire +int random_mass(); + +// retourne une position aléatoire +Vector random_position(); + +// retourne une vitesse aléatoire +Vector random_speed(Star* star); + + +// calcule une super etoile d'après le poids et la position d'un groupe d'étoiles +Star* super_star(Star* list_stars, int size_list); + +int random_10(); +double random_1(); + + + + + void tests_star();